4

我想在后端/管理员的产品列表的 magento 中创建一个 pdf。我不知道该怎么做,而且我在互联网上找到的东西没有那么有用。希望有人能帮助我。

编辑

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

这是我的控制器

4

1 回答 1

11

请注意:

  • 这不是一个好方法,因为它覆盖了 Magento 核心文件,你必须在你的模块中覆盖这些文件。
  • 这不是一个完整的解决方案,而是一个提示,可以让您自己理解并走得更远。(这将只打印标题,不打印数据)

我将指导您向客户添加 PDF 导出功能(默认有 CSV 和 Excel)

编辑app/code/core/Mage/Adminhtml/Block/Widget/Grid.php,添加如下函数

 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.

于 2012-06-06T14:01:37.760 回答