sluggerdog
sluggerdog

Reputation: 843

tcpdf - background image - CakePdf

I am trying to setup pdf generation with a background image using CakePdf and the TcPdf engine. I have managed to get the image onto the background using the following settings:

// set background image
$img_file = APP . 'webroot/img/BC_Letterhead.jpg';
$TCPDF->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);

The problem is the image does seem to cover the full background. It seems to be about 1 inch short to the right and about 2 inches short of the bottom of the page. The image I am using is the correct size (I double checked it)

Could this be a margin issue or similar?

Thanks

Upvotes: 2

Views: 20078

Answers (3)

Juangui Jordán
Juangui Jordán

Reputation: 6607

The point is to remove header and footer, setting margins to zero and auto page break to false.

For example, to print an image covering a full A4 page:

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// set margins
$pdf->SetMargins(0, 0, 0, true);

// set auto page breaks false
$pdf->SetAutoPageBreak(false, 0);

// add a page
$pdf->AddPage('P', 'A4');

// Display image on full page
$pdf->Image('background.jpg', 0, 0, 210, 297, 'JPG', '', '', true, 200, '', false, false, 0, false, false, true);

//Close and output PDF document
$pdf->Output('page.pdf', 'I');

There's also an example using a custom header in the documentation:

http://www.tcpdf.org/examples/example_051.phps

Upvotes: 10

minimal
minimal

Reputation: 457

This works for me.

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 background image
    $img_file = K_PATH_IMAGES.'pozadina.jpg';
    $this->Image($img_file, 0, 0, 98, 59, '', '', '', 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();
}
}
$pdf = new MYPDF('L', 'mm', array(59,98));
$pdf->Output('filename.pdf', 'D'); //To force download

Upvotes: 2

sluggerdog
sluggerdog

Reputation: 843

I ended up getting this to work more or less with the following code. The only issue is I still have about 1cm at the bottom without the background image covering it, the width is fine.

        // -- SET BACKGROUND IMAGE ------------------------------>
        $TCPDF->SetFooterMargin(0);

        // get the current page break margin
        $bMargin = $TCPDF->getBreakMargin();

        // get current auto-page-break mode
        $auto_page_break = $TCPDF->getAutoPageBreak();

        // disable auto-page-break
        $TCPDF->SetAutoPageBreak(true, 0);

        // set background image
        $img_file = APP . 'webroot/img/BC_Letterhead.jpg';
        $TCPDF->Image($img_file, 0, 0, 225, 305, '', '', '', false, 300, '', false, false, 0);

        // set the starting point for the page content
        $TCPDF->setPageMark();
        $TCPDF->setPrintFooter(false);
        // END BACKGROUND MODS -------------------------------->

Upvotes: 1

Related Questions