We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    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

    <?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="MagentoSalesModelOrderPdfInvoice" type="[vendername][modulename]ModelOrderPdfinvoice" />
     <preference for="MagentoSalesModelOrderPdfShipment" type="[vendername][modulename]OrderPdfshipment" />
    </config>

    Step 2

    Create new model file Pdfinvoice.php in your Model/Order folder and put below code in it.

    <?php
    namespace [vendername][modulename]ModelOrder;
    class Pdfinvoice extends MagentoSalesModelOrderPdfInvoice
    {
     /**
     * 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,
     MagentoStoreModelScopeInterface::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.

    <?php
    namespace [vendername][modulename]ModelOrder;
    class Pdfshipment extends MagentoSalesModelOrderPdfShipment
    {
     /**
     * Return PDF document
     *
     * @param MagentoSalesModelOrderShipment[] $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,
     MagentoStoreModelScopeInterface::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');
    
     }
    }
    field_5bfb909c5ccae

      Get a Free Quote