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

☆樱花仙子☆ 提交于 2019-12-25 16:59:27

问题


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..


回答1:


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



回答2:


TCPDF Bookmark:

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

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



来源:https://stackoverflow.com/questions/23484605/tcpdf-how-do-you-make-a-link-to-the-table-of-contents-toc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!