Override a Method of Abstract File of Magento 2 Invoice PDF
Do you want to change your store invoices from boring pieces of paper to new attractive and effective brand building tool in the Magento 2 store? If yes, this post is for you!
Invoices are the official documents that you send with every order that is important for accounting and tax purposes. Default Magento 2 offers invoice template as shown below.
But this is not enough. A store needs to add the signature, logo, etc. elements that help in branding. Also, it improves the appearance of the invoice. It is necessary to optimize the invoice as it is the means of communication that customers promptly open as they are bound to open it to check if they are charged the correct amount. Better invoices help in reinforcing your brand values!
Changing the default Invoice is not a permanent solution. You may lose your changes if your Magento 2 version is upgraded. Also, it is not advisable to make changes in the default configuration. The solution is to override a method of abstract file of Magento 2 invoice PDF.
It enables the custom design of the Invoice, while not disturbing the default settings! Here I am taking an example of how to change the font of invoice pdf that uses the procedure to override a method of abstract file of Magento 2 invoice pdf.
Method to Override a Method of Abstract File of Magento 2 Invoice PDF:
- Create registration.php in appcodeVendorModule and add the following code:
123456<?phpMagentoFrameworkComponentComponentRegistrar::register(MagentoFrameworkComponentComponentRegistrar::MODULE,'Vendor_Module',__DIR__); - Create module.xml in appcodeVendorModuleetc and add the following code:
12345<?xml version="1.0" encoding="UTF-8"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="Vendor_Module" setup_version="1.0.0"/></config> - Create di.xml which is located at appcodeVendorModuleetc and add the following code:
1234<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"><preference for="MagentoSalesModelOrderPdfInvoice" type="VendorModuleModelRewriteOrderPdfInvoice"/></config> - Create Invoice.php in appcodeVendorModuleModelRewriteOrderPdf and add the following code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657<?phpnamespace VendorModuleModelRewriteOrderPdf;use MagentoSalesModelOrderPdfInvoice as CoreInvoice;class Invoice extends CoreInvoice{/*** Set font as regular** @param Zend_Pdf_Page $object* @param int $size* @return Zend_Pdf_Resource_Font*/protected function _setFontRegular($object, $size = 7){$font = Zend_Pdf_Font::fontWithPath($this->_rootDirectory->getAbsolutePath('lib/internal/dejavu-sans/DejaVuSansCondensed.ttf'));$object->setFont($font, $size);return $font;}/*** Set font as bold** @param Zend_Pdf_Page $object* @param int $size* @return Zend_Pdf_Resource_Font*/protected function _setFontBold($object, $size = 7){$font = Zend_Pdf_Font::fontWithPath($this->_rootDirectory->getAbsolutePath('lib/internal/dejavu-sans/DejaVuSansCondensed.ttf'));$object->setFont($font, $size);return $font;}/*** Set font as italic** @param Zend_Pdf_Page $object* @param int $size* @return Zend_Pdf_Resource_Font*/protected function _setFontItalic($object, $size = 7){$font = Zend_Pdf_Font::fontWithPath($this->_rootDirectory->getAbsolutePath('lib/internal/dejavu-sans/DejaVuSansCondensed.ttf'));$object->setFont($font, $size);return $font;}}
When you implement the above method, you can set the custom font in the Invoice pdf. Similarly, you can design custom Invoice pdf too!
You can send Invoices that serve as the marketing tool as shown here using the above method:
No more sloppy and unprofessional Emails that you have to change with every Magento 2 upgrade!
Hopefully, your customers will be impacted with the invoice marketing technique
Magento 1 store owners can also refer the same solution for Magento 1 at “How To Override a Method of Abstract File of Magento Invoice PDF”
I’d be pleased to solve any doubts my readers have regarding the topic that can be mentioned in the Comments section.
Thank You!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
18 Comments
I needed a custom invoice downloadable option in admin>sales>orders actions dropdown list, I added an option successfully but unable to determine the path path=”Vendor_Module/Model/Rewrite/Order/Pdf/invoice”/ leads me to a 404 page.
Hii Ahmed Zubair,
For that You will have to create the extension as per mentioned in the blog
specifying Vendor Name and Extension Name.
Hi, I did all steps of this post but i can’t override the Invoice.php file
Hello Jose,
Run command of di compile after following all the steps
Check whether any third party module does not override invoice file.
Thank You
Hello
Do I need to declare contrusctor of AbstractPdf class to override insertOrder method ?
Hello Rajni,
You don’t need to use constructor.
Thanks
Hi, can you write funcitons how to add Note, like you did in your example?
Hello Tina,
For your requirements, you need to copy insertOrder and getPdf function from main class Magento\Sales\Model\Order\Pdf\Invoice and change code as per your requirements.
Thanks.
Hello, i would like to ask you how could include the comments history ONLY for administrators? For example, if the administrator choose Actions->Print Invoices from orders grid, the printed PDF to include also the comments history. Any idea? Many thanks
Hello Nick,
Please use the code to override for your requirements or contact us for further customization.
Thank you.
Hi, is it working in Magento 2.4.1 too? Where shoud be the extension in backend to change the the Invoice-Design?
Hello Stan,
There is no such option in the backend. The file that we discussed over here is called when the PDF is generated.
Thank you.
can I just copy the entire insertOrder Function from AbstractPDF.php to shipment.php to use? and I override the shipment.php model?
Hi Paul,
No, you cannot use the same function as it’s for the invoice and not for the shipment.
It not working on magento 2.3. I follow all the steps given. Please tell me how do I do It it very urgent for my project
Hello Ajay,
I’ve followed the same method and it works fine for me.
Thanks.
Do we need to restart server or anything we need to do after implementing above method?……..I did above steps but but invoice is generating in same format as earlier.
No, all you have to do are steps shown in the post. I have implemented this method and it works fine. Please check again if there is some issue in creating the extension.