Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top