使用 echo 在 Magento2 的 pdf 发票中打印简单消息?

Print simple message in pdf invoice of Magento2 using echo?

我在(销售订单)选项下创建了Magento2的默认发票,见截图:https://prnt.sc/tje4zn

这是Magento2发票pdf的文件路径:/vendor/magento/module-sales/Model/Order/Pdf/AbstractPdf.php下面是文件代码,

我想在文件末尾回显简单的文本消息,例如

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

请帮助我如何实现并将此消息添加到最后的 pdf 发票格式,正如我在上面的屏幕截图中也提到的那样。

非常感谢。

你可以用代码做到这一点, 假设您有一个自定义 Vendor_Invoice 模块:根据您的更改特定路径和模块名称。将以下内容放入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>

现在您可以创建 Vendor\Invoice\Model\Order\Pdf\InvoicePdf.php 文件并添加以下代码,

<?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;
    }
}

希望这会有所帮助,如果有任何问题,请告诉我...