本文介绍了Zend_pdf文件因存在html内容而在magento中引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在magento1.7中,只需在app \ code \ core \ Mage \ Wishlist \ Helper \ Data.php(Data.php)文件中写入以下行

In magento1.7, just write following lines in app\code\core\Mage\Wishlist\Helper\Data.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;
   }

调用此功能

  <?php  Mage::helper('wishlist')->getpdf();?>

<div class="my-wishlist">行之前的app \ design \ frontend \ base \ default \ template \ wishlist \ view.phtml(view.phtml)文件中的

.现在在浏览器中运行magento并登录.然后单击我的愿望清单"链接,它将在downlods文件夹中显示myfile.pdf.打开它会引发错误消息.现在替换这些行

in app\design\frontend\base\default\template\wishlist\view.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;

   $pdf->save("mydoc.pdf");

然后在magento1.7文件夹中打开mydoc.pdf,它没有任何错误地打开,并在Pdf文档的每一页上显示"Hello World".现在,在文本编辑器中比较这两个文件"myfile.pdf"和"mydoc.pdf",您将在"myfile.pdf"中看到html内容的存在. 我的问题是我找不到从生成的PDF文档中删除这些html内容的方法.谁能帮助我解决这个问题. 请注意,上面的代码只是我所做的演示.

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.

推荐答案

有多种方法可以给猫剥皮.我认为您需要一个理想的PDF文档.考虑使用wkhtml2pdf进行制作.这将涉及样式页面的PRINT版本-如果media = print,则为此添加了CSS,wkhtml2pdf就是这种情况.

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.

wkhtml2pdf的结果比使用Zend PDF东西的任何手工绘制"要好得多.

The reslts of wkhtml2pdf are vastly better than anything 'hand drawn' with Zend PDF stuff.

要开始安装wkhtml2pdf,只需在命令行上运行它,修改选项,直到出现一半即可.

To get started install wkhtml2pdf and simply run it on the command line, tinkering with the options until it comes out half way decent.

wkhtml2pdf的唯一缺点是您无法控制分页符.

The only downside to wkhtml2pdf is you have no control on the page breaks.

这篇关于Zend_pdf文件因存在html内容而在magento中引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 11:09