TCPDF autopagebreak + backgroundimage

TwinsIT picture TwinsIT · Jul 3, 2012 · Viewed 19.3k times · Source

I am busy with a project that needs a lot of pdf files. Because all of them need the design of the company I use a background-image with the logo/watermark.

Everything goes fine if I have only 1 page, but when there are multiple pages, the background is only on the first.

$pdf->Image('bg/background.jpg', 0, 0, 210, 297, '', '', '', false, 0, '', false, false, 0);
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setPageMark();
$pdf->SetAutoPageBreak(true);
$pdf->writeHTML($bodyText, true, true, true, true, '');
$pdf->lastPage();
$pdf->Output('doc.pdf', 'I');

So my $bodyText is more then 1 page...

Is there a solution to have every page a background?

Thanks

Wouter

Answer

Máthé Endre-Botond picture Máthé Endre-Botond · Jul 7, 2012

You could extend the TCPDF class with a custom header function, and add image to header, with TCPDF::Image. There is an example on how to do this within the TCPDF examples

From the example:

// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {
    //Page header
    public function Header() {
        // get the current page break margin
        $bMargin = $this->getBreakMargin();
        // get current auto-page-break mode
        $auto_page_break = $this->AutoPageBreak;
        // disable auto-page-break
        $this->SetAutoPageBreak(false, 0);
        // set bacground image
        $img_file = K_PATH_IMAGES.'image_demo.jpg';
        $this->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
        // restore auto-page-break status
        $this->SetAutoPageBreak($auto_page_break, $bMargin);
        // set the starting point for the page content
        $this->setPageMark();
    }
}

And use MYPDF instead of TCPDF just as you would use TCPDF. The only thing I don't know if the PDF body can overlap with the header, but I think it can if you explicitly specify the margins and the header size.

Let me know if this works.