2010-11-23 67 views
5

我想合併2 PDF,一個在我的服務器(不動態生成),一個合併之前生成,並沒有保存在服務器上的任何地方(我只是希望我的客戶端下載它)。所以我只有pdf的內容。兩種PDF都具有相同的格式(A4)。合併2 pdf與Zend框架

合併的文件將有2頁,並且不會保存在服務器上。因爲,我使用Zend框架,我寧願與它的解決方案(不能找到一個在線...)其他任何建議嗎?

(common solution found online but doesn't work)

編輯:因爲人是懶惰點擊。無論如何,代碼都在鏈接中,因爲它是錯誤的並且不起作用。

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

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

+0

澄清「不工作」 – Gordon 2010-11-23 08:57:31

+0

@Gordon:好,請閱讀鏈接的頁面> _> _I嘗試下面的腳本,但我得到的錯誤:未捕獲的異常「Zend_Pdf_Exception」有消息「頁面連接到一個文件,但在另一個環境中渲染_我知道我討厭_doesn't工作問題,但嚴重的是,在這裏,你只需要閱讀... – Shikiryu 2010-11-23 08:59:15

+2

謝謝澄清。只要儘量在你的問題中儘可能完整,而不是指責你要求懶惰的人。不把所有的信息放在首位都是懶惰的。另外,儘管用戶有錯誤,您是否嘗試過給出的代碼?我GOOGLE了錯誤信息和[這個特殊的錯誤似乎是固定在1.11](http://framework.zend.com/issues/browse/ZF-449) – Gordon 2010-11-23 09:12:55

回答

7

好的,在@Gordon對我的問題的評論中,我得到了一個解決方案。

  1. 您必須至少Zend Framework的1.11(我是在1.9,第一個錯誤)(發現由於對這個問題的第三個評論)
  2. 您必須要合併的PDF clone頁面,否則,您的應用程序將打印一個錯誤(自我解釋的錯誤)(發現感謝this slideshare這對Zend_Pdf非常有趣)
  3. 靜態PDF必須是PDF < = 1.4(我的是1.6)。 Zend_Pdf無法解析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內容。

0

您是否嘗試過使用PDF toolkit合併它們?

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

123.pdf將生成的PDF:

合併與其多個PDF可以利用來實現。您可以暫時將生成的PDF存儲在服務器上,將其發送到瀏覽器並在不再需要時將其刪除。

+0

感謝您的回答。不幸的是,我不能使用這個解決方案。 1-我無法在服務器上安裝任何東西(另一個人的工作,他真的很忙,不能在2周之前完成)2-不需要在服務器上保存就更好了。 – Shikiryu 2010-11-23 09:46:17

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

這就是我做到的。

希望這可以幫助大家。

TJ

1
$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

0

我在合併的PDF體驗:

  • Zend_Pdf是緩慢和效率不高的大合輯,
  • PDFTK寬鬆文檔的書籤和崩潰,如果文檔的尺寸大於文檔合併的更大,
  • pdflib非常快速並且保存書籤,對大型編譯非常有效。