4

I want to create a pdf in magento of the product list in the backend/admin. i don't know how to do it and the things i find on the internet are not that helpfull. Hope somebody can help me.

gr

edit

class Wouterkamphuisdotcom_Web_Adminhtml_WebController extends Mage_Adminhtml_Controller_action {

protected function _initAction() {
    $this->loadLayout()
            ->_setActiveMenu('web/items')
            ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));

    return $this;
}
public function exportPdfAction(){
    $fileName = 'customers.pdf';        
    $content = $this->getLayout()->createBlock('Web/Web_Grid')->getPdfFile();
    $this->_prepareDownloadResponse($fileName, $content);
}

this is my controller

Wouter
  • 465
  • 2
  • 7
  • 24

1 Answers1

11

Please note:

  • This is not the good way to do it, because it overrides Magento core files, you have to override those files within your modeule.
  • This is not a complete solution but a tip to make you understand and go further yourself. (this will print only headers, no data)

I will guide you adding PDF Export feature to customers (by default there is CSV and Excel)

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

Edit app/code/core/Mage/Adminhtml/controllers/CustomerController.php, add the following function

public function exportPdfAction(){
    $fileName = 'customers.pdf';        
    $content = $this->getLayout()->createBlock('adminhtml/customer_grid')->getPdfFile();
    $this->_prepareDownloadResponse($fileName, $content);
}

Edit app/code/core/Mage/Adminhtml/Block/Customer/Grid.php, locate

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

Add the PDF Export

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

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

ilyes kooli
  • 11,959
  • 14
  • 50
  • 79
  • thx this will help a lot, now i only need to find out how to add data :) – Wouter Jun 06 '12 at 14:22
  • Update your question and post the relevant code of WebController.php so we can help you.. – ilyes kooli Jun 07 '12 at 09:16
  • found the answer myself, there was an error in Web/Web_Grid, needed to be another addition to it: Web/Adminhtml_Web_Grid – Wouter Jun 08 '12 at 11:21
  • Good, it works fine. i also tried this pdf export method for products but it doesn't work for me. here's the link http://magento.stackexchange.com/questions/23840/export-product-data-attributes-to-pdf – HungryDB Nov 10 '14 at 10:07
  • 1
    Downvoted for instructing to modify core files most likely. (something that should NEVER be done in Magento) – Wranorn May 18 '16 at 07:22