【发布时间】:2014-03-17 01:08:21
【问题描述】:
我正在使用 TCPDF 库通过 PHP/HTML 生成 PDF。可以在此处找到文档:http://www.tcpdf.org/doc/code/classTCPDF.html。
我正在尝试创建目录(如 TCPDF 网站上的 example 59 所示),但遇到了一些问题:
1) 目录页码显示为文档的最后一页。 (8 个中的 8 个,而实际上是 8 个中的 2 个,位于封面之后)。
2) 其他页面的页码没有调整。 TOC 之后的页面应该是 3 of 8,而是显示 2 of 8。
3) 目录具有正确的书签页码,但这些页码与这些页面上的页码不匹配(与问题 #2 相关)。
我如何生成书签:
我通过在每个添加页面后调用 Bookmark() 方法为每个页面生成书签。
$pdf->AddPage();
$pdf->Bookmark('Chapter 1', 0, 0, '', 'B', array(0,64,128));
我如何生成目录:
这是直接从上面链接的示例 59 中提取的。在该示例中,场景略有不同,因为它没有封面。
// add a new page for TOC
$pdf->addTOCPage();
// write the TOC title and/or other elements on the TOC page
$pdf->SetFont('times', 'B', 16);
$pdf->MultiCell(0, 0, 'Table Of Content', 0, 'C', 0, 1, '', '', true, 0);
$pdf->Ln();
$pdf->SetFont('helvetica', '', 10);
// define styles for various bookmark levels
$bookmark_templates = array();
/*
* The key of the $bookmark_templates array represent the bookmark level (from 0 to n).
* The following templates will be replaced with proper content:
* #TOC_DESCRIPTION# this will be replaced with the bookmark description;
* #TOC_PAGE_NUMBER# this will be replaced with page number.
*
* NOTES:
* If you want to align the page number on the right you have to use a monospaced font like courier, otherwise you can left align using any font type.
* The following is just an example, you can get various styles by combining various HTML elements.
*/
// A monospaced font for the page number is mandatory to get the right alignment
$bookmark_templates[0] = '<table border="0" cellpadding="0" cellspacing="0" style="background-color:#EEFAFF"><tr><td width="155mm"><span style="font-family:times;font-weight:bold;font-size:12pt;color:black;">#TOC_DESCRIPTION#</span></td> <td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:12pt;color:black;" align="right"> #TOC_PAGE_NUMBER#</span></td></tr></table>';
$bookmark_templates[1] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="5mm"> </td><td width=" 150mm"><span style="font-family:times;font-size:11pt;color:green;">#TOC_DESCRIPTION#</span></td><td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:11pt;color:green;" align="right">#TOC_PAGE_NUMBER#</span></td></tr ></table>';
$bookmark_templates[2] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="10mm"> </td><td width=" 145mm"><span style="font-family:times;font-size:10pt;color:#666666;"><i>#TOC_DESCRIPTION#</i></span></td><td width="25mm "><span style="font-family:courier;font-weight:bold;font-size:10pt;color:#666666;" align="right">#TOC_PAGE_NUMBER#</span ></td></tr></table>';
// add other bookmark level templates here ...
// add table of content at page 1
// (check the example n. 45 for a text-only TOC
$pdf->addHTMLTOC(2, 'INDEX', $bookmark_templates, true, 'B', array(128,0,0));
// end of TOC page
$pdf->endTOCPage();
我如何获取页脚的页码:
// Page footer
public function Footer() {
$pageN = $this->PageNo();
if($pageN === 1){
// Do nothing
} else {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont($helvetica_light, '', 10);
$this->setTextColor(255,255,255);
// Page number
$this->Cell(0, 12, ' '.$this->PageNo().' of '.$this->getNumPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
基本上,我希望目录位于第 2 页,页脚显示第 2 页,所有后续页码都正确标记。我怎样才能做到这一点?如果需要,我可以澄清问题/代码。
任何帮助表示赞赏:)
【问题讨论】:
-
所以目录显示了正确的页码,但页面底部的页码不正确,对吗?那么既然目录的创建没有任何问题,您能否提供生成要显示的页码的代码部分?
-
@olger 感谢您的回复!查看我的编辑。