Basically for order invoice and packingslip we used default pdf system in magento. But if you want to add something extra information or custom image or barcode, you must customize the default magento pdf invoice and packingslip. Here some reason for customize magento invoice and packingslip
For Customize default pdf we will rewrite magento core classes that are responsible for the PDF generation.
So We will create new module and rewrite the following code in config.xml. I Don’t go for details how to create module.
For Invoice Pdf we will customize the invoice template
Code Location
For customization we need to change the code inside those file but as this is core file we don’t edit them we create a new module and rewrite them and all changes done in the local pool.
Our Config file should like this
[sourcecode language=”xml”]
<?xml version="1.0"?>
<config>
<modules>
<namespace_modulename>
<version>0.1.0</version>
</ namespace_modulename >
</modules>
<global>
<models>
<sales>
<rewrite>
<order_pdf_invoice>namespace_modulename_Model_Order_Pdf_Invoice</order_pdf_invoice>
<order_pdf_items_invoice_default>namespace_modulename_Model_Order_Pdf_Items_Invoice_Default</order_pdf_items_invoice_default>
</rewrite>
</sales>
</models>
</global>
</config>
[/sourcecode]
Go Mage_Sales_Model_Order_Pdf_Invoice. Take a look at the core Mage_Sales_Model_Order_Pdf_Invoice class. Copy getPDF function to the newly created Invoice.php. Now you are ready to make changes.
In code you will get line
$page = $this->newPage();
Edit For Moderation
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;
Changes the parameter in newPage() method for your expected pages
SIZE_A4
SIZE_A4_LANDSCAPE
SIZE_LETTER
SIZE_LETTER_LANDSCAPE
You have to write this code and changes value or uncomment to define header of order
Change the page size In code you will get line
$page = $this->newPage();
Edit For Moderation
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;
Changes the parameter in newPage() method for your expected pages:
SIZE_A4
SIZE_A4_LANDSCAPE
SIZE_LETTER
SIZE_LETTER_LANDSCAPE
Changing Header:
You have to write this code and chages value or uncomment to define header of order
[sourcecode language=”php”]
/**
* Draw table header for product items
*
* @param Zend_Pdf_Page $page
* @return void
*/
protected function _drawHeader(Zend_Pdf_Page $page)
{
/* Add table */
$this->y -= 23;
$page->setFillColor(new Zend_Pdf_Color_Html(‘#787879’));
$this->y -= 10;
//columns headers
$lines[0][] = array(
‘text’ => Mage::helper(‘sales’)->__(‘PRODUCT’),
‘feed’ => 37,
‘font_size’=> 9
);
$lines[0][] = array(
‘text’ => Mage::helper(‘sales’)->__(‘PRICE’),
‘feed’ => 403,
‘font_size’=> 9
);
$lines[0][] = array(
‘text’ => Mage::helper(‘sales’)->__(‘QTY’),
‘feed’ => 468,
‘font_size’=> 9
);
$lines[0][] = array(
‘text’ => Mage::helper(‘sales’)->__(‘SUBTOTAL’),
‘feed’ => 507.5,
‘font_size’=> 9
);
$lineBlock = array(
‘lines’ => $lines,
‘height’ => 11
);
$this->drawLineBlocks($page, array($lineBlock), array(‘table_header’ => true));
$page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
$this->y -=17.5;
}
[/sourcecode]
Same Way You can edit all method used on pdf creating. If you want to edit the default drawLineBlocks() method you have to copy the code of the method and keep in invoice.php file and edit as you want
This actually prints out text on your page. The parameters are text, x, y, format. So the follow text would appear near the top left of the page:
[sourcecode language=”php”]
$page->drawText("My Text", 35, 770, ‘UTF-8’);
[/sourcecode]
Before adding the text you may want to set the font size and colour. The following will set the font to a regular style rather than bold and in size 6. The next sets it to bold, and the next to italic.
[sourcecode language=”php”]
$this->_setFontRegular($page, 6);
$this->_setFontBold($page, 6);
$this->_setFontItalic($page, 6);
[/sourcecode]
[sourcecode language=”php”]
$page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
$page->setLineWidth(0.5);
$page->drawRectangle(30, 30, 50, 50);
$page->drawLine(200, 30, 200, 40);
[/sourcecode]
Drawing A Simple Rectangle
[sourcecode language=”php”]
$page->drawRectangle(30, 30, 50, 50);)
$page->drawRectangle(400, 600, 590, 830);)
[/sourcecode]
If we write this code a simple rectangule will create with 30px width and 50px height
[sourcecode language=”php”]
Insert Image
$image = Zend_Pdf_Image::imageWithPath("image_path");
$page->drawImage($image, 0, 750, 595, 842);
[/sourcecode]
One Important thing if you want to hide a part of the invoice
(example you don’t want to show SKU on invoice)
you have to customize the
/app/code/local/Mage/Sales/Model/Order/Pdf/Items/Invoices/Default.php file