由于 html 内容的存在,Zend_pdf 文档在 magento 中引发错误 [英] Zend_pdf document throws error in magento due to presence of html contents

查看:21
本文介绍了由于 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)文件中,就在

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

 $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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆