Reputation: 11
Add background image in all tcpdf pages , How do I add background image on tcpdf i tried everything but the image just won't show o\in all other pages , only shows first page
Upvotes: 1
Views: 4364
Reputation: 8103
Please use a custom header to set the background to your PDF, and then every page will have the background
For example, if you want to use './certbg/cs.jpg' as the background image, then :
$img_file = './certbg/cs.jpg';
(A) In order to display the background image properly, we have to disable auto-page-break, set the background and then restore the page break afterward - all to be done in the custom header.
(B) I have intentionally use $pdf->AddPage();
TWICE to demonstrate that the background image will be applied to both of the two pages. Actually you may add further pages and will see that all the pages will have the background image. This is because "header" will be effective for all the pages and this is a known trick to fulfill what you want.
Note: Recommended background image size (for a A4 portrait PDF): 1134px * 1600px
The code (fully working) is:
<?php
require_once('./tcpdf/config/lang/eng.php');
require_once('./tcpdf/tcpdf.php');
// 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 = './certbg/cs.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();
}
}
// create new PDF document
$pdf = new MYPDF("P", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Author Name');
$pdf->SetTitle('PDF');
$pdf->SetSubject('PDF');
$pdf->SetKeywords('TCPDF, HK');
//$pdf->SetProtection(array('print', 'copy'), '', null, 0, null);
$pdf->SetProtection(array('copy'), '', null, 0, null);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0);
// remove default footer
$pdf->setPrintFooter(false);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 0);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set font
$pdf->SetFont('times', '', 48);
// add a page
$pdf->AddPage();
$html.= '<table border=0 style="">
<tr><td style="height:180px;"> </td></tr>
<tr><td align="center" style="font-size:80px;height:180px;color:#fa8832;text-align:center;">'. ' Page 1'. '</td></tr>
<tr><td style="height:450px;"> </td></tr>
<tr><td align="center" style="font-size:30px;color:#444444;">'. ' Page 1 ' . '</td></tr>
</table>';
$pdf->writeHTML($html, true, false, true, false, '');
$html= '<table border=0 style="">
<tr><td style="height:180px;"> </td></tr>
<tr><td align="center" style="font-size:80px;height:180px;color:#fa8832;text-align:center;">'. ' Page 2'. '</td></tr>
<tr><td style="height:450px;"> </td></tr>
<tr><td align="center" style="font-size:30px;color:#444444;">'. ' Page 2 ' . '</td></tr>
</table>';
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
// ---------------------------------------------------------
$y1=chr(mt_rand(65, 90));
$y2=chr(mt_rand(65, 90));
$y3=chr(mt_rand(48, 57));
$y4=chr(mt_rand(65, 90));
$y5=chr(mt_rand(48, 57));
$y6=chr(mt_rand(65, 90));
$y7=chr(mt_rand(48, 57));
$y8=chr(mt_rand(48, 57));
$y9=chr(mt_rand(65, 90));
$ry=$y1.$y2.$y3.$y4.$y5.$y6.$y7.$y8.$y9;
$pdf->Output($ry.".pdf",'FD');
?>
Upvotes: 1