Today I worked on a Zend Framework application, I had to merge a dynamically generated PDF-File with a stored PDF-FIle. The idea was to clone all pages of all pdf documents to a new pdf document. The best way to do that is with the Zend_Pdf module. It’s obvious since the application is already in the Zend Framework environment. The big advantage is Zend_Pdf objects can load a PDF from a string and otherwise it can load stored PDF-files.

So i extended my existing renderPDF() method with the following snippet:

$pdfNew = new Zend_Pdf();
$extractor = new Zend_Pdf_Resource_Extractor();
 
//load pdf 1 from a string
$pdf1 = Zend_Pdf::parse(parent::renderPDF());
//Clone pages of first pdf document and add the pages to the new pdf object
foreach ($pdf1->pages as $p) $pdfNew->pages[] = $extractor->clonePage($p);
 
//load pdf 2 from a file
$pdf2  = Zend_Pdf::load('/path/to/your.pdf');
//Clone pages of second pdf document and add the pages to the new pdf object
foreach ($pdf2->pages as $p) $pdfNew->pages[] = $extractor->clonePage($p);
 
//here you can add more pdf's
...
 
//output new pdf as a string
return $pdfNew->render();

Here are some alternative possibilities which i found prior to Zend_Pdf: