How to Create Barcode and Add it in Magento 2 Invoice PDF
Magento 2 allows the admin to send invoice PDF as the order completes.
Invoices are the official documents that you send with every order that is important for accounting and tax purposes.
The default Magento 2 facilitates the merchants to provide multiple things in invoice PDFs like order id, customer name, shipping address, product name, product quantity, product price, total amount, and so on.
However, depending on the business requirements, customization in invoice PDF can be done.
One such customization solution is offered in this post to create barcode and add it in Magento 2 invoice PDF.
Barcode in invoice PDF helps the customer to scan and get the order id using which it is easy to get order details.
Barcodes in invoices enhance the after-sales customer experience of the store. Barcodes are easy to use and decrease human errors.
Check the programmatic solution to add a barcode in invoice pdf in Magento 2 below:
Solution to Create Barcode and Add it in Magento 2 Invoice PDF
- Create di.xml file at VendorExtensionetc
1234567<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"><type name="MagentoSalesModelOrderPdfInvoice"><plugin name="barcodes" type="VendorExtensionPluginInvoice" sortOrder="10"/></type></config> - Create Invoice.php file in VendorExtensionPluginInvoice.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546<?phpnamespace VendorExtensionPlugin;use MagentoFrameworkAppConfigScopeConfigInterface;use ZendBarcodeBarcode;class Invoice{const XML_PATH_BARCODES_ENABLED = 'barcodes/general/eb_barcodes_active';private $scopeConfig;public function __construct(ScopeConfigInterface $scopeConfig){$this->scopeConfig = $scopeConfig;}public function beforeInsertDocumentNumber($subject, $page, $text){$config = new Zend_Config(['barcode' => 'code128','barcodeParams' => ['text' => $this->getInvoiceNumber($text),'drawText' => true],'renderer' => 'image','rendererParams' => ['imageType' => 'png']]);$barcodeResource = Barcode::factory($config)->draw();ob_start();imagepng($barcodeResource);$barcodeImage = ob_get_clean();$image = new Zend_Pdf_Resource_Image_Png('data:image/png;base64,' . base64_encode($barcodeImage));if ($image) {$docHeader = $subject->getDocHeaderCoordinates();$page->drawImage($image, $docHeader[2] - 130, $docHeader[1] + 2, $docHeader[2] + 8, $docHeader[1] + 35);}}protected function getInvoiceNumber($text){$array_of_words = explode("#", $text);return $array_of_words[1];}}
Done! That’s what you need to do.
If you have questions for this post, feel free to ask queries in the Comment section below.
I will try my level best to solve your queries.
Also, do consider sharing this post with Magento Community via social media. Your single share motivates me a lot.
Thank you.
Auto-assign barcode numbers to products & print customized barcode labels in Magento 2 Store.
Related Posts:
- How to Add Order ID in Invoice PDF in Magento 2
- How to Change PDF Invoice Logo in Magento 2
- How to Add Header and Footer to Magento 2 Invoice PDF
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.
8 Comments
its shows error like Class ‘Zend\Barcode\Barcode’ not found shows on my version of code
Hello Monarc,
The mentioned issue is version specific issue as from Magento 2.3.6, it uses laminas instead of zend framework.
You can use the below solution:
Find the below line of code
$barcodeResource = Barcode::factory($config)->draw();
and replace it with
$barcodeResource =\Zend_ Barcode::factory($config)->draw();
Thank You
Hi I am new to magento. I refer the internet and practicing. I searched for barcode to implement in invoice pdf. I choose your coding and implemented. I got the below error. Please help me to rectify.
[InvalidArgumentException]
Plugin class ColanDisributorsPluginInvoice doesn’t exist
Hello,
Please check if the path given at the plugin in di.xml has the Invoice.php file or not. Also, check if the namespace is correct in the Invoice.php file.
Thanks.
Hello,
I have used this code in Magento 2.3.2 and 2.4.2 also but no barcode show at invoice .
Please let us know the issue.
Thanks.
Hello Smruti,
It’s working in Magento 2.3.2, please check the error log.
And, it will not work in Magento 2.4.2 because it supports ‘laminas’ which you can use, instead of Zend classes.
Thank You.
Hello,
I have used Magento 2.3.5-p2 and add this code in module.
But, its shows error like Class ‘Zend\Barcode\Barcode’ not found
Please let us know the issue.
Thanks.
Hello Khushbu,
I have used Magento 2.3.3 for this solution.
Thank you.