2

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.

3
  • 1
    Change the font sizes as well. Also are you working with product images in your pdf? magpleasure.com/blog/… Sep 25, 2018 at 9:38
  • Default font was GnuFreeFont, I changed it to LinLibertineFont. But still no hope and it became 800kb but not 19kb, and I saw the link before you share. How to embed this font FONT_HELVETICA. When I include the line, the section giving error. And I'm trying to make this changes in Magento 2.2.5
    – M.Suman
    Sep 25, 2018 at 10:03
  • 1
    No need to include the Helvetica has its already available. You can directly add it. But according to the blog you are following is for magento 1 not for magento 2. Sep 25, 2018 at 10:17

2 Answers 2

9

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.

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.