Combine 2 pdf with Zend Framework

I am trying to combine 2 PDFs, one on my server (not dynamically generated) and one generated just before the merge and not saved anywhere on the server (I just want my client to download it). So I only have pdf content. Both PDFs have the same format (A4).

The combined file will have 2 pages and will not be saved on the server.

Since I am using Zend Framework, would I prefer a solution with it (can't find one online ...) any more tips?

(general solution found on the Internet but not working)

Edit: people are lazy to click. The code is still in the link because it is erroneous and does not work.

I will try the script below, but I get Error:

Unhandled 'Zend_Pdf_Exception' exception with the message Page attached to one document, but displayed in the context of another

+5
merge php pdf zend-framework
Nov 23 '10 at 8:52
source share
5 answers

Ok, with a guide from the comment by @Gordon in my question, I got a solution.

  • You should have at least Zend Framework 1.11 (I was in 1.9, the first error) (found thanks to the 3rd comment on this question)
  • You must clone page from the PDF file you want to combine, otherwise your application will print an error message (self-evident) (found thanks to this slideshare , which is very interesting for Zend_Pdf)
  • Static PDF should be PDF <= 1.4 (mine was 1.6). Zend_Pdf cannot parse PDF version> 1.4

I used this application to convert static files that I had in versions 1.6 to 1.4.

Here is the crude code that I have and works (I know that it is not optimized, I will do it later, but still, it may be useful)

 $pdf2show = new Zend_Pdf(); // Initializing the merged PDF $pdf1 = Zend_Pdf::parse($pdfContent, 1); // $pdfContent is the generated one, got the content... $template = clone $pdf1->pages[0]; // cloning the page (a must do) $page1 = new Zend_Pdf_Page($template); // Creating the first page of the merged PDF with the previous content $pdf2show->pages[] = $page1; // Adding this page to the final PDF $pdf2 = Zend_Pdf::load('urlToYourPDF.pdf'); // Loading the statif PDF $template2 = clone $pdf2->pages[0]; // cloning the page (a must do) $page2 = new Zend_Pdf_Page($template2); // Creating the second page of the merged PDF with the previous content $pdf2show->pages[] = $page2; // Adding this page to the final PDF sendToWebBrowser('title', $pdf2show->render()); 

sendToWebBrowser is a function to send PDF content to a browser with the title title as .... $pdf2show->render() creates a merged PDF file as a string.

+7
Nov 23 '10 at 13:44
source share
 $extractor = new Zend_Pdf_Resource_Extractor(); $clone_page_to_use_in_any_pdf = $extractor->clonePage($original_pdf->pages[0]); 

Here is how I did it.

Hope this helps everyone.

t

+3
Mar 10 2018-11-11T00:
source share
 $extractor = new Zend_Pdf_Resource_Extractor(); $page1 = $extractor->clonePage($pdf->pages[$templatePageIndex1]); $page2 = $extractor->clonePage($pdf->pages[$templatePageIndex2]); $page1->drawText('Some text...', $x, $y); $page2->drawText('Another text...', $x, $y); $pdf = new Zend_Pdf(); $pdf->pages[] = $page1; $pdf->pages[] = $page2; 

Straight from the mouth of horses.

http://framework.zend.com/manual/en/zend.pdf.pages.html#zend.pdf.pages.cloning

+1
Mar 10 2018-11-11T00:
source share

Did you try to merge them using the PDF toolkit ?

Merging several PDF files with it can be achieved using:

 pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdf 

123.pdf will be received PDF. You can temporarily save the received PDF file on the server, send it to the browser and delete it when it is no longer needed.

0
Nov 23 '10 at 9:35
source share

My experience of combining PDF files:

  • Zend_Pdf is slow and inefficient for large compilations,
  • pdftk frees up bookmarks of documents and breaks if the document is larger than the combined documents,
  • pdflib is really fast and saves bookmarks, effective for large compilations.
0
May 30 '12 at 22:21
source share



All Articles