PDF中文乱码
1、添加支持的中文字体(SourceHanSans-Normal.ttf)
将字体放到 lib/internal/GnuFreeFont/
目录下即可
2、重写\Magento\Sales\Model\Order\Pdf\Invoice
及 Magento\Sales\Model\Order\Pdf\Shipment
(注:抽象类 Magento\Sales\Model\Order\Pdf\AbstractPdf
不支持重写,所以需要重写继承了它的子类)
1)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">
<!--Pdf support chinese fonts-->
<preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="Vendor_name\Module_name\Model\Order\Pdf\Invoice" />
<preference for="Magento\Sales\Model\Order\Pdf\Shipment" type="Vendor_name\Module_name\Model\Order\Pdf\Shipment" />
</config>
2)Vendor_name\Module_name\Model\Order\Pdf\Invoice.php
<?php
namespace Vendor_name\Module_name\Model\Order\Pdf;
class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
/**
* Set font as regular
*
* @param \Zend_Pdf_Page $object
* @param int $size
* @return \Zend_Pdf_Resource_Font
*/
protected function _setFontRegular($object, $size = 7)
{
$font = \Zend_Pdf_Font::fontWithPath(
$this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/SourceHanSans-Normal.ttf')
);
$object->setFont($font, $size);
return $font;
}
/**
* Set font as bold
*
* @param \Zend_Pdf_Page $object
* @param int $size
* @return \Zend_Pdf_Resource_Font
*/
protected function _setFontBold($object, $size = 7)
{
$font = \Zend_Pdf_Font::fontWithPath(
$this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/SourceHanSans-Normal.ttf')
);
$object->setFont($font, $size);
return $font;
}
/**
* Set font as italic
*
* @param \Zend_Pdf_Page $object
* @param int $size
* @return \Zend_Pdf_Resource_Font
*/
protected function _setFontItalic($object, $size = 7)
{
$font = \Zend_Pdf_Font::fontWithPath(
$this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/SourceHanSans-Normal.ttf')
);
$object->setFont($font, $size);
return $font;
}
}
Vendor_name\Module_name\Model\Order\Pdf\Shipment.php
同上;
PDF内容(products, address)过长未自动换行
1、products 过长需重写 Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice
下 draw() 方法;
2、address 过长需重写 Magento\Sales\Model\Order\Pdf\AbstractPdf
_formatAddress() 方法;
注:以上2个function中 split() 的第二个参数即用于设置换行长度,将其修改为自己需要的长度即可
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。