请注意:
- 这不是good way为此,因为它会覆盖 Magento 核心文件,所以您必须覆盖模块中的这些文件。
- 这不是一个完整的解决方案但这是一个让你理解并进一步走得更远的提示。 (这将只打印标题,不打印数据)
我将指导您为客户添加PDF导出功能(默认有CSV和Excel)
Edit 应用程序/代码/核心/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 应用程序/代码/核心/Mage/Adminhtml/controllers/CustomerController.php,添加以下函数
public function exportPdfAction(){
$fileName = 'customers.pdf';
$content = $this->getLayout()->createBlock('adminhtml/customer_grid')->getPdfFile();
$this->_prepareDownloadResponse($fileName, $content);
}
Edit 应用程序/代码/核心/Mage/Adminhtml/Block/Customer/Grid.php, locate
$this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('customer')->__('Excel XML'));
添加 PDF 导出
$this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('customer')->__('Excel XML'));
$this->addExportType('*/*/exportPdf', Mage::helper('customer')->__('PDF'));
现在刷新管理,您可以将客户导出为PDF。