让Magento生成的PDF打印图片(Magento invoice shipment pdf add product images)
来源:互联网 发布:知乎怎么邀请好友回答 编辑:程序博客网 时间:2024/05/17 00:08
应公司的要求,尝试着在magento打印出来的invoice和shipment中打印产品图片。于是乎参考了电商中国的http://www.ecartchina.com/display-products-images-on-pdf.html和Inchoo的http://inchoo.net/ecommerce/magento/magento-pdf-invoice-with-product-images-extension/两位前辈的文章,这里我想对带产品图打印做个小结,有什么不对的地方还望读者指教,谢谢!
Magento的打印功能其实是用ZF中的zend_pdf类库来实现的,因为magento本身就集成了zf,所以可以在代码中直接使用该类库,具体的document可以参见zf官方文档http://framework.zend.com/manual/en/zend.pdf.drawing.html 这里的测试版本我选用magento CE 1.5.1.0的老版本
首先更改app/code/local/Mage/Sales/Model/Order/Pdf/Invoice.php的代码:
class Mage_Sales_Model_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Abstract{protected function insertImage($image, $x1, $y1, $x2, $y2, $width, $height, &$page){if (!is_null($image)) {try{$width = (int) $width;$height = (int) $height; //Get product image and resize it.$imagePath = Mage::helper('catalog/image')->init($image, 'image')->keepAspectRatio(true)->keepFrame(false)->resize($width, height)->__toString();$imageLocation = substr($imagePath,strlen(Mage::getBaseUrl()));$image = Zend_Pdf_Image::imageWithPath($imageLocation);//Draw image to PDF$page->drawImage($image, $x1, $y1, $x2, $y2);}catch (Exception $e) {return false;}}} public function getPdf($invoices = array()) { $width = 500;$height = 500; $this->_beforeGetPdf(); $this->_initRenderer('invoice'); $pdf = new Zend_Pdf(); $this->_setPdf($pdf); $style = new Zend_Pdf_Style(); $this->_setFontBold($style, 10); foreach ($invoices as $invoice) { if ($invoice->getStoreId()) { Mage::app()->getLocale()->emulate($invoice->getStoreId()); Mage::app()->setCurrentStore($invoice->getStoreId()); } $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); $pdf->pages[] = $page; $order = $invoice->getOrder(); /* Add image */ $this->insertLogo($page, $invoice->getStore()); /* Add address */ $this->insertAddress($page, $invoice->getStore()); /* Add head */ $this->insertOrder($page, $order, Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId())); $page->setFillColor(new Zend_Pdf_Color_GrayScale(1)); $this->_setFontRegular($page); $page->drawText(Mage::helper('sales')->__('Invoice # ') . $invoice->getIncrementId(), 35, 780, 'UTF-8'); /* Add table */ $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92)); $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5)); $page->setLineWidth(0.5); $page->drawRectangle(25, $this->y, 570, $this->y -15); $this->y -=10; /* Add table head */ $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4)); $page->drawText(Mage::helper('sales')->__('Products'), 35, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Product Image'), 255, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('SKU'), 380, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Price'), 430, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Qty'), 480, $this->y, 'UTF-8'); //$page->drawText(Mage::helper('sales')->__('Tax'), 480, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8'); $this->y -=15; $page->setFillColor(new Zend_Pdf_Color_GrayScale(0)); /* Add body */ foreach ($invoice->getAllItems() as $item){ if ($item->getOrderItem()->getParentItem()) { continue; } if ($this->y < 15) { $page = $this->newPage(array('table_header' => true)); } /* Draw item */ $page = $this->_drawItem($item, $page, $order); /* Draw product image */$productId = $item->getOrderItem()->getProductId();$image = Mage::getModel('catalog/product')->load($productId);$this->insertImage($image, 240, (int)($this->y + 15), 350, (int)($this->y+115), $width, $height, $page); } /* Add totals */ $page = $this->insertTotals($page, $invoice); if ($invoice->getStoreId()) { Mage::app()->getLocale()->revert(); } } $this->_afterGetPdf(); return $pdf; } /** * Create new page and assign to PDF object * * @param array $settings * @return Zend_Pdf_Page */ public function newPage(array $settings = array()) { /* Add new table head */ $page = $this->_getPdf()->newPage(Zend_Pdf_Page::SIZE_A4); $this->_getPdf()->pages[] = $page; $this->y = 800; if (!empty($settings['table_header'])) { $this->_setFontRegular($page); $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92)); $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5)); $page->setLineWidth(0.5); $page->drawRectangle(25, $this->y, 570, $this->y-15); $this->y -=10; $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4)); $page->drawText(Mage::helper('sales')->__('Product'), 35, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Product Image'), 255, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('SKU'), 380, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Price'), 430, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Qty'), 480, $this->y, 'UTF-8'); //$page->drawText(Mage::helper('sales')->__('Tax'), 535, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8'); $page->setFillColor(new Zend_Pdf_Color_GrayScale(0)); $this->y -=20; } return $page; }
TAX我注释掉了,因为我不需要将其打印出来。
然后修改app/code/local/Mage/Sales/Model/Order/Pdf/Items/Invoice/Default.php
class Mage_Sales_Model_Order_Pdf_Items_Invoice_Default extends Mage_Sales_Model_Order_Pdf_Items_Abstract{ /** * Draw item line * */ public function draw() { $order = $this->getOrder(); $item = $this->getItem(); $pdf = $this->getPdf(); $page = $this->getPage(); $lines = array(); // draw Product name $lines[0] = array(array( 'text' => Mage::helper('core/string')->str_split($item->getName(), 60, true, true), 'feed' => 35, )); // draw SKU $lines[0][] = array( 'text' => Mage::helper('core/string')->str_split($this->getSku($item), 25), 'feed' => 375 ); // draw Price $lines[0][] = array( 'text' => $order->formatPriceTxt($item->getPrice()), 'feed' => 445, 'font' => 'bold', 'align' => 'right' ); // draw QTY $lines[0][] = array( 'text' => $item->getQty()*1, 'feed' => 485 ); // draw Tax /* $lines[0][] = array( 'text' => $order->formatPriceTxt($item->getTaxAmount()), 'feed' => 495, 'font' => 'bold', 'align' => 'right' );*/ // draw Subtotal $lines[0][] = array( 'text' => $order->formatPriceTxt($item->getRowTotal()), 'feed' => 565, 'font' => 'bold', 'align' => 'right' ); // custom options $options = $this->getItemOptions(); if ($options) { foreach ($options as $option) { // draw options label $lines[][] = array( 'text' => Mage::helper('core/string')->str_split(strip_tags($option['label']), 70, true, true), 'font' => 'italic', 'feed' => 35 ); if ($option['value']) { $_printValue = isset($option['print_value']) ? $option['print_value'] : strip_tags($option['value']); $values = explode(', ', $_printValue); foreach ($values as $value) { $lines[][] = array( 'text' => Mage::helper('core/string')->str_split($value, 50, true, true), 'feed' => 40 ); } } } } $lineBlock = array( 'lines' => $lines, 'height' => 110 ); $page = $pdf->drawLineBlocks($page, array($lineBlock), array('table_header' => true)); $this->setPage($page); }}
循环打印出产品,并且设置行距为110,这样两步就OK了,提醒下,zend_pdf要求php extension 有GD库和Zilb库来运行jpg和png格式的图片打印,另外,请注意你的图片路径没有问题,如果图片打印不出来,请使用断点调试来确保图片路径可供程序访问。(我之前就是因为这个问题白白耗了1天,晕~)shipment的打印原理也是一样的,好了,不足之处,还请高人指正,谢谢!附上效果图:
- 让Magento生成的PDF打印图片(Magento invoice shipment pdf add product images)
- Add Custom Info on Invoice Pdf for Magento
- Add Product Images via CSV in Magento
- 可以修改Magento的订单号,Invoice,Shipment和Credit Memo号码的插件-Flex Invoice
- Magento订单打印(pdf格式) 转:鹏程万里
- Product Tier Price add/update in Magento
- Magento PDF发票,支持中文,以及修改的办法.
- [原创]解决Magento 导出中文PDF 过大的问题。
- Magento打印(配送单、退款单、发票)时PDF中的乱码问题
- magento -- 通过Shipment Code找到Order信息
- magento------让菜单显示图片-------显示图片的菜单
- 生成pdf专题之一 打印生成pdf
- 在magento的xml文件中加入图片images,或者一些其他的html代码
- magento前台展示product的各种信息
- magento product 添加新的字段
- [Magento] Get Product Attributes
- [Magento] Product Import Scripts
- magento missing product img
- 防盗软件基础知识
- 通过存储过程分页
- 2011年 下半年 C++程序设计(二十八)
- db2基本命令
- 2011年 下半年 C++程序设计(二十九)
- 让Magento生成的PDF打印图片(Magento invoice shipment pdf add product images)
- 改变D3D中的渲染状态---SetRenderState
- QTableWidget的其中某些列怎么设置为只读模式使其不能被修改
- Lucene教程详解
- [工具]Mac平台开发几个网络抓包工具
- gcc预处理注释后会留下空白
- myecplise 和 svn的集成和一些基本操作
- 20120305晴
- ADC和触摸屏接口