【问题标题】:Merging 2 pdf with Zend Framework将 2 pdf 与 Zend 框架合并
【发布时间】:2011-05-14 08:15:20
【问题描述】:

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

合并后的文件将有 2 页,也不会保存在服务器上。

由于我使用的是 Zend Framework,我更喜欢它的解决方案(在网上找不到...)还有什么建议吗?

(common solution found online but doesn't work)

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

我尝试了下面的脚本,但我得到了 错误:

未捕获的异常 带有消息的“Zend_Pdf_Exception” '页面附加到一个文档,但是 在另一个上下文中呈现

【问题讨论】:

  • @Gordon : 好吧,阅读链接上的页面 >_> 我尝试下面的脚本,但我收到错误:未捕获的异常 'Zend_Pdf_Exception' 和消息 'Page is attach to one文档,但在另一个上下文中呈现 我知道我讨厌 不起作用 问题,但说真的,在这里,您只需阅读...
  • 感谢您的澄清。尽量让你的问题尽可能完整,而不是指责那些你寻求帮助的人懒惰。一开始就不把所有信息都放在那里是懒惰的。此外,尽管用户有错误,您是否尝试过那里给出的代码?我用谷歌搜索了错误消息和this particular error seems to be fixed in 1.11
  • @Gordon :我不应该首先提及它,因为它是一个错误(是的,我尝试了我在网上找到的所有内容;))我只是展示,我进行了研究和测试。只是为了表明,我来这里并不是为了一个预先消化的答案,就像现在一群帅哥所做的那样。我现在正在尝试 1.11 以查看它是否有效,以随时通知您。我现在是 1.9。

标签: php zend-framework pdf merge


【解决方案1】:

我在合并 pdf 方面的经验:

  • Zend_Pdf 速度慢,对于大型编译效率不高,
  • pdftk 松散文档的书签并在文档大小大于合并文档时崩溃,
  • pdflib 速度非常快并且可以保留书签,对于大型编译非常有效。

【讨论】:

    【解决方案2】:
    $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

    【讨论】:

    • 对,但是您的示例没有回答问题;)它从 same pdf $pdf 中提取页面。
    【解决方案3】:
    $extractor = new Zend_Pdf_Resource_Extractor();
    $clone_page_to_use_in_any_pdf = $extractor->clonePage($original_pdf->pages[0]);
    

    我就是这样做的。

    希望对大家有所帮助。

    TJ

    【讨论】:

    • 在我看来,这是最好的解决方案!
    【解决方案4】:

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

    1. 您必须至少拥有 Zend Framework 1.11(我在 1.9,第一个错误)(感谢对这个问题的第三条评论)
    2. 您必须从要合并的 PDF 中找到clone 页面,否则,您的应用程序将打印一个错误(自我解释错误)(感谢this slideshare 这对 Zend_Pdf 来说非常有趣)
    3. 静态 PDF 必须是 PDF

    我使用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 内容生成为字符串。

    【讨论】:

    • 看,一个解决方案 :) 只是因为你告诉我错误信息才找到 ;)
    【解决方案5】:

    您是否尝试过使用PDF toolkit 合并它们?

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

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

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

    【讨论】:

    • 感谢您的回答。不幸的是,我不能使用这个解决方案。 1- 我无法在服务器上安装任何东西(另一个人的工作,他真的很忙,2 周前无法完成) 2- 不必在服务器上保存会更好。
    猜你喜欢
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多