由于 html 内容的存在,Zend_pdf 文档在 magento 中引发错误
在 magento1.7 中,只需在 appcodecoreMageWishlistHelperData.php (Data.php) 文件中写入以下几行
公共函数 getpdf(){$pdf = 新 Zend_Pdf();$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);for($i=0; $i<5; $i++) {$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);$page->setFont($font, 24) ->drawText('Hello World page '.$i, 72, 720);$pdf->pages[] = $page;}$pdfString = $pdf->render();header("内容处理:附件;文件名=myfile.pdf");header("内容类型:应用程序/x-pdf");回声 $pdfString;}
调用这个函数
<?php Mage::helper('wishlist')->getpdf();?>
在 appdesignfrontendasedefault emplatewishlistview.phtml (view.phtml)文件中,就在
$pdfString = $pdf->render();header("内容处理:附件;文件名=myfile.pdf");header("内容类型:应用程序/x-pdf");回声 $pdfString;
与
$pdf->save("mydoc.pdf");
然后在 magento1.7 文件夹中打开 mydoc.pdf,它打开没有错误并在 Pdf 文档的每一页上显示Hello World".现在在文本编辑器中比较这两个文件myfile.pdf"和mydoc.pdf",您将看到myfile.pdf"中存在 html 内容.我的问题是我无法找到从生成的 PDF 文档中删除这些 html 内容的方法.任何人都可以帮助我解决这个问题.请注意,上面的代码只是我所做的演示.
解决方案给猫剥皮的方法不止一种.我认为你想要一个不错的愿望者 PDF.考虑使用 wkhtml2pdf 来制作它.这将涉及对页面的 PRINT 版本进行样式设置 - 如果 media=print,则会为此添加一个 CSS,wkhtml2pdf 就是这种情况.
wkhtml2pdf 的结果比使用 Zend PDF 内容手绘"的任何东西都要好得多.
要开始安装 wkhtml2pdf 并只需在命令行上运行它,修改选项直到它出现一半.
wkhtml2pdf 的唯一缺点是您无法控制分页符.
In magento1.7, just write following lines in appcodecoreMageWishlistHelperData.php (Data.php) file
public function getpdf()
{
$pdf = new Zend_Pdf();
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
for($i=0; $i<5; $i++) {
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$page->setFont($font, 24) ->drawText('Hello World page '.$i, 72, 720);
$pdf->pages[] = $page;
}
$pdfString = $pdf->render();
header("Content-Disposition: attachment; filename=myfile.pdf");
header("Content-type: application/x-pdf");
echo $pdfString;
}
Call this function
<?php Mage::helper('wishlist')->getpdf();?>
in appdesignfrontendasedefault emplatewishlistview.phtml (view.phtml)file just before <div class="my-wishlist">
line. Now run magento in browser and login. Then click on My wishlist link and it will show myfile.pdf in downlods folder. When it is opened it throws error message.Now replace these lines
$pdfString = $pdf->render();
header("Content-Disposition: attachment; filename=myfile.pdf");
header("Content-type: application/x-pdf");
echo $pdfString;
with
$pdf->save("mydoc.pdf");
Then open mydoc.pdf in magento1.7 folder, it's opened with no error and displays "Hello World" on each pages of Pdf document. Now compare these two files "myfile.pdf" and "mydoc.pdf" in text editor, you will see presence of html contents in "myfile.pdf". My problem is that I am not able to find a way for removing these html contents from generated PDF document. Can anybody help me in resolving this issue. Please note that above code is just a demostration of what I did.
解决方案There is more than one way to skin a cat. I take it you want a nice PDF of the wishist. Consider using wkhtml2pdf to make it. This will involve styling up the PRINT version of the page - there is a CSS that gets added for that if media=print, which is the case with wkhtml2pdf.
The reslts of wkhtml2pdf are vastly better than anything 'hand drawn' with Zend PDF stuff.
To get started install wkhtml2pdf and simply run it on the command line, tinkering with the options until it comes out half way decent.
The only downside to wkhtml2pdf is you have no control on the page breaks.
相关文章