代码之家  ›  专栏  ›  技术社区  ›  Shikiryu DhruvJoshi

将2pdf与Zend框架合并

  •  5
  • Shikiryu DhruvJoshi  · 技术社区  · 14 年前

    我正在尝试合并2个PDF文件,一个在我的服务器上(不是动态生成的),一个在合并之前生成的,没有保存在服务器上的任何地方(我只是希望我的客户端下载它)。所以我只有pdf的内容。两种PDF格式相同(A4)。

    合并的文件将有两页,并且不会保存在服务器上。

    因为,我使用的是Zend框架,所以我更喜欢使用它的解决方案(在网上找不到…)还有什么建议吗?

    (common solution found online but doesn't work)

    编辑:因为人们懒得点击。无论如何,代码都在链接中,因为它是错误的,不起作用。

    我试了下面的剧本,但是我得到了 错误:

    意外异常 “Zend_Pdf_Exception”和消息 '页附加到一个文档,但是 在另一个上下文中呈现

    5 回复  |  直到 14 年前
        1
  •  9
  •   Shikiryu DhruvJoshi    14 年前

    好吧,根据@Gordon在我问题中的评论,我找到了一个解决方案。

    1. 您必须至少有Zend Framework 1.11(我在1.9中,第一个错误)(由于对这个问题的第三个注释而找到)
    2. 你必须 clone 要合并的PDF页面,否则应用程序将打印错误(不言而喻)(由于 this slideshare 这对Zend_Pdf来说非常有趣)
    3. 静态PDF必须是PDF<=1.4(我的是1.6)。Zend_Pdf无法分析哪个版本是1.4

    我用过 this application 将我在1.6版中的静态文件转换为1.4版。

    下面是我所拥有和工作的粗略代码(我知道它没有优化,我稍后会做;但是,它仍然是有用的)

    $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 是一个将PDF内容发送到浏览器的函数 title 作为。。。头衔。 $pdf2show->render() 以字符串形式生成合并的PDF内容。

        2
  •  4
  •   Tyler Jensen    14 年前
    $extractor = new Zend_Pdf_Resource_Extractor();
    $clone_page_to_use_in_any_pdf = $extractor->clonePage($original_pdf->pages[0]);
    

    我就是这样做的。

    希望这对大家都有帮助。

    TJ公司

        3
  •  2
  •   Tyler Jensen    14 年前
    $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;
    

    就在马嘴里。

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

        4
  •  0
  •   Sébastien Ballesté-Antich    13 年前

    我在合并PDF方面的经验:

    • Zend_Pdf速度慢,对于大型编译来说效率不高,
    • 如果文档的大小大于文档合并的大小,则pdftk会丢失文档的书签并崩溃,
    • pdflib真的很快并且保存书签,对于大型编译是有效的。
        5
  •  -1
  •   wimvds    14 年前

    你试过用 PDF toolkit ?

    可以使用以下方法将多个PDF与其合并:

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

    123.pdf将是生成的pdf。您可以将生成的PDF临时存储在服务器上,将其发送到浏览器,并在不再需要时将其删除。