Merging 2 pdf with Zend Framework

后端 未结 5 1893
一整个雨季
一整个雨季 2020-12-06 07:44

I\'m trying to merge 2 PDF, 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

相关标签:
5条回答
  • 2020-12-06 07:52
    $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;
    

    Right from the horses mouth.

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

    0 讨论(0)
  • 2020-12-06 07:53

    Alright, with the guide from @Gordon 's comment in my question, I got a solution.

    1. You must have at least Zend Framework 1.11 (I was in 1.9, first error) (found thanks to the 3rd comment to this question)
    2. You must clone page from the PDF you want to merge, else, your application will print an error (self explanatory one) (found thanks to this slideshare which is very interesting for Zend_Pdf)
    3. The static PDF must be a PDF <= 1.4 (mine was 1.6). Zend_Pdf can't parse PDF which version is > 1.4

    I used this application to convert the static files I had in version 1.6 to 1.4.

    Here's the rough code I have and work (I know it's not optimised, I'll do it later; but still, it can 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 sending the PDF content to browser with the title as... title. $pdf2show->render() produces the merged PDF content as a string.

    0 讨论(0)
  • 2020-12-06 07:53
    $extractor = new Zend_Pdf_Resource_Extractor();
    $clone_page_to_use_in_any_pdf = $extractor->clonePage($original_pdf->pages[0]);
    

    This is how I did it.

    Hope this helps everyone.

    TJ

    0 讨论(0)
  • 2020-12-06 08:06

    My experience in merging pdfs :

    • Zend_Pdf is slow and not efficient for large compilations,
    • pdftk loose document's bookmarks and crashed if document's size is larger than document merged,
    • pdflib is really fast and preserve bookmarks, is efficient for large compilations.
    0 讨论(0)
  • 2020-12-06 08:06

    Have you tried merging them using the PDF toolkit?

    Merging multiple PDFs with it can be achieved using :

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

    123.pdf will be the resulting PDF. You can temporarily store the resulting PDF on the server, send it to the browser and remove it when it's no longer needed.

    0 讨论(0)
提交回复
热议问题