How to Add Header and Footer to Magento 2 Invoice PDF
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 – Vendor\Module\Model\Rewrite\Order\Pdf\Invoice
Method to Add Header and Footer to Magento 2 Invoice PDF:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
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()); } } |
1 |
$this->insertTotals($page, $invoice); |
1 2 |
$page = $this->insertTotals($page, $invoice); $this->_drawFooter($page); |
Feel free to ask any doubts in the Comments section below. I’d be happy to help. 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
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.
6 Comments
Could not find $this->insertTotals($page, $invoice); in the class?
Where do we need to add this.
I have created a module also with your reference blog for the pdf invoice changes.
but its not working as could not find “$this->insertTotals($page, $invoice);” in the class..
Hello Himanshu,
Extend Magento\Sales\Model\Order\Pdf\Invoice in class for the access.
Thank you.
Thanks you so much for your help.. appreciated!!
Can we do same with packaging slip and shipment in same custom module created?
Hello Himanshu,
Yes, it is possible. For shipment, you need to inherit the shipment related class.
Thank you.
Hi Sanjay,
What is the purpose of using Object Manager here in the code sample?
Thanks.
Hello,
The Object Manager is used to initialize Magento\Framework\Filesystem
Using Magento\Framework\Filesystem object, we can get absolute path of current magento.
Thanks.