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

How to Add Header and Footer to Magento 2 Invoice PDF

By Sanjay JethvaUpdated on May 22, 2025 4 min read

As a Magento 2 store owner, you surely know that you always have to send invoices to your customers with every order you deliver. Sending out authoritative transactional documents is crucial for both yours and your customers’ accounting and tax objectives.

Another important thing is how is your invoice designed. Earlier, I had shown how to make changes in the default invoice with the method to override a method of abstract file of Magento 2 invoice PDF. It is necessary that you refer that post prior to implementing the below solution.

Having a well-designed invoice pays off. Today, I’ll show how to add header and footer to Magento 2 invoice PDF programmatically.  Headers and footers carry the important company information to give order documents a more professional look. Showcase your brand name and logo, add the company details and improve the brand engagement with existing customers!

A makeover for the default Magento 2 invoice PDF with informative header and footer is just going to be easy with the below solution!

Add the function _drawFooter() in the class – VendorModuleModelRewriteOrderPdfInvoice

Method to Add Header and Footer to Magento 2 Invoice PDF:

protected function _drawFooter(\Zend_Pdf_Page $page)
    {
        /* Add table foot */
        try {
            $this->y -= 10;
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $fileSystem = $objectManager->create('\Magento\Framework\Filesystem');
            $mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath();

            $image = \Zend_Pdf_Image::imageWithPath($mediaPath.'wysiwyg\your_image.png');   // [MAGENTO_ROOT]/pub/media/wysiwyg/your_image.png This is an absolute path
            $top = 70;
            //top border of the page
            $widthLimit = 270;
            //half of the page width
            $heightLimit = 270;
            
            $width = $image->getPixelWidth();
            $height = $image->getPixelHeight();

            //preserving aspect ratio (proportions)
            $ratio = $width / $height;
            if ($ratio > 1 && $width > $widthLimit) {
                $width = $widthLimit;
                $height = $width / $ratio;
            } elseif ($ratio < 1 && $height > $heightLimit) {
                $height = $heightLimit;
                $width = $height * $ratio;
            } elseif ($ratio == 1 && $height > $heightLimit) {
                $height = $heightLimit;
                $width = $widthLimit;
            }

            $y1 = $top - $height;
            $y2 = $top;
            $x1 = 35;
            $x2 = $x1 + $width;

            //coordinates after transformation are rounded by Zend
            $page->drawImage($image, $x1, $y1, $x2, $y2);
            $font = $this->_setFontBoldOver($page, 10);
            $value = $this->getFooterContent();
            $line = 28;
            if ($value !== '') {
                $value = str_replace(' @ ', "\n", $value);

                $page->setFillColor(new \Zend_Pdf_Color_RGB(0, 0, 0));
                $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5));
                foreach(explode("\n", $value) as $textLine){
                    //$feed = $this->getAlignCenter($textLine, 30, 520, $font, 12);
                    $page->drawText(strip_tags($textLine), 120, $line, 'UTF-8');
                    $line -=16;
                }
                $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
            }
            $this->y -= 20;
        } catch (\Exception $e) {
            \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($e->getMessage());
        }
    }

In this class, you have to override insertOrder() method and replace the following code:

$this->insertTotals($page, $invoice);

with:

$page = $this->insertTotals($page, $invoice);
$this->_drawFooter($page);

That’s it! The above example is to add the logo and address in the footer. You can customize the code as per the business requirement and send invoices as the specimen shown here:

Header and Footer to Magento 2 Invoice PDF - Meetanshi

You may also want to remove the default footer links in Magento 2.

I’d be very grateful if you helped share this helpful post on social media to fellow developers!

Thanks!

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.