How to change footer of a particular page with a p

2019-06-06 13:33发布

问题:

Iam creating multiple pages in TCPDF using AddPage() method. When creating a second page , i call setHeaderData() method to set a header name. In some cases the first page might overflow and auto page break. I need to identify the page before the first page having the header title set and change its footer only. How to achieve this using TCPDF.

回答1:

One solution is to set a new property that will identify this page when the Footer() method is called by TCPDF.

The example below sets a new PrintCoverPageFooter property to True before creating the first page, then sets it to False before generating the second page. This property is then used in a conditional statement with the page property to create unique footers. There is also a PrintCoverPageHeader property that allows for custom headers on the cover page of the document.

<?php
require_once('tcpdf_include.php');

class MYPDF extends TCPDF {
    public function Header() {
        if ($this->PrintCoverPageHeader) {
            $this->Cell(0, 15, '<< Cover Page Header >> ', 0, false, 'C', 0, '', 0, false, 'M', 'M');
        } else {
            $this->Cell(0, 15, '<< Other Page Header >> ', 0, false, 'C', 0, '', 0, false, 'M', 'M');
        }
    }

    public function Footer() {
        $this->SetY(-15);
        if ($this->PrintCoverPageFooter && $this->page == 1){
            $this->Cell(0, 10, 'Cover Page Footer '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
        } elseif ($this->PrintCoverPageFooter && $this->page == 2){
                $this->Cell(0, 10, 'Cover Page Overflow Footer '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
        } else {
            $this->Cell(0, 10, 'Other Page Footer'.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
        }
    }
}

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// Add first page with first page header and footer.
$pdf->PrintCoverPageHeader = True;
$pdf->PrintCoverPageFooter = True;
$pdf->AddPage();
$pdf->Write(0, str_repeat("Cover Page\n",80), '', 0, 'C', true, 0, false, false, 0);

// Add second page with other header and footer.
$pdf->PrintCoverPageHeader = False;
$pdf->AddPage();
$pdf->PrintCoverPageFooter = False;
$pdf->Write(0, "Second Page", '', 0, 'C', true, 0, false, false, 0);

// Add third page with other header and footer.
$pdf->AddPage();
$pdf->Write(0, "Third Page", '', 0, 'C', true, 0, false, false, 0);

$pdf->Output('example.pdf', 'I');


标签: php tcpdf