如何在 TCPDF 中更改具有特定页眉标题的特定页面的页脚 [英] How to change footer of a particular page with a particular header title in TCPDF

查看:81
本文介绍了如何在 TCPDF 中更改具有特定页眉标题的特定页面的页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AddPage() 方法在 TCPDF 中创建多个页面.创建第二个页面时,我调用 setHeaderData() 方法来设置标题名称.在某些情况下,第一页可能会溢出并自动分页.我需要在设置了页眉标题的第一页之前识别页面并仅更改其页脚.如何使用 TCPDF 实现这一点.

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.

推荐答案

一种解决方案是设置一个新属性,当 Footer() 方法被 TCPPDF 调用时,该属性将标识此页面.

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

下面的示例在创建第一页之前将新的 PrintCoverPageFooter 属性设置为 True,然后在生成第二页之前将其设置为 False.然后在带有 page 属性的条件语句中使用此属性来创建唯一的页脚.还有一个 PrintCoverPageHeader 属性允许在文档封面上自定义标题.

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

这篇关于如何在 TCPDF 中更改具有特定页眉标题的特定页面的页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆