Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF autopagebreak + backgroundimage

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

like image 256
TwinsIT Avatar asked Jul 03 '12 10:07

TwinsIT


People also ask

How do I upload an image to Tcpdf?

$tcpdf = new TCPDF_TCPDF(); $img = file_get_contents(Mage::getBaseDir('media') . '/dhl/logo. jpg'); $PDF_HEADER_LOGO = $tcpdf->Image('@' . $img);//any image file.

How do I add a background image in Fpdf?

Adding Image to PDF file by FPDF php'); $pdf = new FPDF(); $pdf->AddPage(); $pdf->Image('images/pdf-header. jpg',0,0); $pdf->Output(); ?> The ouput of above code is here . ( Show Output ) We can add position with height, width and link to above code.


1 Answers

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.

like image 62
Máthé Endre-Botond Avatar answered Dec 23 '22 17:12

Máthé Endre-Botond