【问题标题】:TCPDF table of contents and issues with page numbersTCPDF 目录和页码问题
【发布时间】: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">&nbsp;</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">&nbsp;</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 感谢您的回复!查看我的编辑。

标签: php html pdf tcpdf


【解决方案1】:

PageNo() 在计算页面时有点奇怪。它按创建顺序输出它是哪个页面,并且由于 ToC 页面是作为最后一个页面创建的,因此它将显示为最后一个页面。

所以改为使用

  // Page number
  $this->Cell(0, 12, ' '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');

您提供的其余代码,例如检查它是否是第一页,应该与 PageNo() 一起使用。

【讨论】:

  • 效果很好。感谢您的快速答复!我正在拔头发:)
  • 没问题,别忘了把这个作为接受的答案:)
  • 接受!另外:关于如何从页脚中的页码中删除右边距的任何想法?右对齐不会将数字放在边距上。有什么想法吗?
  • 不完全确定,也许可以尝试使用 SetMargins() 函数将右边距设置为 0(或您喜欢的值)。
  • 啊,太糟糕了。您总是可以尝试使用 SetXY() 手动定位页码(或它们所在的单元格),但这有点难看,如果您有不同格式和/或方向的 PDF,会搞砸。
猜你喜欢
  • 2016-06-18
  • 1970-01-01
  • 2012-01-10
  • 2012-03-14
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
相关资源
最近更新 更多