简体   繁体   中英

TCPDF How do you make a link to the Table of Contents (TOC)?

Can anyone demonstrate a basic example that adds a link on an arbitrary page in the pdf that takes the user back to the table of contents?

I have everything working fine, but can't get this simple requirement going.

Some background:

You'd think this would be trivial, everything about TCPDF seems to function so well, maybe a bad day I'm having. I have tried the following:

From the documentation there seems to be 2 options for creating internal document links

  • Via methods addlink() and setlink() to specify a target page.
  • Via an html anchor tag rendered in the pdf (where you specify the href attribute as #15 for page 15 for eg.)

These methods basically work just fine.

A quote from the author -

The Table Of Content page is created on the current page and then moved to the specified page...To create a link to the TOC page you have to set a link to the last page (you must know the total number of pages before).

I Understand this, I create the TOC last (using the supplied methods), and make a link to this last page, but it is unclickable (not a link in the rendered doc). I therefore have to interpret the quote 'you must know the total number of pages before' as meaning TCPDF must know the number of pages! Clearly a big difference, meaning for most practical purposes the answer is no, not by this method (maybe one link on the last page!)

Finally, the documentation for the addTOC method mentions the $toc_name argument:

TCPDF::addTOC   (       
 $page = '',
 $numbersfont = '',
 $filler = '.',
 $toc_name = 'TOC',
 $style = '',
}   

$toc_name (string) name to use for TOC bookmark.

Unfortunately I can't see anyway to use this name, no docs, help or examples anywhere.

Someone.. please tell me I'm being silly..

It may be late, but this is what I use (my table of contents is on page 2):

$pdf->addTOCPage();             
$link = $pdf->AddLink();
$pdf->SetLink($link, 0, '*2');
$pdf->addTOC(2, 'courier', '.', 'INDEX', 'R', array(128,0,0));
$pdf->endTOCPage();

And then at where ever I want to link back to the TOC in the document, I do the following:

$html = '<a href="#*2" style="color:blue;">Return to TOC</a>';
$pdf->writeHTML($html, true, false, true, false, 'R');

TCPDF Bookmark:

 $bookmark = "Title of my Bookmark";     
 $pdf->Bookmark($bookmark);

This is your best bet for adding to the PDF Bookmarks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM