【问题标题】:Zend_pdf document throws error in magento due to presence of html contents由于存在 html 内容,Zend_pdf 文档在 magento 中引发错误
【发布时间】:2012-12-05 18:14:38
【问题描述】:

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

  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();?>

在 app\design\frontend\base\default\template\wishlist\view.phtml (view.phtml) 文件中,就在 &lt;div class="my-wishlist"&gt; 行之前。现在在浏览器中运行 magento 并登录。然后单击我的愿望清单链接,它将在下载文件夹中显示 myfile.pdf。打开时会抛出错误消息。现在替换这些行

   $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 内容的方法。谁能帮我解决这个问题。 请注意,上面的代码只是我所做的演示。

【问题讨论】:

    标签: php zend-framework magento zend-pdf


    【解决方案1】:

    给猫剥皮的方法不止一种。我认为您想要一个不错的愿望者 PDF。考虑使用 wkhtml2pdf 来制作它。这将涉及对页面的 PRINT 版本进行样式设置 - 如果 media=print,则会为此添加一个 CSS,wkhtml2pdf 就是这种情况。

    wkhtml2pdf 的结果比 Zend PDF 的任何“手绘”都要好。

    要开始安装 wkhtml2pdf 并简单地在命令行上运行它,对选项进行修改,直到它出现一半体面。

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

    【讨论】:

    • 我想要上述问题的解决方案,而不是替代解决方案。请帮忙
    【解决方案2】:

    Mage_Wishlist_IndexController::indexAction() 末尾添加此代码:

    $sResponse = $this->getResponse()->getBody();
    $sNewResponse = strstr($sResponse, '%PDF-1.4');
    $this->getResponse()
        ->clearBody()
        ->appendBody($sNewResponse);
    

    就在$this-&gt;renderLayout() 电话后面。

    【讨论】:

    • 这不行,它仍然在pdf文档中显示html内容。
    • 使用您给定的代码 - except 替换为 $pdf-&gt;save("mydoc.pdf") - 并添加我的代码,如新的 Magento 1.7.0.2 安装所示。
    • 那么这可能是我的magento安装的问题。您能否提供一些其他禁用布局和视图渲染的方法。
    【解决方案3】:

    为了从 Magento 发送 PDF 字符串并将其呈现在浏览器上,您需要执行以下操作:

    $pdfString = $pdf->render();
    $this->getResponse()->setHeader('Content-Type', 'application/pdf');
    $this->getResponse()->setBody($pdfString);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 2010-12-08
      相关资源
      最近更新 更多