user1099862
user1099862

Reputation: 123

TCPDF - Having footer/header on certain pages only

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

Upvotes: 5

Views: 20703

Answers (2)

Nishanth Rajan
Nishanth Rajan

Reputation: 81

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

Upvotes: 1

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76870

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  

}

Upvotes: 8

Related Questions