🔥 Just Launched! Werra Premium Template for HyväSee it in Action

Override a Method of Abstract File of Magento 2 Invoice PDF

By Sanjay JethvaUpdated on May 22, 2025 4 min read

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:

<?php
Magento\Framework\Component\ComponentRegistrar::register(
    Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

2. Create module.xml in app\code\Vendor\Module\etc and add the following code:

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

3. Create di.xml which is located at app\code\Vendor\Module\etc and add the following code:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="Vendor\Module\Model\Rewrite\Order\Pdf\Invoice"/>
</config>

4. Create Invoice.php in app\code\Vendor\Module\Model\RewriteOrderPdf and add the following code:

<?php

namespace Vendor\Module\Model\Rewrite\Order\Pdf;

use Magento\Sales\Model\Order\Pdf\Invoice 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:

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

Thank You!

Sanjay Jethva Full Image
Article bySanjay 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.