I am using TCPDF in PHP to create an e-book style PDF. It consists of a two pages for the introduction, a TOC page added at the bottom of the code using the addTOC() function and multiple chapters of HTML text and images. For each chapter, I add a bookmark. The bookmark, as used in the TOC, references the correct position.
In addition, I need to create a link to a previously added chapter inside the HTML text. For this reason, after adding the bookmark I immediately add a link to the current position using the AddLink() and SetLink() functions and write the links to an array.
$bookmarkid=$pdf->Bookmark($chno." ".$chapter["title"], 0, 0, '', 'B', array(0,64,128));
$targetlink=$pdf->AddLink();
$pdf->SetLink($targetlink,$pdf->getY(),$pdf->PageNo());
$pdf->Link($targetlink,0,$bookmarkid,$pdf->getY());
$chanker[$chno]=$targetlink;
This way, I can replace my temporary anchors in the HTML and insert the correct link returned by the AddLink() function.
foreach($chanker as $no=>$anker){
$text=str_replace("#ch_$no","#$anker",$text);
}
All this said, these internal links are clickable, but lead to some completely wrong positions (e.g. not a 1 page offset, but seemingly random).
What reasons could there be for this behavior? Is there some better way to add these internal links?
Edit: added code
Edit 2: modified code, it was the wrong version