Skip to content

Instantly share code, notes, and snippets.

@zepgram
Created June 17, 2022 13:43
Show Gist options
  • Save zepgram/74fca9ffd358bb44e4875c6378b2e609 to your computer and use it in GitHub Desktop.
Save zepgram/74fca9ffd358bb44e4875c6378b2e609 to your computer and use it in GitHub Desktop.
PDF size reduction for Magento 2
<?php
declare(strict_types=1);
namespace MyNamespace\Sales\Model\Order\Pdf;
use Magento\Sales\Model\Order\Pdf\AbstractPdf as MagentoAbstractPdf;
/**
* Drastically reduce size of PDF by changing fonts
*/
abstract class AbstractPdf extends MagentoAbstractPdf
{
protected function _setFontRegular($object, $size = 7)
{
$font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_TIMES);
$object->setFont($font, $size);
return $font;
}
protected function _setFontBold($object, $size = 7)
{
$font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_TIMES_BOLD);
$object->setFont($font, $size);
return $font;
}
protected function _setFontItalic($object, $size = 7)
{
$font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_TIMES_ITALIC);
$object->setFont($font, $size);
return $font;
}
}
<?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\AbstractPdf" type="MyNamespace\Sales\Model\Order\Pdf\AbstractPdf" />
</config>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment