Zend_pdf document throws error in magento due to presence of html contents

流过昼夜 提交于 2019-12-04 05:36:40

问题


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;
   }

Call this function

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

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;

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.


回答1:


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.




回答2:


Add this code at the end of your Mage_Wishlist_IndexController::indexAction():

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

right behind the $this->renderLayout() call.




回答3:


In order to send a PDF string from Magento and have it rendered on the browser you need to do the following:

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


来源:https://stackoverflow.com/questions/12214990/zend-pdf-document-throws-error-in-magento-due-to-presence-of-html-contents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!