Override a Method of Abstract File of Magento 2 Invoice PDF

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.

1_Default Magento Invoice

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:

  1. Create registration.php in app\code\Vendor\Module\ and add the following code:
  2. Create module.xml in app\code\Vendor\Module\etc and add the following code:
  3. Create di.xml which is located at app\code\Vendor\Module\etc and add the following code:
  4. Create Invoice.php in app\code\Vendor\Module\Model\Rewrite\Order\Pdf and add the following code:
     

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:

2_Custom Designed M2 Invoice

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!

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

4.7
(based on 15 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

14 comments On Override a Method of Abstract File of Magento 2 Invoice PDF

  • Hello
    Do I need to declare contrusctor of AbstractPdf class to override insertOrder method ?

  • 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

  • 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?

  • 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

  • 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.

Leave a reply:

Your email address will not be published.