Question

I'm using the TCPDF library to generate PDFs with PHP/HTML. The docs can be found here: http://www.tcpdf.org/doc/code/classTCPDF.html.

I am trying to create a table of contents (as demonstrated in example 59 on the TCPDF site) but I'm experiencing a few issues:

1) The Table of Contents page number is showing as the last page of the document. (8 of 8 when in reality it's 2 of 8, comes after a cover page).

2) The page numbers on the other pages are not adjusting. The page after the TOC should be 3 of 8, but instead says 2 of 8.

3) The table of contents has the correct page numbers for the bookmarks, but these numbers don't match the page numbers on those pages (related to issue #2).

How I'm generating bookmarks:

I'm generating bookmarks for each page by calling the Bookmark() method after each added page.

$pdf->AddPage();
$pdf->Bookmark('Chapter 1', 0, 0, '', 'B', array(0,64,128));

How I'm generating the table of contents:

This is ripped directly from Example 59 linked above. In that example, the scenario is slightly different as it does not have a cover page.

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

How I'm getting the page numbers for the footers:

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

  }
}

Basically, I'd like the Table of Contents to fall on page 2, with a footer that says Page 2, all subsequent page numbers to be labeled correctly. How can I do this? I can clarify the question/code if needed.

Any help is appreciated :)

Was it helpful?

Solution

PageNo() is a bit funky when it comes to calculating pages. It outputs what page it is in order of creation, and since the ToC page is created as the very last it will show that as the last page.

So instead use

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

The rest of the code you provided, like the check if it is the first page, should work with PageNo().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top