Thursday, March 5, 2015

Change Fonts and Reduce File Size for PDF Invoice in Magento

We often meet a situation where we need to print an Order Invoice for a particular customer.

If you ever noticed, the size of a PDF Invoice file is roughly 1.5Mb. It is not a problem if you only need to download a single file.
But in a situation where we need to download 10, 20 or 50 files in one go; the total download size will increase dramatically.
This becomes a headache especially when you need to get these files urgently.

We found a solution to reduce the size of PDF Invoice files. The reason why the PDFs are so large by default is because magento is using the following font

/lib/LinLibertineFont/LinLibertineC_Re-2.8.0.ttf

which is attached to every file. This font file by itself is 1.5MB. We can replace these fonts with Zend_Pdf default fonts. This way we will get smaller file size for our PDFs. In order to do this, just open the file:

app/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php

find lines

$font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertineC_Re-2.8.0.ttf');

and replace them with

$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

The font of Pdf files will be as readable, though not as beautiful as before. And the size of file will be reduced by minimum 5 times.

If you want to increase the text size on the PDF Invoice for billing and shipping addresses, it can be done by modifying exact same file. Simply change the lines:

protected function _setFontRegular($object, $size = 7)
protected function _setFontBold($object, $size = 7)
protected function _setFontItalic($object, $size = 7)
to
protected function _setFontRegular($object, $size = 9)
protected function _setFontBold($object, $size = 9)
protected function _setFontItalic($object, $size = 9)

accordingly.

As you can see we simply increased the default size value. In order to preserve your modifications from future Magento upgrades you need to copy:

app/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php
into
app/local/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php

and enter your changes there.

Welcome for your queries!!

No comments:

Post a Comment