繁体   English   中英

将2 pdf与Zend Framework合并

[英]Merging 2 pdf with Zend Framework

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

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

既然,我正在使用Zend Framework,我更喜欢使用它的解决方案(在网上找不到一个......)其他任何建议?

(在线发现常见解决方案但不起作用)

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

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

未捕获的异常'Zend_Pdf_Exception',消息'Page附加到一个文档,但在另一个文档的上下文中呈现

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

  1. 您必须至少拥有Zend Framework 1.11(我在1.9,第一次出错)(感谢此问题的第3条评论)
  2. 您必须从要合并的PDF中clone页面,否则,您的应用程序将打印错误(自解释)(感谢此幻灯片共享 ,这对Zend_Pdf非常有用)
  3. 静态PDF必须是PDF <= 1.4(我的是1.6)。 Zend_Pdf无法解析PDF版本> 1.4

我使用此应用程序将我在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内容生成为字符串。

$extractor = new Zend_Pdf_Resource_Extractor();
$clone_page_to_use_in_any_pdf = $extractor->clonePage($original_pdf->pages[0]);

这就是我做到的。

希望这有助于每个人。

TJ

$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

我合并pdf的经验:

  • Zend_Pdf很慢,对于大型编译来说效率不高,
  • pdftk松散文档的书签,如果文档的大小大于合并的文档,则会崩溃,
  • pdflib非常快且保留书签,对于大型编辑非常有效。

您是否尝试使用PDF工具包合并它们?

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM