問題描述
我正在嘗試合并 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.
- 您必須至少擁有 Zend Framework 1.11(我使用的是 1.9,第一個錯誤)(感謝對這個問題的第 3 條評論)
- 您必須從要合并的 PDF 中
clone
頁面,否則,您的應用程序將打印一個錯誤(不言自明)(感謝 this slideshare 這對 Zend_Pdf 非常有趣) - 靜態 PDF 必須是 PDF <= 1.4(我的是 1.6).Zend_Pdf 無法解析 PDF 版本 > 1.4
- 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)
- 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) - 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.
這篇關于將 2 pdf 與 Zend Framework 合并的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!