TCPDF set different headers for different pages in one document

bububaba picture bububaba · Mar 1, 2012 · Viewed 27.9k times · Source

Is there a way to have a different header logo for the 1st page in a document and different for the 2nd page?

I thought that changing the header data between adding pages might do the trick, but in my tests it seems that setting the header after adding the first page has no effect:

/* other stuff
$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->AliasNbPages();
*/

$pdf->SetHeaderData("logo_1.png", PDF_HEADER_LOGO_WIDTH, '', '');
$pdf->AddPage();
$pdf->writeHTML($htmlContent, true, 0, true, true);

$pdf->SetHeaderData("logo_2.png", PDF_HEADER_LOGO_WIDTH, '', '');
$pdf->AddPage();
$pdf->writeHTML($htmlContent2, true, 0, true, true);

The above produces a document with 2 pages, both having logo_1.png in header.

Will I need to customize TCPDF itself? Has anyone done this? I'm using version 5.9.144.

Answer

LR1234567 picture LR1234567 · Mar 13, 2012

Strange. I'm having the same issue, but this worked in my older version of TCPDF version:4.8.009 and I noticed the issue when I upgraded to 5.9.149.

I compared the 2 and isolated the issue to the Header() function.

I could force it to allow me to change the header and accept it by running this:

$pdf->setHeaderTemplateAutoreset(true);