Search code examples
phppdfechomagento2magento2.2

Print simple message in pdf invoice of Magento2 using echo?


I have created a Default invoice of Magento2 under the (sales order) option, see the screenshot: https://prnt.sc/tje4zn

This is the file path for invoice pdf of Magento2: /vendor/magento/module-sales/Model/Order/Pdf/AbstractPdf.php and below is the file code,

I want to echo simple text message in the end of the file like

<?php echo "NOTE: This is not a GST invoice. This is a packing slip only.";  ?>

Please help me with how I can achieve and add this message to pdf invoice format in the last as I have also mentioned in the screenshot above.

Many Thanks in Advance.


Solution

  • You can do so this with code, Assuming you have a custom Vendor_Invoice module: Change the path specific and module name as per yours. Put following content into the Vendor/Invoice/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">
        <preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="Vendor\Invoice\Model\Order\Pdf\InvoicePdf"/>
    </config>
    

    Now you can create Vendor\Invoice\Model\Order\Pdf\InvoicePdf.php file & add the below code,

    <?php
    
    namespace Vendor\Invoice\Model\Order\Pdf;
    
    use \Magento\Sales\Model\Order\Pdf\Invoice;
    
    class InvoicePdf extends Invoice
    {
        /**
         * We only need to override the getPdf of Invoice,
         *  most of this method is copied directly from parent class
         *
         * @param array $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();
                }
                // draw custom notice
                $this->drawNotice($page);
            }
            $this->_afterGetPdf();
            return $pdf;
        }
    
        /**
         * draw notice below content
         *
         * @param \Zend_Pdf_Page $page
         */
        protected function drawNotice(\Zend_Pdf_Page $page) {
            $iFontSize = 10;     // font size
            $iColumnWidth = 520; // whole page width
            $iWidthBorder = 260; // half page width
            $sNotice = "NOTE: This is not a GST invoice. This is a packing slip only."; // your message
            $iXCoordinateText = 30;
            $sEncoding = 'UTF-8';
            $this->y -= 10; // move down on page
            try {
                $oFont = $this->_setFontRegular($page, $iFontSize);
                $iXCoordinateText = $this->getAlignCenter($sNotice, $iXCoordinateText, $iColumnWidth, $oFont, $iFontSize);  // center text coordinate
                $page->setLineColor(new \Zend_Pdf_Color_Rgb(1, 0, 0));                                             // red lines
                $iXCoordinateBorder = $iXCoordinateText - 10;                                                               // border is wider than text
                // draw top border
                $page->drawLine($iXCoordinateBorder, $this->y, $iXCoordinateBorder + $iWidthBorder, $this->y);
                // draw text
                $this->y -= 15;                                                                                             // further down
                $page->drawText($sNotice, $iXCoordinateText, $this->y, $sEncoding);
                $this->y -= 10; // further down
                // draw bottom border
                $page->drawLine($iXCoordinateBorder, $this->y, $iXCoordinateBorder + $iWidthBorder, $this->y);
                // draw left border
                $page->drawLine($iXCoordinateBorder, $this->y, $iXCoordinateBorder, $this->y + 25 /* back to first line */);
                // draw right border
                $page->drawLine($iXCoordinateBorder + $iWidthBorder, $this->y, $iXCoordinateBorder + $iWidthBorder, $this->y + 25 /* back to first line */);
                $this->y -= 10;
            } catch (\Exception $exception) {
                // handle
            }
        }
    
        /**
         * Draw header for item table
         *
         * @param \Zend_Pdf_Page $page
         * @return void
         */
        protected function _drawHeader(\Zend_Pdf_Page $page)
        {
            /* Add table head */
            $this->_setFontRegular($page, 10);
            $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
            $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5));
            $page->setLineWidth(0.5);
            $page->drawRectangle(25, $this->y, 570, $this->y - 15);
            $this->y -= 10;
            $page->setFillColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));
    
            //columns headers
            $lines[0][] = ['text' => __('Products'), 'feed' => 35];
    
            $lines[0][] = ['text' => __('SKU'), 'feed' => 290, 'align' => 'right'];
    
            // custom column
            $lines[0][] = ['text' => __('HSN'), 'feed' => 290, 'align' => 'right'];
    
            $lines[0][] = ['text' => __('Qty'), 'feed' => 435, 'align' => 'right'];
    
            $lines[0][] = ['text' => __('Price'), 'feed' => 360, 'align' => 'right'];
    
            $lines[0][] = ['text' => __('Tax'), 'feed' => 495, 'align' => 'right'];
    
            $lines[0][] = ['text' => __('Subtotal'), 'feed' => 565, 'align' => 'right'];
    
            $lineBlock = ['lines' => $lines, 'height' => 5];
    
            $this->drawLineBlocks($page, [$lineBlock], ['table_header' => true]);
            $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
            $this->y -= 20;
        }
    }
    

    Hope this will helps, and please let me know if there is any issues...