让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的打印原理也是一样的,好了,不足之处,还请高人指正,谢谢!附上效果图:


原创粉丝点击