【问题标题】:Zend_Pdf and templatedZend_Pdf 和模板化
【发布时间】:2011-06-20 22:36:25
【问题描述】:

我在 Zend_Pdf 中为我的 pdf 文档创建模板时遇到问题。我已按照http://framework.zend.com/manual/en/zend.pdf.pages.html 提供的指南进行操作,但它似乎不起作用,因为除了第一页之外的任何页面都看不到页脚文本或徽标。

这是我的代码示例:(注意;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 中是如何工作的?我知道与某些外部 pdf 生成器(如 FPDF)相比,Zend_Pdf 缺少相当多的功能,但仍然必须有某种方法为所有页面制作基本的页眉/页脚,对吗?

【问题讨论】:

    标签: zend-framework pdf pdf-generation zend-pdf


    【解决方案1】:

    尝试使用clone,而不是将页面作为参数传递给新页面。

    $page1 = clone $template;
    

    或者干脆

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

    【讨论】:

    • 不幸的是,这也不起作用。我收到一条错误消息,上面写着“消息:不支持使用 'clone' 关键字克隆 Zend_Pdf_Page 对象。使用 'new Zend_Pdf_Page($srcPage)' 语法”。我已经完成了一个解决方法,我已经实现了一个新的“newPage”函数。每次我调用此函数时,它都会创建一个页面,然后调用两个新函数(generatePdfHeader 和 generatePdfFooter)来生成页眉和页脚。返回结果页面,然后我可以继续使用常规 Zend_Pdf 函数。
    • 问题是 - 您使用什么版本的 ZF?。我刚刚在 1.11.6 上对其进行了测试,只是为了 100% 确定,并且使用这个版本的页面被克隆没有任何问题。
    • 我使用的是 1.10,所以这可能是问题所在。不幸的是,我现在没有时间升级和重做 pdf 功能,但是在 beta 版本发布后有时间专门用于错误修复、重构和精简。在那个阶段,我肯定会升级并再次测试它。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    相关资源
    最近更新 更多