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 page
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);
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:
my solution, with just a condition
function Header(){
if($this->page==1){
$html = '<div><img src="./outils/img1.png" alt=""></div>';
$this->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
}else{
$html = '<div><img src="./outils/img2.png" alt=""></div>';
$this->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
}
}
I used:
$pdf->resetHeaderTemplate();
It should override the template header and assign the new one according to need. It worked for me.
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.
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!