Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF - Having footer/header on certain pages only

Tags:

tcpdf

I want to place the footer section on every page of my document except the first one.

I created my custom footer by extending the TCPDF class and overriding its footer method.

Then based on the documentation and using below example I understand I should use SetPrintHeader and SetPrintFooter methods:

http://www.tcpdf.org/examples/example_002.phps

// Page one

$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);     
$pdf->AddPage();

// Page two and on ..

$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);     
$pdf->AddPage();

However, the above does not prevent the footer/header from being printed at all!

What am i doing wrong here ?

Thanks a million in advance !!

like image 267
user1099862 Avatar asked Dec 15 '11 12:12

user1099862


2 Answers

I think that

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

prints or hides the header and footer globally so if you do

$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);     
$pdf->AddPage();

// Page two and on ..

$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);   

You are just telling TCPDF to print the header and footer (The last two statements).

What you should do is, in the header and footer function, print things conditionally based on the page you are in. Something like (not tested, i haven't my PHP IDE right now)

function Header(){
   $pageN = PageNo();
   if($pageN % 2 === 0){
      //if page is 2/4/6... don't print anything
       return;
    }else{
       //do your stuff  

}
like image 189
Nicola Peluchetti Avatar answered Nov 10 '22 17:11

Nicola Peluchetti


Header can be controlled by modifying the function startPage in tcpdf.php

In this example, I need headers from page 2 only.

    // print page header
    if ($this->numpages > 1)    {
        $this->setHeader();
    }
like image 1
Nishanth Rajan Avatar answered Nov 10 '22 17:11

Nishanth Rajan