One of our clients was looking to add the Barcode using OrderIncrement id.
To achieve that I used picqer/php-barcode-generator library in Magento. You can follow the remaining steps only after installing the library.
Step 1: Create a Plugin and define that plugin di.xml. Use the below code and add it to the di.xml file.
1 2 3 |
<type name="Magento\Sales\Model\Order\Pdf\Invoice"> <plugin name="barcodes_pdf_invoice" type="Magemonkeys\Barcodes\Plugin\Invoice" sortOrder="10" /> </type> |
Step 2: Create a Invoice.php file under Plugin under this folder “app/code/Magemonkeys/Barcode/Plugin“.
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<?php /** * @category Print Barcode On Invoice PDF * @package Magemonkeys_Barcodes * @author Abbacus Team */ namespace Magemonkeys\Barcodes\Plugin; use Magento\Framework\App\Config\ScopeConfigInterface; use Picqer\Barcode\BarcodeGeneratorPNG; class Invoice { /** * @var ScopeConfigInterface */ private $scopeConfig; /** * @var \Magento\Sales\Model\Order\Invoice */ private $invoiceModel; /** * * @param ScopeConfigInterface $scopeConfig * @param \Magento\Sales\Model\Order\Invoice $invoiceModel */ public function __construct( ScopeConfigInterface $scopeConfig, \Magento\Sales\Model\Order\Invoice $invoiceModel ) { $this->scopeConfig = $scopeConfig; $this->invoiceModel = $invoiceModel; } /** * * @param string $subject * @param object $page * @param string $text */ public function beforeInsertDocumentNumber($subject, $page, $text) { $docHeader = $subject->getDocHeaderCoordinates(); $image = $this->generateBarcode($text); $page->drawImage($image, $docHeader[2] - 150, $docHeader[1] + 20, $docHeader[2] , $docHeader[1] +55); } /** * * @param string $text * @return \Zend_Pdf_Resource_Image_Png */ protected function generateBarcode($text) { $invoiceIncrement = $this->extractInvoiceNumber($text); $invoice = $this->invoiceModel->loadByIncrementId(trim($invoiceIncrement)); $order = $invoice->getOrder(); $orderIncrementId = $order->getIncrementId(); $generator = new BarcodeGeneratorPNG; $image = new \Zend_Pdf_Resource_Image_Png('data:image/png;base64,' . base64_encode($generator->getBarcode($orderIncrementId, $generator::TYPE_CODE_128))); return $image; } /** * * @param string $text * @return string */ protected function extractInvoiceNumber($text) { $array_of_words = explode("#", $text); return $array_of_words[1]; } } |
Now, when you print the Invoice PDF, the barcode will be shown on the top of the PDF. Happy coding. 🙂
[crayon-63e09dffe6308468299553/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.