TCPDF set different headers for different pages in one document

30,057

Solution 1

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);

Solution 2

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   
        }
    }
}

Solution 3

I used:

$pdf->resetHeaderTemplate();

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

Solution 4

How about... have TCPDF generate pages with different headers as separate documents, and then use something to merge all those intermediate PDFs together to form the final document's pages (maybe even TCPDF itself can merge, I don't know)?

A couple of "how to merge?" results:

Share:
30,057
bububaba
Author by

bububaba

Updated on July 05, 2022

Comments

  • bububaba
    bububaba almost 2 years

    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.