Php TCPDF目录和页码问题

Php TCPDF目录和页码问题,php,html,pdf,tcpdf,Php,Html,Pdf,Tcpdf,我正在使用TCPDF库用PHP/HTML生成PDF。可以在以下位置找到文档: 我正在尝试创建目录(如TCPDF网站中所示),但遇到了一些问题: 1) 目录页码显示为文档的最后一页。(八分之八,实际上是八分之二,在封面之后) 2) 其他页面上的页码未调整。TOC后面的页面应该是第3页,共8页,但应该是第2页,共8页 3) 目录中的书签页码正确,但这些页码与这些页面上的页码不匹配(与第2期相关) 我如何生成书签: // Page footer public function Footer() {

我正在使用TCPDF库用PHP/HTML生成PDF。可以在以下位置找到文档:

我正在尝试创建目录(如TCPDF网站中所示),但遇到了一些问题:

1) 目录页码显示为文档的最后一页。(八分之八,实际上是八分之二,在封面之后)

2) 其他页面上的页码未调整。TOC后面的页面应该是第3页,共8页,但应该是第2页,共8页

3) 目录中的书签页码正确,但这些页码与这些页面上的页码不匹配(与第2期相关)

我如何生成书签:

// 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');

  }
}
我通过在每个添加的页面之后调用Bookmark()方法为每个页面生成书签

$pdf->AddPage();
$pdf->Bookmark('Chapter 1', 0, 0, '', 'B', array(0,64,128));
如何生成目录:

// 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');

  }
}
这是直接从上面链接的示例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();

基本上,我希望目录落在第2页,页脚上写着第2页,所有后续页码都要正确标记。我该怎么做?如果需要,我可以澄清问题/代码

非常感谢您的帮助:)

PageNo()在计算页面时有点古怪。它按照创建顺序输出它是什么页面,并且由于ToC页面是作为最后一个页面创建的,所以它将显示为最后一个页面

所以用

  // Page number
  $this->Cell(0, 12, ' '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
您提供的其余代码(如检查是否为第一页)应使用PageNo()。

PageNo()在计算页面时有点古怪。它按照创建顺序输出它是什么页面,并且由于ToC页面是作为最后一个页面创建的,所以它将显示为最后一个页面

所以用

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

您提供的其余代码(如检查是否为第一页)应使用PageNo()。

因此目录显示了正确的页码,但页面底部的页码不正确,对吗?因此,既然目录的创建没有出现任何问题,您能否提供生成页码的代码部分?@olger感谢您的回复!请看我的编辑。目录显示了正确的页码,但页面底部的页码不正确,对吗?因此,既然目录的创建没有出现任何问题,您能否提供生成页码的代码部分?@olger感谢您的回复!查看我的编辑。效果很好。谢谢你的快速回复!我正在拔头发:)没问题,别忘了检查一下,这是公认的答案:)公认的!还有:关于如何从页脚的页码中删除右边距,你有什么想法吗?右对齐不会将数字与边距对齐。有什么想法吗?不完全确定,也许可以尝试使用SetMargins()函数将右边距设置为0(或您喜欢的值)。啊,太糟糕了。你可以尝试使用SetXY()手动定位页码(或它们所在的单元格),但这有点难看,如果你有不同格式和/或方向的PDF,就会搞砸。效果很好。谢谢你的快速回复!我正在拔头发:)没问题,别忘了检查一下,这是公认的答案:)公认的!还有:关于如何从页脚的页码中删除右边距,你有什么想法吗?右对齐不会将数字与边距对齐。有什么想法吗?不完全确定,也许可以尝试使用SetMargins()函数将右边距设置为0(或您喜欢的值)。啊,太糟糕了。您总是可以尝试使用SetXY()手动定位页码(或它们所在的单元格),但这有点难看,如果您有不同格式和/或方向的PDF,则会造成混乱。