TCPDF set different headers for different pages in one document

前提是你 提交于 2019-12-20 19:46:58

问题


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.


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




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



回答3:


I used:

$pdf->resetHeaderTemplate();

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




回答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:

  • Merge PDF files with PHP
  • Merge files into a single PDF using PHP/linux



回答5:


Just for the record, if someone has the same problem in the future and can use Zend_Pdf:

// $filename is the final filename with path to save the generated PDF
$dir = dirname($filename);
$base = basename($filename);

$page1 = $dir . DIRECTORY_SEPARATOR . "tmp_1_" . $base;
$page2 = $dir . DIRECTORY_SEPARATOR . "tmp_2_" . $base;

//creates 1st page with TCPDF and saves to filesystem with filename $page1
$this->generateInvoicePage1($html_1, $page1);

//creates 2nd page with TCPDF and saves to filesystem with filename $page2
$this->generateInvoicePage2($html_2, $page2);

$pdf1 = Zend_Pdf::load($page1);
$pdf2 = Zend_Pdf::load($page2);

foreach ($pdf2->pages as $page) {
    $pdf1->pages[] = clone($page);
}

$pdf1->save($filename);

unlink($page1);
unlink($page2);



回答6:


I found this to be the solution with the lightest touch:

class MYPDF extends TCPDF {

    //Page header
    public function AddNewHeader($newTitle) {
        $this->header_xobj_autoreset = true;
        $this->header_title = $newTitle;
    }

}

Be sure to call TCPDF::setHeaderData() first. Next, call this function before every AddPage() event, or, if you're looping through data and relying on tcpdf to add pages, call it after every element add. It breaks the caching of the header, but allows the user to put a new and custom header on each page. All the elements returned by TCPDF::getHeaderData() can be updated in this way.




回答7:


If you wish to have a cover page without header and footer and internal pages with them, there is an easier way to handle it. Simply turn off the header and footer print via 'setPrintHeader' and 'setPrintFooter' as follows:

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

$pdf->AddPage();
$pdf->SetFont("freesans", "B", 20);
$pdf->Cell(0,10,"COVER TEXT",1,1,'C');

$pdf->setPrintHeader(true);
$pdf->setPrintFooter(true);

$pdf->setHeaderFont(array("freesans", "", 9));
$pdf->SetHeaderData('', '', 'Document Title', 'Document Header Text');
$pdf->AddPage();
$pdf->SetFont("freesans", "B", 20);
$pdf->Cell(0,10,"Internal text",1,1,'C');

$pdf->Output("HappyCover.pdf", "I");

Enjoy!



来源:https://stackoverflow.com/questions/9513145/tcpdf-set-different-headers-for-different-pages-in-one-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!