It is impossible that you have never noticed those bar lines that are printed on the backside of any product or product related docs. Whatever it is but for us, it’s completely unreadable until we own optical character reader machine or software that reads those data which is made up of varying the widths and spacings of parallel lines in a linear structure.
In this digital era, technology is changing rapidly with new aspects of innovation to make human life easier than it was. But for Barcode its opposite, day by day it became digital and inexpensive to generate and print that can store various information. Also, it helps a lot to eliminate the possibility of human error and also it takes less little than a second to read.
Today, we are back with a different kind of blog that helps you to automatically create a barcode of invoice ID and add it to invoice pdf In Magento 2.
First, we need to create custom extension after that for displaying our barcode in invoice PDF we need to link our custom code file.
app\code\Vendor\Extension\etc\di.xml
<?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="Magento\Sales\Model\Order\Pdf\Invoice"> <plugin name="vendor_extension_sales_model_order_pdf_invoice" type="Vendor\Extension\Plugin\Invoice" sortOrder="10" /> </type> </config>
Now we need to create invoice.php which contains our logic of generating a barcode. app\code\Vendor\Extension\Plugin\invoice.php
<?php namespace Vendor\Extension\Plugin; class Invoice { public function beforeInsertDocumentNumber($subject, $page, $text) { $docHeader = $subject->getDocHeaderCoordinates(); $image = $this->_generateBarcode($text); $width = $image->getPixelWidth(); $height = $image->getPixelHeight(); $page->drawImage($image, $docHeader[2] - $width, $docHeader[1] -$height, $docHeader[2], $docHeader[1]); } protected function _generateBarcode($text) { $config = new \Zend_Config([ 'barcode' => 'code128', 'barcodeParams' => [ 'text' => $this->_extractInvoiceNumber($text), 'drawText' => true ], 'renderer' => 'image', 'rendererParams' => ['imageType' => 'png'] ]); $barcodeResource = \Zend_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)); return $image; } protected function _extractInvoiceNumber($text) { $array_of_words = explode("#", $text); return $array_of_words[1]; } }
Step 1: Add this class,
use \Zend\Barcode\Barcode;
Step 2:Replace this:
$barcodeResource = \Zend_Barcode::factory($config)->draw();
With this:
$barcodeResource = Barcode::factory($config)->draw();
Tadaa! You did it.
Now you can sit back and relax. This code will automatically generate a barcode for each invoice pdf. You can also modify this according to your need of generating a barcode for various product documents.
Lastly, hit that stars if this code worked for you.
Happy Coding!
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…
As the world of eCommerce continues to thrive, Shopify has become one of the most…
Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…
Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…
View Comments
Hello ,
This code is not working for magento 2.4.1 can you please help.
Thank you
Can you please share the issues which you are facing in magento 2.4.1
Hi,
In Magento 2.4.1 display this issue: Error: Class 'Zend\Barcode\Barcode' not found
I have to face the same issue.
Are resolved this issue Talal Aswad?
In the New Magento version, they remove the classes, you need to do things in another way.
So you contact us at support@magecomp.com for more information.
Hello ,
I used this code in magento 2.3.2 and 2.4.1 also . But problem is that no barcode show on that invoice pdf page , Please Help.
Can you please share the exact issue if you are facing?
Could not get to work for Magento v2.3
We have updated the code for Magento 2.3. please refer again.