Changing or eliminating Header & Footer in TCPDF

南楼画角 提交于 2019-11-29 00:58:25
Brian Showalter

Use the SetPrintHeader(false) and SetPrintFooter(false) methods before calling AddPage(). Like this:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', true, 'UTF-8', false);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();

A nice easy way to have control over when to show the header - or bits of the header - is by extending the TCPDF class and creating your own header function like so:

  class YourPDF extends TCPDF {
        public function Header() {
            if (count($this->pages) === 1) { // Do this only on the first page
                $html .= '<p>Your header here</p>';
            }

            $this->writeHTML($html, true, false, false, false, '');
        }
    }

Naturally you can use this to return no content as well, if you'd prefer to have no header at all.

Here is an alternative way you can remove the Header and Footer:

// Remove the default header and footer
class PDF extends TCPDF { 
    public function Header() { 
    // No Header 
    } 
    public function Footer() { 
    // No Footer 
    } 
} 

$pdf = new PDF();

How do I eliminate/override this?

Also, Example 3 in the TCPDF docs shows how to override the header and footer with your own class.

Kracekumar
// set default header data
$pdf->SetHeaderData('', PDF_HEADER_LOGO_WIDTH, 'marks', 'header string');

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

With the help of above functions you can change header and footer.

Example:
- First page, no footer
- Second page, has footer, start with page no 1

Structure:

    // First page
    $pdf->startPageGroup();
    $pdf->setPrintFooter(false);

    $pdf->addPage();
    // ... add page content here
    $pdf->endPage();

    // Second page
    $pdf->startPageGroup();
    $pdf->setPrintFooter(true);

    $pdf->addPage();
    // ... add page content here
    $pdf->endPage();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!