[英]TCPDF Change Footer on last page
我正在尝试使用TCPDF创建一个pdf,并在最后一页需要一个不同的页脚
使用下面的代码我可以在第一页上获得不同的页脚,但不是最后一页
我已经看过几篇关于此的帖子,但无法使其发挥作用
任何帮助实现这一点将非常感激
public function Footer() {
$tpages = $this->getAliasNbPages();
$pages = $this->getPage();
$footer = 'NORMAL' . $pages . $tpages;
if ($pages == 1 ) $footer = 'FIRST' . $pages . $tpages;
if ($pages == $tpages) $footer = 'LAST' . $pages . $tpages;
$this->Cell(0, 10, $footer, 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
这给了我
第1页 - FIRST13第2页 - NORMAL23第3页(最后一页)NORMAL23
回答:
public function Footer() {
$tpages = $this->getAliasNbPages();
$pages = $this->getPage();
$footer = 'NORMAL' . $pages . $tpages;
if ($pages == 1 ) $footer = 'FIRST' . $pages . $tpages;
if ($this->end == true) $footer = 'LAST' . $pages . $tpages;
$this->Cell(0, 10, $footer, 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
function display() {
#function that has main text
$this->AddPage();
$html = '1st page';
$this->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
$this->AddPage();
$html = '2nd page';
$this->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
$this->AddPage();
$html = 'Last page';
$this->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
$this->end = true;
}
你自己的答案没有回答你的问题,在最后一页上有一个不同的页脚。
我从tcPDF本人的作者那里找到了以下代码 ,它完全符合您的要求。
class mypdf extends tcpdf {
protected $last_page_flag = false;
public function Close() {
$this->last_page_flag = true;
parent::Close();
}
public function Footer() {
if ($this->last_page_flag) {
// ... footer for the last page ...
} else {
// ... footer for the normal page ...
}
}
}
现在该代码可以正常工作,但前提是您的最后一页不同。 在我的情况下,我可以有0-X的最后页面,所以我仍然需要依赖页面计数器。 这段代码适合我:
class mypdf extends tcpdf {
public $page_counter = 1;
public function Make() {
...
// Create your own method for determining how many pages you got, excluding last pages
$this->page_counter = NUMBER;
...
}
public function Footer() {
if ($this->getPage() <= $this->page_counter) {
// ... footer for the normal page ...
} else {
// ... footer for the last page(s) ...
}
}
}
我希望这有帮助:
if($this->page == 1){
$this->Cell(0, 10, "ORIGINAL - Pagina '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
} else {
$this->Cell(0, 10, "COPY - Pagina '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
要么
define("titulos_pie_pdf", ",ORIGINAL, COPY, TITLE1, TITLE2, ...", true);
然后
$titulos = explode(",",titulos_pie_pdf);
$this->Cell(0, 10, $titulos[$this->page]." - Pagina '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
我不是一个大程序员,但这段代码对我有帮助。
嗨我有类似的问题,这解决了它:
public $isLastPage = false;
public function Footer()
{
if($this->isLastPage) {
$this->writeHTML($this->footer);
}
}
public function lastPage($resetmargins=false) {
$this->setPage($this->getNumPages(), $resetmargins);
$this->isLastPage = true;
}
希望它会帮助你:)它不完全是你想要的但它的easyli可调节我认为:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.