Customize the design of PDF invoice in Magento

Customize the design of PDF invoice in Magento

Posted: 10 years ago in  PHP | Magento |


Generate PDF Invoice is one magento's feature. It use Zend PDF library to generate invoice. As email template, we can customize it for our purposes like adding in more informatio / redesign color and text style or logo


Generate PDF Invoice is one magento's feature. It use Zend PDF library to generate invoice. As email template, we can customize it for our purposes like adding in more informatio / redesign color and text style or logo

To change logo at the top of invoice, we can use the magento admin panel

Go to System => Configuration => Sale => Invoice and Packing Slip Design

To change style, add information, following next steps but before, you need to know the structure of magento module. Here is quick tutorial to create custom module in Magento

The main purpose is creating a module to override functions that generating PDF Invoice

Step 1:

Create folder CustomPDF in /app/code/local. In this folder, we create folder tree:

CustomPDF

--------Check

--------etc

--------config.xml

--------Model

--------Invoice.php

Step 2:

This is content in CustomPDF/Check/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
 <modules>
 <CustomPDF_Check>
 <version>1.0.0</version>
 </CustomPDF_Check>
 </modules>
 <global>
 <models>
 <sales>
 <rewrite> 
 <order_pdf_invoice>CustomPDF_Check_Model_Invoice</order_pdf_invoice>
 </rewrite>
 </sales>
 </models>
 </global>
</config>

Explaination: CustomPDF is module, Check is subfolder, Model is folder where file defining class of module, Invoice is file declaring model.

Step 3:

This is content of CustomPDF/Check/Model/Invoice.php

class CustomPDF_Check_Model_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
{}

Within class declaration in CustomPDF/Check/Model/Invoice.php, you can override the functions declared in app/code/core/Mage/Sales/Model/Order/Pdf/Invoice.php. Notice the class name if you want to change module's name.

Here some tweaks you might needs

I override function getPdf($invoices = array()) and comment out line

$this->insertLogo($page, $invoice->getStore());

the logo at the top of invoice will disappeared.

You can add in information using $page variable to draw it in insertOrder() or insertAddress(), try this snippet code then you will get it

$this->_setFontBold($page, 12);
$page->drawText("This is custom text and it should be bold");
$this->_setFontRegular($page, 12);
$page->drawText("This is custom text and it shouldn't be bold");

You can use magento function Mage:: to load helpers, model... then print the data in invoices.

Step 4:

Then, register this module with magento. Create file CustomPDF_All.xml in app/etc/modules

<?xml version="1.0" encoding="UTF-8"?>
<config>
 <modules>
 <CustomPDF_Check>
 <active>true</active>
 <codePool>local</codePool>
 </CustomPDF_Check>
 </modules>
</config>

Go to Sales => Invoice => Print to view your customization