How to create a pdf in magento?

后端 未结 1 2004
星月不相逢
星月不相逢 2021-02-04 20:51

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

相关标签:
1条回答
  • 2021-02-04 21:36

    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.

    0 讨论(0)
提交回复
热议问题