Merge two or more pdf files in Zend Framework

Zend Framework Tool

It is possible to merge two or more pdf files into one file using Zend Class Library. Zend framework version 1.11 provides ability to merge different pdf files into one. We can merge different pdf files  just by adding few statements. The process of merging files includes:  creating  or loading pdf document.  Then creating clone of each page of the pdf document by using Zend_Pdf_Resource_Extractor.  After that appending the cloned page to an existing file. Then finally saving the file to which the cloned pages are appended.

The code provided will work only if you have all required classes in zend library. If you have older version of zend framework, you can update Zend Framework Standard Library with latest version.  The latest Zend Framework Library is freely available at:

http://framework.zend.com/download/latest

Just replaced the zend directory within library. The folder structure :

Zend Merge Tool for PDF

If you don’t want to replace complete zend directory. You can update only library/zend/pdf.php  file and library/zend/pdf directory with newer version. See the folder structure:

Update Zend Library

Step by step explanation of merging two files.

1. Create a new pdf document by creating Zend_Pdf Class object.

$pdfNew = new Zend_Pdf();

2. Load the existing PDF documents from files by using static method load().

$pdf1 = Zend_Pdf::load('path/to/your/folder/filename1.pdf');
$pdf2  = Zend_Pdf::load('path/to/your/folder/filename2.pdf');

3. Create Zend_Pdf_Resource_Extractor class object.

$extractor = new Zend_Pdf_Resource_Extractor();

4. Create clone of each page of pdf document using clonePage() method.

//Create clone of first page of first pdf document.
$pdfFirstPageFirst = $extractor->clonePage($pdf1->page[0]);
//Add first page of first pdf to new pdf document.
$pdfNew->pages[] = $pdfFirstPageFirst;
//Create clone of second page of first pdf document.
$pdfFirstPageSecond = $extractor->clonePage($pdf1->page[1]);
//Add second page of first pdf to new pdf document.
$pdfNew->pages[] = $ pdfFirstPageSecond;

And so on…

//Second Pdf
//Create clone of first page of second pdf document.
$pdfSecondPageFirst = $extractor->clonePage($pdf2->page[0]);
//Add first page of second pdf to new pdf document.
$pdfNew->pages[] = $pdfSecondPageFirst;
//Create clone of second  page of second  pdf document.
$pdfSecondPageSecond = $extractor->clonePage($pdf2->page[1]);
//Add second page of second pdf to new pdf document.
$pdfNew->pages[] = $ pdf SecondPageSecond;

And so on…

5. Save the new document to desired location.

$ pdfNew ->save('path/to/store/'.$filename);

This is the optimized code for merging any number of pdf files. Just add the files need to be merged to the array. And specify the location where the merged file should be stored.

// Array of the pdf files need to be merged
$pdfDocs = array('path/to/your/folder/filename1.pdf',  'path/to/your/folder/filename2.pdf',  'path/to/your/folder/filename3.pdf');
$pdfNew = new Zend_Pdf();
foreach($pdfDocs as $file){
$pdf = Zend_Pdf::load($file);
$extractor = new Zend_Pdf_Resource_Extractor();
foreach($pdf->pages as $page){
$pdfExtract = $extractor->clonePage($page);
$pdfNew->pages[] = $pdfExtract;
}
}
}
$mergePdf ="mergefile.pdf";
$pdfNew ->save('path/to/store/'.$mergePdf);

We hope that will find a use for this tool. It is OpenSource and Free. Share it and comment it. We would be happy to improve it with the participation of the Zend Framework OpenSource community.

Shilpa
Lead Developer E-nova Technologies

Related Posts:

Tags: , ,

Leave a Reply

CommentLuv Enabled

Notify me of followup comments via e-mail. You can also subscribe without commenting.