{"id":311,"date":"2019-02-08T11:02:47","date_gmt":"2019-02-08T11:02:47","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/02\/08\/override-magento-2-invoice-pdf\/"},"modified":"2025-05-22T17:06:13","modified_gmt":"2025-05-22T11:36:13","slug":"override-magento-2-invoice-pdf","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/override-magento-2-invoice-pdf\/","title":{"rendered":"Override a Method of Abstract File of Magento 2 Invoice PDF"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2019\/01\/1_Default-Magento-Invoice.png\" alt=\"1_Default Magento Invoice\" class=\"wp-image-4193\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">But this is not enough. A store needs to add the signature, logo, etc. elements that help in branding.&nbsp;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!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&nbsp;<em><strong>override a method of abstract file of&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/display-extra-fee-to-the-total-of-order-invoice-pdf-in-magento-2\/\">Magento 2 invoice PDF<\/a><\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It enables the custom design of the Invoice, while not disturbing the default settings! Here I am taking an example of&nbsp;how to change the font of invoice pdf that uses the procedure&nbsp;to&nbsp;<em><strong>o<\/strong><\/em><em><strong>verride a method of abstract file of Magento 2 invoice pdf<\/strong><\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Override a Method of Abstract File of Magento 2 Invoice PDF:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<em><strong>registration.php<\/strong><\/em>&nbsp;in&nbsp;<em><strong>app\\code\\Vendor\\Module&nbsp;<\/strong><\/em>and&nbsp;add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nMagento\\Framework\\Component\\ComponentRegistrar::register(\n    Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'Vendor_Module',\n    __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create module.xml in&nbsp;<em><strong>app\\code\\Vendor\\Module\\etc<\/strong><\/em>&nbsp;and&nbsp;add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Vendor_Module\" setup_version=\"1.0.0\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<em><strong>di.xml<\/strong><\/em>&nbsp;which is located at&nbsp;<em><strong>app\\code\\Vendor\\Module\\etc<\/strong><\/em>&nbsp;and add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;preference for=\"Magento\\Sales\\Model\\Order\\Pdf\\Invoice\" type=\"Vendor\\Module\\Model\\Rewrite\\Order\\Pdf\\Invoice\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create&nbsp;<em><strong>Invoice.php<\/strong><\/em>&nbsp;in&nbsp;<em><strong>app\\code\\Vendor\\Module\\Model\\RewriteOrderPdf<\/strong><\/em>&nbsp;and add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Model\\Rewrite\\Order\\Pdf;\n\nuse Magento\\Sales\\Model\\Order\\Pdf\\Invoice as CoreInvoice;\n\nclass Invoice extends CoreInvoice\n{\n    \/**\n     * Set font as regular\n     *\n     * @param \\Zend_Pdf_Page $object\n     * @param int $size\n     * @return \\Zend_Pdf_Resource_Font\n     *\/\n    protected function _setFontRegular($object, $size = 7)\n    {\n        $font = \\Zend_Pdf_Font::fontWithPath(\n            $this->_rootDirectory->getAbsolutePath('lib\/internal\/dejavu-sans\/DejaVuSansCondensed.ttf')\n        );\n        $object->setFont($font, $size);\n        return $font;\n    }\n\n    \/**\n     * Set font as bold\n     *\n     * @param \\Zend_Pdf_Page $object\n     * @param int $size\n     * @return \\Zend_Pdf_Resource_Font\n     *\/\n    protected function _setFontBold($object, $size = 7)\n    {\n        $font = \\Zend_Pdf_Font::fontWithPath(\n            $this->_rootDirectory->getAbsolutePath('lib\/internal\/dejavu-sans\/DejaVuSansCondensed.ttf')\n        );\n        $object->setFont($font, $size);\n        return $font;\n    }\n\n    \/**\n     * Set font as italic\n     *\n     * @param \\Zend_Pdf_Page $object\n     * @param int $size\n     * @return \\Zend_Pdf_Resource_Font\n     *\/\n    protected function _setFontItalic($object, $size = 7)\n    {\n        $font = \\Zend_Pdf_Font::fontWithPath(\n            $this->_rootDirectory->getAbsolutePath('lib\/internal\/dejavu-sans\/DejaVuSansCondensed.ttf')\n        );\n        $object->setFont($font, $size);\n        return $font;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When you implement the above method, you can set the custom font in the Invoice pdf. Similarly, you can design custom Invoice pdf too!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can send Invoices that serve as the marketing tool as shown here using the above method:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2019\/01\/2_Custom-Designed-M2-Invoice.jpg\" alt=\"2_Custom Designed M2 Invoice\" class=\"wp-image-4194\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">No more sloppy and unprofessional Emails that you have to change with every Magento 2 upgrade!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hopefully, your customers will be impacted with the invoice marketing technique<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Magento 1 store owners can also refer the same solution for Magento 1 at \u201c<a href=\"https:\/\/meetanshi.com\/blog\/override-method-of-abstract-file-of-magento-invoice-pdf\/\" target=\"_blank\" rel=\"noreferrer noopener\">How To Override a Method of Abstract File of Magento Invoice PDF<\/a>\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-311","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=311"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":15511,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions\/15511"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}