Well designed and properly formatted invoices always make a good impression and sometimes badly required to fit enough and needful business information within. Defaut Magento neither provides adding more information nor allow customization as it’s way complicated. While working with one of our extensions, we need to change the layout & override default Invoice PDF in Magento to properly fit all the business information. You may require this custom code in your extension to fulfill custom requirements and thus I’m sharing my code here.
Put following code in Module config.xml file:
1 2 3 4 5 6 |
NameSpace_ModuleName_Model NameSpace_ModuleName_Model_Order_Pdf_Invoice NameSpace_ModuleName_Model_Order_Pdf_Items_Invoice_Default |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
<!--?php class NameSpace_ModuleName_Model_Order_Pdf_Items_Invoice_Default extends Mage_Sales_Model_Order_Pdf_Items_Invoice_Default { public function draw() { $order = $this->getOrder(); $item = $this->getItem(); $pdf = $this->getPdf(); $page = $this->getPage(); $lines = array(); $HSNCODE = $this->getHsnCodeValue($item); // draw Product name $lines[0] = array(array( 'text' => Mage::helper('core/string')->str_split($item->getName(), 60, true, true), 'feed' => 35, )); $lines[0][] = array( 'text' => Mage::helper('core/string')->str_split(CustomField, 25), 'feed' => 245 ); // draw SKU $lines[0][] = array( 'text' => Mage::helper('core/string')->str_split($this->getSku($item), 25), 'feed' => 325 ); // draw QTY $lines[0][] = array( 'text' => $item->getQty()*1, 'feed' => 435 ); // draw Price $lines[0][] = array( 'text' => $order->formatPriceTxt($item->getPrice()), 'feed' => 395, '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' => 10 ); $page = $pdf->drawLineBlocks($page, array($lineBlock), array('table_header' => true)); $this->setPage($page); } private function getHsnCodeValue($item) { $prod = Mage::getModel('catalog/product')->load($item->getProductId()); if(!($return_hsncode = $prod->getHsnCode())) return 'N/A'; else return $return_hsncode; } } </pre> <p>Put below code in <strong>NameSpace/ModuleName/Model/Order/Pdf/</strong> as <strong>Invoice.php</strong></p> <pre class="lang:default decode:true"> <?php class NameSpace_ModuleName_Model_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice { public function getPdf($invoices = array()) { $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()); } $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; $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')->__('HSN Code'), 245, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('SKU'), 325, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Price'), 380, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Qty'), 430, $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)); 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); } $page = $this->insertTotals($page, $invoice); if ($invoice->getStoreId()) { Mage::app()->getLocale()->revert(); } } $this->_afterGetPdf(); return $pdf; } public function newPage(array $settings = array()) { $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')->__('HSN Code'), 245, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('SKU'), 325, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Price'), 380, $this->y, 'UTF-8'); $page->drawText(Mage::helper('sales')->__('Qty'), 430, $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; } } </pre> <p><strong>Note:</strong>Consider HSN Code as our custom field, you can replace it with your own.</p> <p>Once you finish implementing code, go to <strong>Sales >> Invoice >> Print</strong> to test the same and after downloading the PDF, it should contain the custom field you added.<br ?--> Simply change the code according to your requirements and test again. I hope this code has helped you! Cheers! |
Mage::helper(‘core/string’)->str_split($loremIpsum, 60, true, true);
when i use this i get an error that ‘…\…\…\Mage’ is not found in my defaultInvoice.php file can you please guide.
Mage::helper(‘core/string’)->str_split($loremipsum, 60, true, true) when i do this i get an error that the ‘…\…\…\Mage’ not found in my defaultInvoice.php file i suppose that Mage::helper should be available please guide i am stuck! thanks.