Skip to content
Advertisement

Merge pdf files in PHP and keep weblinks inside

We have a project where we merge different pdfs to create a catalog. Right now it’s running on myokyawhtun/pdfmerger, which runs fine, but it does not keep links set in acrobat.

We have tried different libraries we found (pure PHP, we cannot install or call applications from the command line via shell-exec or similar on this webspace, so no gs), even if we just import the pdf-files via fpdi and resave them, the hyperlinks get lost.

Is there any (pure PHP) library out there which can retain links inside the files? Or are there some special settings that we missed?

We have tried:

  • setasign/fpdi
  • iio/libmergepdf
  • jurosh/pdf-merge

Example code for the current lib (myokyawhtun/pdfmerger):

require('vendor/myokyawhtun/pdfmerger/tcpdf/tcpdf.php');
require('vendor/myokyawhtun/pdfmerger/tcpdf/tcpdi.php');
require('vendor/myokyawhtun/pdfmerger/PDFMerger.php');

$pdf = new PDFMergerPDFMerger;

foreach($sourcePdfs as $file)
{
    $pdf->addPDF($pdfDir.'/source/'.$file);
}

$pdf->merge('download', 'Download.pdf');

Advertisement

Answer

All the mentioned libraries use FPDI under the hood, which simply does not support content outside of a pages content stream, such as links or any other annotation type.

We (author of FPDI) also offer non-free products which work on another level and which allow you keep all annotations including links and also forms when you concatenate the documents. This is possible with the SetaPDF-Merger component:

$merger = new SetaPDF_Merger();

foreach($sourcePdfs as $file) {
    $merger->addFile($pdfDir . '/source/' . $file);
}

$merger->merge();

$document = $merger->getDocument();
$document->setWriter(new SetaPDF_Core_Writer_Http('Download.pdf'));
$document->save()->finish();
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement