TCPDF 更改最后一页的页脚 [英] TCPDF Change Footer on last page
问题描述
我正在尝试使用 TCPDF 创建 pdf 并且需要在最后一页上使用不同的页脚
I am trying to create a pdf using TCPDF and need a different footer on the last page
使用以下代码,我可以在第一页而不是最后一页获得不同的页脚
using the following code I can get a different footer on the first page but not the last
我已经看过几篇关于这个的帖子,但无法使它工作
I have looked at several post about this but can not make it work
任何帮助实现这一点将不胜感激
Any help implementing this would be much appreciated
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 页 - 正常 23第 3 页(最后一页)NORMAL23
page1 - FIRST13 page 2 - NORMAL23 page 3 (Last Page) 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 作者本人的代码,这正是您想要的.
Your own answer does not answer your question to have a different footer on last page.
I found the following code from the author of tcPDF himself, which does exactly what you want.
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 的最后一页,所以我仍然需要依靠页面计数器.此代码适用于我:
Now that code works, but only if your last page differ. In my case I could have 0-X last pages, so I still need to rely on a page counter. This code works for me:
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) ...
}
}
}
这篇关于TCPDF 更改最后一页的页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!