在PDF文件显示产品图片-Display Products Images On PDF
如果喜欢本blog,请订阅.或者点击下旁边的+1:
Here thanks for Mladen Lotar
因为客户需要,一直在尝试显示产品图片到PDF文件.
这样会方便手下的其他人去拿货(店长可能根据产品名字和sku就能知道是什么产品,但是手下的人并不一定知道)
自己在一年前就曾经尝试过添加,但是一直没有成功.今年看见inchoo发表了一个增加属性到PDF的文章.所以就阅读了一下他们的代码.
发现他们增加的是字符串,我需要增加的是产品图片.还是不能解决我的问题.所以就给他们留言了.
没有想到,马上就得到了回复.和他们给的代码.由于他们是在Magento专业版1.9上做的测试.所以自己就尝试移植过来,并按自己的需求更改了代码.
下面是按我改动后的代码:
创建: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, $width)
->__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'), 245, $this->y, 'UTF-8');
$page->drawText(Mage::helper('sales')->__('SKU'), 375, $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'), 245, $this->y, 'UTF-8');
$page->drawText(Mage::helper('sales')->__('SKU'), 375, $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');
$page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
$this->y -=20;
}
return $page;
}
创建: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 QTY
$lines[0][] = array(
'text' => $item->getQty()*1,
'feed' => 495
);
// draw Price
$lines[0][] = array(
'text' => $order->formatPriceTxt($item->getPrice()),
'feed' => 445,
'font' => 'bold',
'align' => 'right'
);
/* 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' => 60
);
$page = $pdf->drawLineBlocks($page, array($lineBlock), array('table_header' => true));
$this->setPage($page);
}
详细分析了下.发现Magento专业版和Magento社区版的代码规范是一样的.几乎没有出入.
演示:

参考文章:http://inchoo.net/ecommerce/magento/magento-pdf-invoice-with-product-images-extension/
转载请附上本站链接:http://www.ecartchina.com