And*_*rew 1 html php pdf tcpdf
我正在使用TCPDF库生成PHP/HTML格式的PDF.这些文档可以在这里找到:http://www.tcpdf.org/doc/code/classTCPDF.html.
我正在尝试创建一个目录(如TCPDF站点上的示例59所示)但我遇到了一些问题:
1)目录页码显示为文档的最后一页.(实际上是8个中的2个,在封面后出现).
2)其他页面上的页码不调整.TOC之后的页面应该是8个中的3个,而是8个中的2个.
3)目录具有正确的书签页码,但这些号码与这些页面上的页码不匹配(与问题#2相关).
我是如何生成书签的:
我通过在每个添加的页面后调用Bookmark()方法为每个页面生成书签.
$pdf->AddPage();
$pdf->Bookmark('Chapter 1', 0, 0, '', 'B', array(0,64,128));
Run Code Online (Sandbox Code Playgroud)
我如何生成目录:
这直接来自上面链接的实施例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();
Run Code Online (Sandbox Code Playgroud)
我如何获取页脚的页码:
// 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');
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,我希望目录落在第2页,页脚会显示第2页,所有后续页码都要正确标记.我怎样才能做到这一点?如果需要,我可以澄清问题/代码.
任何帮助表示赞赏:)
小智 5
在计算页面时,PageNo()有点时髦.它按照创建顺序输出它是什么页面,并且由于ToC页面是最后一个页面,它将显示为最后一页.
所以改为使用
// Page number
$this->Cell(0, 12, ' '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
Run Code Online (Sandbox Code Playgroud)
您提供的其余代码(如检查它是否是第一页)应与PageNo()一起使用.
| 归档时间: |
|
| 查看次数: |
4831 次 |
| 最近记录: |