Zend Pdf merge two PDF in PHP

With the 1.11 release of the Zend Framework, we are finally able to merge different PDF documents into one. Here is how to proceed to fusion PDF1 and PDF2:

1) Create a PDF by loading PDF1

$pdf1 = Zend_Pdf::load(pdf1.pdf'); 	

2) Create a second PDF object with PDF2 in it

$pdf2 = Zend_Pdf::load(pdf2.pdf'); 

3) Load PDF Resource Extractor

$extractor = new Zend_Pdf_Resource_Extractor();

4) Use PDF Resource Extractor to extract the info from PDF2 (only page per page, here page 1)

$pdf2_content = $extractor->clonePage($pdf2->pages[0]);

5) Add PDF2 (page 1) into PDF1

$pdf1->pages[] = $pdf2_content;

That’s it! You now have PDF2 in PDF1. If PDF2 has more than one page, just repeat step 4) and 5) for ever page.
An example is also shown here (under page cloning): http://framework.zend.com/manual/en/zend.pdf.pages.html


Leave a Reply