In this article, I will guide you on, how to add VAT and Company Number in invoice and shipment PDF in Magento 2?
Follow the below steps:
You need to override two models in custom extension.
I assume you know about how to override model.
Step 1
Create di.xml file in your module’s etc folder and put below code in it
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="[vendername]\[modulename]\Model\Order\Pdfinvoice" /> <preference for="Magento\Sales\Model\Order\Pdf\Shipment" type="[vendername]\[modulename]\Order\Pdfshipment" /> </config> |
Step 2
Create new model file Pdfinvoice.php in your Model/Order folder and put below code in it.
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 76 77 78 79 80 81 82 |
<?php namespace [vendername]\[modulename]\Model\Order; class Pdfinvoice extends \Magento\Sales\Model\Order\Pdf\Invoice { /** * Return PDF document * * @param array|Collection $invoices * @return \Zend_Pdf */ public function getPdf($invoices = []) { $this->_beforeGetPdf(); $this->_initRenderer('invoice'); $pdf = new \Zend_Pdf(); $this->_setPdf($pdf); $style = new \Zend_Pdf_Style(); $this->_setFontBold($style, 10); foreach ($invoices as $invoice) { if ($invoice->getStoreId()) { $this->_localeResolver->emulate($invoice->getStoreId()); $this->_storeManager->setCurrentStore($invoice->getStoreId()); } $page = $this->newPage(); $order = $invoice->getOrder(); /* Add image */ $this->insertLogo($page, $invoice->getStore()); /* Add address */ $this->insertAddress($page, $invoice->getStore()); /* Add head */ $this->insertOrder( $page, $order, $this->_scopeConfig->isSetFlag( self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $order->getStoreId() ) ); /* Add document text and number */ $this->insertDocumentNumber($page, __('Invoice # ') . $invoice->getIncrementId()); /* Add table */ $this->_drawHeader($page); /* Add body */ foreach ($invoice->getAllItems() as $item) { if ($item->getOrderItem()->getParentItem()) { continue; } /* Draw item */ $this->_drawItem($item, $page, $order); $page = end($pdf->pages); } /* Add totals */ $this->insertTotals($page, $invoice); if ($invoice->getStoreId()) { $this->_localeResolver->revert(); } $this->_drawFooter($page); } $this->_afterGetPdf(); return $pdf; } protected function _drawFooter(\Zend_Pdf_Page $page) { $this->y = 50; $page->setFillColor(new \Zend_Pdf_Color_RGB(1, 1, 1)); $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5)); $page->drawRectangle(25, $this->y, 570, $this->y -30); $page->setLineWidth(0.5); $page->setFillColor(new \Zend_Pdf_Color_RGB(0.1, 0.1, 0.1)); $this->_setFontRegular($page, 10); $this->y -=16; $page->drawText(__('VAT # xxxxxxxx'), 220, $this->y, 'UTF-8'); $page->drawText(__('Company # xxxxxxxxx'), 320, $this->y, 'UTF-8'); } } |
Step 3
Create another new model file Pdfshipment.php in your Model/Order folder and put below code in it.
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 76 77 78 |
<?php namespace [vendername]\[modulename]\Model\Order; class Pdfshipment extends \Magento\Sales\Model\Order\Pdf\Shipment { /** * Return PDF document * * @param \Magento\Sales\Model\Order\Shipment[] $shipments * @return \Zend_Pdf */ public function getPdf($shipments = []) { $this->_beforeGetPdf(); $this->_initRenderer('shipment'); $pdf = new \Zend_Pdf(); $this->_setPdf($pdf); $style = new \Zend_Pdf_Style(); $this->_setFontBold($style, 10); foreach ($shipments as $shipment) { if ($shipment->getStoreId()) { $this->_localeResolver->emulate($shipment->getStoreId()); $this->_storeManager->setCurrentStore($shipment->getStoreId()); } $page = $this->newPage(); $order = $shipment->getOrder(); /* Add image */ $this->insertLogo($page, $shipment->getStore()); /* Add address */ $this->insertAddress($page, $shipment->getStore()); /* Add head */ $this->insertOrder( $page, $shipment, $this->_scopeConfig->isSetFlag( self::XML_PATH_SALES_PDF_SHIPMENT_PUT_ORDER_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $order->getStoreId() ) ); /* Add document text and number */ $this->insertDocumentNumber($page, __('Packing Slip # ') . $shipment->getIncrementId()); /* Add table */ $this->_drawHeader($page); /* Add body */ foreach ($shipment->getAllItems() as $item) { if ($item->getOrderItem()->getParentItem()) { continue; } /* Draw item */ $this->_drawItem($item, $page, $order); $page = end($pdf->pages); } if ($shipment->getStoreId()) { $this->_localeResolver->revert(); } $this->_drawFooter($page); } $this->_afterGetPdf(); return $pdf; } protected function _drawFooter(\Zend_Pdf_Page $page) { $this->y = 50; $page->setFillColor(new \Zend_Pdf_Color_RGB(1, 1, 1)); $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5)); $page->drawRectangle(25, $this->y, 570, $this->y -30); $page->setLineWidth(0.5); $page->setFillColor(new \Zend_Pdf_Color_RGB(0.1, 0.1, 0.1)); $this->_setFontRegular($page, 10); $this->y -=16; $page->drawText(__('VAT # xxxxxxxxx'), 220, $this->y, 'UTF-8'); $page->drawText(__('Company # xxxxxxxxx'), 320, $this->y, 'UTF-8'); } } |
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-6284635bcd796460258294/] Create...
You can try below code to change local date to...
Step 1: First you need to add registration.php file in...
Step1 : Override message.js in current theme file on the...