{"id":1427,"date":"2020-12-13T05:29:53","date_gmt":"2020-12-13T05:29:53","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/create-barcode-and-add-it-in-magento-2-invoice-pdf\/"},"modified":"2025-07-17T10:17:04","modified_gmt":"2025-07-17T04:47:04","slug":"create-barcode-and-add-it-in-magento-2-invoice-pdf","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-barcode-and-add-it-in-magento-2-invoice-pdf\/","title":{"rendered":"How to Create Barcode and Add it in Magento 2 Invoice PDF"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0allows the admin to send invoice PDF as the order completes.<\/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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default Magento 2 facilitates the merchants to provide multiple things in&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/display-extra-fee-to-the-total-of-order-invoice-pdf-in-magento-2\/\">invoice PDFs<\/a>&nbsp;like order id, customer name, shipping address, product name, product quantity, product price, total amount, and so on.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, depending on the business requirements, customization in invoice PDF can be done.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One such customization solution is offered in this post to&nbsp;<em><strong>create barcode and add it in Magento 2 invoice PDF<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Barcode in invoice PDF helps the customer to scan and get the order id using which it is easy to get order details.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Barcodes in invoices enhance the after-sales customer experience of the store. Barcodes are easy to use and decrease human errors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check the programmatic solution to add a barcode in invoice pdf in Magento 2 below:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution to Create Barcode and Add it in Magento 2 Invoice PDF<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>di.xml<\/strong>&nbsp;file at&nbsp;<strong><em><em><strong>Vendor\\Extension<\/strong><\/em><\/em><strong>\\etc<\/strong><\/strong><\/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\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"MagentoSalesModelOrderPdfInvoice\">\n        &lt;plugin name=\"barcodes\" type=\"VendorExtensionPluginInvoice\" sortOrder=\"10\"\/>\n    &lt;\/type>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>Invoice.php<\/strong>&nbsp;file in&nbsp;<strong><strong><em>Vendor\\Extension\\Plugin\\Invoice.php<\/em><\/strong><\/strong><\/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 VendorExtensionPlugin;\n\nuse MagentoFrameworkAppConfigScopeConfigInterface;\nuse ZendBarcodeBarcode;\n\nclass Invoice\n{\n    const XML_PATH_BARCODES_ENABLED = 'barcodes\/general\/eb_barcodes_active';\n    private $scopeConfig;\n\n    public function __construct(ScopeConfigInterface $scopeConfig)\n    {\n        $this->scopeConfig = $scopeConfig;\n    }\n\n    public function beforeInsertDocumentNumber($subject, $page, $text)\n    {\n        $config = new Zend_Config([\n            'barcode' => 'code128',\n            'barcodeParams' => [\n                'text' => $this->getInvoiceNumber($text),\n                'drawText' => true\n            ],\n            'renderer' => 'image',\n            'rendererParams' => ['imageType' => 'png']\n        ]);\n\n        $barcodeResource = Barcode::factory($config)->draw();\n        ob_start();\n        imagepng($barcodeResource);\n        $barcodeImage = ob_get_clean();\n        $image = new Zend_Pdf_Resource_Image_Png('data:image\/png;base64,' . base64_encode($barcodeImage));\n        if ($image) {\n            $docHeader = $subject->getDocHeaderCoordinates();\n            $page->drawImage($image, $docHeader[2] - 130, $docHeader[1] + 2, $docHeader[2] + 8, $docHeader[1] + 35);\n        }\n    }\n\n    protected function getInvoiceNumber($text)\n    {\n        $array_of_words = explode(\"#\", $text);\n        return $array_of_words[1];\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After doing this, the customers will start receiving invoice PDFs having a barcode.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/11\/Annotation-on-2020-11-11-at-16-22-20-e1605092492967.png\" alt=\"Stepwise Programmatic Solution to Generate Barcode in Invoice PDF in Magento 2\" class=\"wp-image-11713\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Done! That\u2019s what you need to do.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do consider sharing this post with Magento Community via social media. Your single share motivates me a lot.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0allows the admin to send invoice PDF as the order completes. Invoices are the official documents that you send with every order that is&#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-1427","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1427","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=1427"}],"version-history":[{"count":6,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1427\/revisions"}],"predecessor-version":[{"id":13919,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1427\/revisions\/13919"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}