Pergunta

I'm trying to make compress invoice PDF file size. How to make this in Magento 2.2.5. Default PDF size is 3.2MB

I'm trying to make this in between 19KB. How to make this?

I tried below link's solution but my PDF reduce to 800kb. Not able to do make it 19kb. Here is the link I tried.

Reduce PDF file size in magento

How to add custom font also? There is two type of font I found in Magento 2.2.5 setup. But no hope by them.

Please help anybody. I tried lot, but unable to do this.

Foi útil?

Solução

Just to consolidate all this information:

1: Create (or use) a custom module under app/code/YourModule - eg app/code/YourModule/Sales

2: Add a <preference \> entry to app/code/YourModule/Sales/etc/di.xml - eg:

<?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="YourModule\Sales\Model\Order\Pdf\Invoice" />

</config>

3: Create the file app/code/YourModule/Sales/Model/Order/Pdf/Invoice.php

<?php
namespace YourModule\Sales\Model\Order\Pdf;

/**
 * Use built-in fonts in PDFs so that invoices are smaller.
 *
 */
class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
    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;
    }
}

That will reduce your invoices from 3Mb each to 2Kb.

Outras dicas

You need to replace below functions:

protected function _setFontRegular($object, $size = 7)
{
    /*$font = \Zend_Pdf_Font::fontWithPath(
        $this->_rootDirectory->getAbsolutePath('lib/internal/LinLibertineFont/LinLibertine_Re-4.4.1.ttf')
    );*/
    $font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA);
    $object->setFont($font, $size);
    return $font;
}

and

protected function _setFontBold($object, $size = 7)
{
    /*$font = \Zend_Pdf_Font::fontWithPath(
        $this->_rootDirectory->getAbsolutePath('lib/internal/LinLibertineFont/LinLibertine_Bd-2.8.1.ttf')
    );*/
    $font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA);
    $object->setFont($font, $size);
    return $font;
}

and

protected function _setFontItalic($object, $size = 7)
{
    /*$font = \Zend_Pdf_Font::fontWithPath(
        $this->_rootDirectory->getAbsolutePath('lib/internal/LinLibertineFont/LinLibertine_It-2.8.2.ttf')
    );*/
    $font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA);
    $object->setFont($font, $size);
    return $font;
}

You can see that I have replaced the default font to HELVETICA and its works fine for me.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top