4

I'm looking for a way to export product data and attributes from the product page via an export to PDF button. Are there any reliable extensions around as a solution?

I'm currently looking at the following 2 extensions:

Global PDF: http://www.boostmyshop.com/english/global-pdf.html

Magazento: http://www.magazento.com/english/magento-ext/magazento-extensions/pdf-export

10
+25

I have suggest simple solution. I think you want the link at product detail page. when some user click on the Download PDF Button then it will generate the PDF of that product only.

For that you have to create one Helper Class. Suppose your Namespace will be Test and Module name will be Generatepdf.

Step 1: Create the module Configuration file in \app\etc\modules\Test_Generatepdf.xml as below.

<?xml version="1.0"?>
   <config>
     <modules>
        <Test_Generatepdf>
          <active>true</active>
          <codePool>local</codePool>
          <version>0.1.0</version>
        </Test_Generatepdf>
     </modules>
   </config>

Step 2: Create the Module Configuration file in \app\code\local\Test\Generatepdf\etc\config.xml as below.

<?xml version="1.0"?>
    <config>
      <modules>
        <Test_Generatepdf>
            <version>0.1.0</version>
        </Test_Generatepdf>
     </modules>
  <global>
      <helpers>
          <generatepdf>
            <class>Test_Generatepdf_Helper</class>
          </generatepdf>
        </helpers>
  </global>
</config> 

Step 3: Create the Helper Class with below code in \app\code\local\Test\Generatepdf\Helper\Data.php.

<?php
class Test_Generatepdf_Helper_Data extends Mage_Core_Helper_Abstract
{
   public function generatePdfFile($_product){
       $this->_isExport = true;
       $pdf = new Zend_Pdf(); 
       $pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); 
       $page=$pdf->pages[0]; // this will get reference to the first page. 
       $style = new Zend_Pdf_Style(); 
       $style->setLineColor(new Zend_Pdf_Color_Rgb(0,0,0)); 
       $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES); 
       $style->setFont($font,12); 
       $page->setStyle($style); 
       $page->drawText($_product->getName(),100,($page->getHeight()-100)); 
       // Hear Add your data as per your requrement.
       // Blank page and pdf headers 
       header('Content-type: application/pdf'); 
       header('Content-Disposition: attachment; filename="downloaded.pdf"'); 

       // Browser watch 
       echo $pdf->render(); 
    }
}

Step 4: access this method in product Detail Page. Make Sure you must have to pass the $_product Object.

 Mage::helper('generatepdf')->generatePdfFile($_product);

Enjoy :)

Update:

If you pass the Product ID in parameter and Load the Product Collection on helper you got the all the detail of product Detail with all available attributes. You can modify this function as per your Requirement.

  • ok no problem :) – Keyul Shah Jun 16 '14 at 3:08
  • @KeyulShah : I followed and got" ( ! ) SCREAM: Error suppression ignored for ( ! ) Fatal error: Class 'Mage_Test_Helper_Data' not found in D:\wamp\www\magento\app\Mage.php on line 547" > Please check once – TBI Infotech Jun 16 '14 at 6:59
  • I have Update the code – Keyul Shah Jun 16 '14 at 7:09
  • Yes the change removes the error but I think the pdf, still needs to be modified, not sure if my system is not configured properly, hopefully you have tested it! – TBI Infotech Jun 16 '14 at 7:39
  • 1
    That's y I mention that you have to change the pdf script code as per your requirement – Keyul Shah Jun 16 '14 at 8:32
4

This may need some alteration

Edit

app/code/core/Mage/Adminhtml/Block/Widget/Grid.php

, add the following function

public function getPdfFile(){
        $this->_isExport = true;
        $this->_prepareGrid();
        $this->getCollection()->getSelect()->limit();
        $this->getCollection()->setPageSize(0);
        $this->getCollection()->load();
        $this->_afterLoadCollection();

        $pdf = new Zend_Pdf();
        $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
        $page->setFont($font, 12);
        $width = $page->getWidth();
        $i=0;
        foreach ($this->_columns as $column) {
            if (!$column->getIsSystem()) {
                $i+=10;
                $header = $column->getExportHeader();                
                $page->drawText($header, $i, $page->getHeight()-20);                
                $width = $font->widthForGlyph($font->glyphNumberForCharacter($header));
                $i+=($width/$font->getUnitsPerEm()*12)*strlen($header)+10;
            }
        }
        $pdf->pages[] = $page;
        return $pdf->render();
    }

app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php

public function exportPdfAction(){
        $fileName = 'products.pdf';        
//check grid for product once if it breaks.        
$content = $this->getLayout()->createBlock('adminhtml_catalog_product_grid')->getPdfFile();
        $this->_prepareDownloadResponse($fileName, $content);
    }

app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php

$this->addExportType('*/*/exportCsv', Mage::helper('catalog/product')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('catalog/product')->__('Excel XML'));

Add the PDF Export

        $this->addExportType('*/*/exportCsv', Mage::helper('catalog/product')->__('CSV'));
        $this->addExportType('*/*/exportXml', Mage::helper('catalog/product')->__('Excel XML'));
        $this->addExportType('*/*/exportPdf', Mage::helper('catalog/product')->__('PDF'));

Now refresh the admin, you can export products as PDF.

Please let me know if it works.

  • They need pdf export functionality in product detail page not in backend grid. – Keyul Shah Jun 22 '14 at 0:45
  • @TBI Infotech ok. i will try and let you know. – HungryDB Nov 10 '14 at 9:23
  • I got 500 internal server error. Did i miss something or code need to be reviewed. – HungryDB Nov 10 '14 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.