magento2 的 pdf 导出很大,有 3m,怎么缩小点?

3.05K 浏览M2交流区PDF

magento2 的 pdf 导出很大,有 3m,怎么缩小点?

magento2 的 pdf 导出很大,有 3m,怎么缩小点?

就纯文字,还只有一页

我看wps 导出就几百k

还好几页的

已回答
0

需要重写生成pdf的代码,修改字体大小。

以invoce的pdf为例,假设你自己的插件为YourModule_Sales.

1,app/code/YourModule/Sales/etc/di.xml

重写默认的pdf代码

<?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="YourModuleSalesModelOrderPdfInvoice" />

</config>

2,app/code/YourModule/Sales/Model/Order/Pdf/Invoice.php

创建文件

<?php
namespace YourModuleSalesModelOrderPdf;

/**
 * Use built-in fonts in PDFs so that invoices are smaller.
 *
 */
class Invoice extends MagentoSalesModelOrderPdfInvoice
{
    protected function _setFontRegular($object, $size = 7)
    {
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); // or FONT_TIMES for serif
        $object->setFont($font, $size);
        return $font;
    }

    protected function _setFontBold($object, $size = 7)
    {
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD); // or FONT_TIMES_BOLD for serif
        $object->setFont($font, $size);
        return $font;
    }

    protected function _setFontItalic($object, $size = 7)
    {
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_ITALIC); // or FONT_TIMES_ITALIC for serif
        $object->setFont($font, $size);
        return $font;
    }
}

这样会将您的发票从每张3Mb减少到2Kb。

参考

https://magento.stackexchange.com/questions/243648/how-to-make-invoice-pdf-file-size-compress-in-magento-2

发表新评论

您好,虽然缩小了,但是里面如果有中文字体就无法显示,请问如果做到既能保留中文,又能缩小pdf大小?谢谢。

0