How to change footer of a particular page with a particular header title in TCPDF

为君一笑 提交于 2020-01-03 02:21:09

问题


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');


来源:https://stackoverflow.com/questions/49187600/how-to-change-footer-of-a-particular-page-with-a-particular-header-title-in-tcpd

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