Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a pdf in magento?

Tags:

pdf

admin

magento

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

like image 494
Wouter Avatar asked Dec 20 '22 20:12

Wouter


1 Answers

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.

like image 62
ilyes kooli Avatar answered Jan 05 '23 13:01

ilyes kooli