Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF set different headers for different pages in one document

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.

like image 996
bububaba Avatar asked Mar 01 '12 08:03

bububaba


3 Answers

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);
like image 107
LR1234567 Avatar answered Oct 07 '22 22:10

LR1234567


The following worked for me,

class MYPDF extends TCPDF{
    function header1(){
        //print whatever the header 1 is
    }
    function Header2(){         
        if($this->page==1){
            //print header 1 and whatever the header 2 is
        }else{
            //print just header 2   
        }
    }
}
like image 25
Joaquin Avatar answered Oct 07 '22 21:10

Joaquin


I used:

$pdf->resetHeaderTemplate();

It should override the template header and assign the new one according to need. It worked for me.

like image 4
user3417572 Avatar answered Oct 07 '22 21:10

user3417572