Reputation: 355
I would like to add nameddests to locations of an existing PDF which are specified by some string (say: put a nameddest on the first occurence of the string "Chapter 1"). Then I would like to be able to jump to those nameddests using JS events.
What I have achieved so far using PHP and FPDF/FPDI: I can load an existing PDF using FPDI and add nameddests to arbitrary positions using a slightly modified version of [1]. I can then embed the PDF in an iframe and navigate to the nameddests using, for example, JS buttons.
However, so far I need to find out the positions of the nameddests by hand. How can I search the PDF for strings and get the page numbers and positions of the search results such that I can add nameddests there?
[1] http://www.fpdf.org/en/script/script99.php
Upvotes: 0
Views: 2876
Reputation: 5058
It is impossible to analyse the content of a PDF document with FPDI.
We (Setasign - author of FPDI and PDF_NamedDestinations) have a product (not free) available which allows you to do handle this task: The SetaPDF-Extractor component.
A simple POC of your project could look like:
<?php
// load and register the autoload function
require_once('library/SetaPDF/Autoload.php');
$writer = new SetaPDF_Core_Writer_Http('result.pdf', true);
$document = SetaPDF_Core_Document::loadByFilename('file/with/chapters.pdf', $writer);
$extractor = new SetaPDF_Extractor($document);
// define the word strategy
$strategy = new SetaPDF_Extractor_Strategy_Word();
$extractor->setStrategy($strategy);
// get the pages helper
$pages = $document->getCatalog()->getPages();
// get access to the named destination tree
$names = $document
->getCatalog()
->getNames()
->getTree(SetaPDF_Core_Document_Catalog_Names::DESTS, true);
for ($pageNo = 1; $pageNo <= $pages->count(); $pageNo++) {
/**
* @var SetaPDF_Extractor_Result_Word[] $words
*/
$words = $extractor->getResultByPageNumber($pageNo);
// iterate over all found words and search for "Chapter" followed by a numeric string...
foreach ($words AS $word) {
$string = $word->getString();
if ($string === 'Chapter') {
$chapter = $word;
continue;
}
if (null === $chapter) {
continue;
}
// is the next word a numeric string
if (is_numeric($word->getString())) {
// get the coordinates of the word
$bounds = $word->getBounds()[0];
// create a destination
$destination = SetaPDF_Core_Document_Destination::createByPageNo(
$document,
$pageNo,
SetaPDF_Core_Document_Destination::FIT_MODE_FIT_BH,
$bounds->getUl()->getY()
);
// create a name (shall be unique)
$name = strtolower($chapter . $word->getString());
try {
// add the named destination to the name tree
$names->add($name, $destination->getPdfValue());
} catch (SetaPDF_Core_DataStructure_Tree_KeyAlreadyExistsException $e) {
// handle this exception
}
}
$chapter = null;
}
}
// save and finish the resulting document
$document->save()->finish();
You can then access the named destinations through the URL this way (the viewer application and browser plugin need to support this):
http://www.example.com/script.php#chapter1
http://www.example.com/script.php#chapter2
http://www.example.com/script.php#chapter10
...
Upvotes: 1