TCPDF - Having footer/header on certain pages only

不问归期 提交于 2020-01-02 04:50:13

问题


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


回答1:


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  

}



回答2:


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


来源:https://stackoverflow.com/questions/8520446/tcpdf-having-footer-header-on-certain-pages-only

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