2011-06-20 23 views
2

我在Zend_Pdf中爲我的pdf文檔創建模板時遇到問題。我遵循http://framework.zend.com/manual/en/zend.pdf.pages.html提供的指南,但它似乎不工作,因爲在第一頁以外的任何頁面上都不顯示頁腳文本或徽標。Zend_Pdf和模板化

這是我的代碼示例:(注意,該MTP不變的是剛剛從毫米到重新計算分)

//Create pdf object 
$pdf = new Zend_Pdf(); 

//Create the first page 
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); 

/*HEADER*/ 
//insert logo at the top 
$logo = Zend_Pdf_Image::imageWithPath(APPLICATION_PATH . '/../document_root/images/logotype.png'); 
$page->drawImage($logo, 22*MTP,274*MTP, 62*MTP, 289*MTP); 

/*FOOTER*/ 
$footerStyle->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 10); 
$page->setStyle($footerStyle); 
$page->drawText('Footer text', 33*MTP, 20*MTP, 'ISO-8859-1'); 

//save the page to pdf->pages 
$pdf->pages[0] = $page; 

//save the page as a template 
$template = $pdf->pages[0]; 

//create the next page from template 
$page1 = new Zend_Pdf_Page($template); 

//save the next page to pdf->pages 
$pdf->pages[] = $page1; 

難道我完全誤解了如何在Zend_Pdf這個「模板函數」的工作?我知道Zend_Pdf缺乏與某些外部pdf生成器(如FPDF)相比較的許多功能,但是仍然必須有某種方法爲所有頁面創建基本頁眉/頁腳?

回答

0

嘗試使用clone而不是將頁面作爲參數傳遞到新頁面。

$page1 = clone $template; 

或者乾脆

$pdf->pages[] = clone $pdf->pages[0]; 
+0

遺憾的是,不能正常工作。我收到一條錯誤消息,說「消息:不支持使用'clone'關鍵字克隆Zend_Pdf_Page對象。使用'new Zend_Pdf_Page($ srcPage)'語法」。 我已經做了一個解決方法,我已經實現了一個新的「newPage」函數。每次我調用這個函數,它會創建一個頁面,然後調用兩個新函數(generatePdfHeader和generatePdfFooter)來生成頁眉和頁腳。返回結果頁面,然後我可以繼續使用常規的Zend_Pdf函數。 – Lobo

+0

問題是 - **你使用什麼版本的ZF?**。我剛剛在1.11.6上進行了測試,以確保100%的可靠性,並且在這個版本的頁面上進行了克隆,沒有任何問題。 –

+0

我使用1.10,這可能是問題所在。不幸的是,我現在沒有時間升級和重做pdf功能,但是在測試版發佈後還有時間專門針對bug修復,重新分解和精簡。在那個階段,我肯定會升級並再次測試。感謝您的幫助 – Lobo