Zend Framework PDF问题 [英] Zend Framework PDF problems
问题描述
又是我,我有一个小问题:
It's me again guys, I have a small problem:
// Create new PDF
$pdf = new Zend_Pdf();
// Add new page to the document
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;
// Set font
$page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 20);
// Draw text
$page->drawText('Hello world!', 100, 510);
$this->getResponse()
->setHeader('Content-Disposition', 'attachment; filename=result.pdf')
->setHeader('Content-type', 'application/x-pdf');
echo $pdf->render();
当我下载文件并尝试打开它时,出现错误,听起来像这样:
When I download the file, and try to open it, i'm getting an error, which sounds like this:
格式错误:没有PDF或已损坏
format error: not a PDF or corrupted
我的问题是:我做错了什么?
推荐答案
如果尝试使用文本编辑器(或十六进制编辑器)打开文件,会得到什么?
If you try to open the file with a text editor (or an hexadecimal editor), what do you get ?
您的PDF文件必须仅包含PDF数据,并且开头或结尾不得包含任何HTML或空白.
Your PDF file must contain only PDF data, and not any HTML or blank space at the beginning or the end.
可能引起麻烦的一件事是Zend Framework自动渲染View.
在操作开始时使用类似方法可能会有所帮助:
One thing that could cause troubles is the automatic rendering of a View by Zend Framework.
Using something like this at the beginning of your action might help :
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
我看了我写的某个动作示例,这是我看到的与您所做的唯一的区别,实际上...
I've looked at a sample I wrote sometime action, and it's the only difference I see with what you did, actually...
如果仍然无法使用,如果您尝试将PDF保存到文件中而不是将其发送给用户该怎么办?例如:
If it still doesn't work, what if you try saving the PDF to a file instead of sending it to the user ? With something like that, for instance :
$pdf->save(CACHE_DIR . '/test-pdf.pdf');
我知道这不是你想做的;但是它可以让您检查PDF是否生成正确,以确认问题是出在PDF生成还是在输出方面.
I know it's not what you want to do ; but it would allow you to check if the PDF is well-generated, to identify if the problem is with the PDF generation, or with it's output.
这是我正在谈论的完整示例:
And here is the full example I was talking about :
public function pdfAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$pdf = new Zend_Pdf();
$pdf->properties['Title'] = "TITLE";
$pdf->properties['Author'] = "AUTHOR";
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$width = $page->getWidth(); // A4 : 595
$height = $page->getHeight(); // A4 : 842
$imagePath = WEB_DIR . '/images/logo.png';
$image = Zend_Pdf_Image::imageWithPath($imagePath);
$x = 15;
$y = $height - 15 - 106/2;
$page->drawImage($image, $x, $y, $x+155/2, $y+106/2);
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font, 36);
$page->drawText('Hello world!', 72, 720, 'UTF-8');
$pdf->pages[] = $page;
$this->getResponse()->setHeader('Content-type', 'application/x-pdf', true);
$this->getResponse()->setHeader('Content-disposition', 'inline; filename=my-file.pdf', true);
$this->getResponse()->setBody($pdf->render());
}
据我所记得,这在几个月前还不错;和您的代码的唯一区别是:
As far as I remember, this was working fine a couple of months ago ; and the only differences with your code are :
- 禁用布局/渲染
- 使用徽标;但这没什么大不同
希望这可以帮助 ;玩得开心!
Hope this helps ; Have fun!
这篇关于Zend Framework PDF问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!