簡體   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