将 2 pdf 与 Zend Framework 合并

2021-12-27 00:00:00 merge pdf php zend-framework

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

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 to download it). So I only have the pdf's content. Both PDF have the same format (A4).

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

The merged file will have 2 pages and won't be saved on server as well.

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

Since, I'm using Zend Framework, I would prefer a solution with it (can't find one online...) else any advice ?

(网上找到的常见解决方案,但不起作用)

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

Edit : because people are lazy to click. The code is in the link anyway since it's wrong and doesn't work.

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

I try the script below, but I get the error:

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

Uncaught exception 'Zend_Pdf_Exception' with message 'Page is attached to one documen, but rendered in context of another

推荐答案

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

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

  1. 您必须至少拥有 Zend Framework 1.11(我使用的是 1.9,第一个错误)(感谢对这个问题的第 3 条评论)
  2. 您必须从要合并的 PDF 中clone 页面,否则,您的应用程序将打印一个错误(不言自明)(感谢 this slideshare 这对 Zend_Pdf 非常有趣)
  3. 静态 PDF 必须是 PDF <= 1.4(我的是 1.6).Zend_Pdf 无法解析 PDF 版本 > 1.4
  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

我使用这个应用程序来转换我在 1.6 版中的静态文件到 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 是将 PDF 内容发送到浏览器的函数,title 作为...标题.$pdf2show->render() 将合并的 PDF 内容生成为字符串.

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.

相关文章