Php 使用echo在Magento2的pdf发票中打印简单消息?
我已在(销售订单)选项下创建了Magento2的默认发票,请参见屏幕截图: 这是Magento2发票pdf的文件路径:/vendor/magento/module sales/Model/Order/pdf/AbstractPdf.php,下面是文件代码 我想在文件末尾回显简单的文本消息,如Php 使用echo在Magento2的pdf发票中打印简单消息?,php,pdf,echo,magento2,magento2.2,Php,Pdf,Echo,Magento2,Magento2.2,我已在(销售订单)选项下创建了Magento2的默认发票,请参见屏幕截图: 这是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."; ?> 请帮助我
<?php echo "NOTE: This is not a GST invoice. This is a packing slip only."; ?>
请帮助我如何实现并将此消息添加到pdf发票格式,正如我在上面的屏幕截图中所提到的
非常感谢。您可以通过代码来实现这一点, 假设您有一个自定义供应商发票模块:根据您的更改路径特定的模块名称。将以下内容放入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;
}
}
getStoreId()
)
);
/*添加文档文本和编号*/
$this->insertDocumentNumber($page,_('Invoice#')。$Invoice->getIncrementId());
/*添加表*/
$this->\u图纸标题($page);
/*添加正文*/
foreach($invoice->getAllItems()作为$item){
如果($item->getOrderItem()->getParentItem()){
继续;
}
/*抽签项目*/
$this->\u drawItem($item,$page,$order);
$page=end($pdf->pages);
}
/*加总*/
$this->insertTotals($page,$invoice);
如果($invoice->getStoreId()){
$this->_localeResolver->revert();
}
//提取海关通知单
$this->drawNotice($page);
}
$this->_afterGetPdf();
返回$pdf;
}
/**
*在内容下方绘制通知
*
*@param\Zend\u Pdf\u第页$Page
*/
受保护函数drawNotice(\Zend\u Pdf\u Page$Page){
$iFontSize=10;//字体大小
$iColumnWidth=520;//整页宽度
$iWidthBorder=260;//半页宽
$sNotice=“注意:这不是商品及服务税发票。这只是一张装箱单。”;//您的留言
$iXCoordinateText=30;
$sEncoding='UTF-8';
$this->y-=10;//在第页上向下移动
试一试{
$oFont=$this->\u setFontRegular($page,$iFontSize);
$iXCoordinateText=$this->getAlignCenter($sNotice,$iXCoordinateText,$iColumnWidth,$oFont,$iFontSize);//居中文本坐标
$page->setLineColor(新建\Zend\u Pdf\u Color\u Rgb(1,0,0));//红线
$IXCoordinateOrder=$iXCoordinateText-10;//边框比文本宽
//画上边框
$page->drawine($ixCoordinateOrder,$this->y,$ixCoordinateOrder+$iWidthBorder,$this->y);
//绘制文本
$this->y-=15;//再往下
$page->drawText($sNotice,$iXCoordinateText,$this->y,$sEncoding);
$this->y-=10;//再往下
//画底边
$page->drawine($ixCoordinateOrder,$this->y,$ixCoordinateOrder+$iWidthBorder,$this->y);
//画左边框
$page->drawLine($IXCoordinateOrder,$this->y,$IXCoordinateOrder,$this->y+25/*返回第一行*/);
//画右边框
$page->drawLine($IXCoordinateOrder+$iWidthBorder,$this->y,$iXCoordinateBorder+$iWidthBorder,$this->y+25/*返回第一行*/);
$this->y-=10;
}catch(\Exception$Exception){
//处理
}
}
/**
*绘制项目表的标题
*
*@param\Zend\u Pdf\u第页$Page
*@返回无效
*/
受保护的函数\u drawHeader(\Zend\u Pdf\u Page$Page)
{
/*加桌头*/
$this->_setFontRegular($page,10);
$page->setFillColor(新的\Zend\uPDF\uColor\uRGB(0.93,0.92,0.92));
$page->setLineColor(新建\Zend\u Pdf\u颜色\u灰度(0.5));
$page->setLineWidth(0.5);
$page->drawRectangle(25,$this->y,570,$this->y-15);
$this->y-=10;
$page->setFillColor(新建\Zend\u Pdf\u颜色\u Rgb(0,0,0));
//列标题
$lines[0][]=['text'=>(产品),'feed'=>35];
$lines[0][]=['text'=>SKU','feed'=>290','align'=>right'];
//自定义列
$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'=>(小计),'feed'=>565,'align'=>'right'];
$lineBlock=['lines'=>$lines,'height'=>5];
$this->drawLineBlocks($page,[$lineBlock],'table_header'=>true]);
$page->setFillColor(新建\Zend\u Pdf\u颜色\u灰度(0));
$this->y-=20;
}
}
希望这会有所帮助,如果有任何问题,请告诉我…嘿,伙计,让我检查并实施相同的方法。