Solving invoice PDF Arabic support problem in Magento 2

Magento 2 has a couple issues with PDF when a language is Arabic:

Separated characters problem in Arabic language

This problem occurs in invoice/shipment/credit memo pdf when a language is Arabic. Other right to left languages might have it too. Below is an image which shows where the problem is:

Actually it’s not a Magento 2 bug. In Magento 2 all PDFs is drawn by using the Zend PDF library. Unfortunately this library hasn’t RTL support.

For now, the only way to fix this problem is to replace Zend PDF library with TCPDF and redraw all PDF documents from scratch. Luckily, there are some Magento 2 PDF extensions, which are generating PDFs with TCPDF.

We have one too. Our Magento 2 PDF Invoice Pro  is drawing PDFs with TCPDF library and has RTL support. Here is an invoice PDF example, generated with our extension. For your inspection you can download it here.

Rectangles instead of characters in PDF

This issue occurs in PDF when the fonts, used to draw PDF, don’t have your language characters. In the latest Magento 2.3.4 version this problem is already fixed in Arabic language by changing the font from LinLibertine to FreeSerif.  But other languages like Chinese, Thai, Hindi still might have this issue. Luckily this issue is fixable and can be fixed by changing a font.

How to change a font in Magento 2 invoice PDF

The fonts of PDFs in Magento 2 are set in:

vendor/magento/module-sales/Model/Order/Pdf/AbstractPdf.php

To don’t get a lost, I divided this instruction into three steps:

Get a font with your language support

Magento 2 is using Zend PDF library. This library can handle only True Type Fonts (.tff) with table version 3. Table version 4 isn’t supported. So your font file has to meet these requirements and support your language characters.

You can download the “Traditional Arabic” font here. This font will be used in the following example steps.

The font .ttf files have to be uploaded to your server in the following directory:

lib/internal/traditional-arabic/

Overwrite the file

Because the methods, which we need to modify, are protected, we can’t create a plugin. The only way is to override them. So we need to create a custom module and override required files. Create the following files with contents.

app/code/Magetrend/CustomPdfFont/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Magetrend_CustomPdfFont',
    __DIR__
);
app/code/Magetrend/CustomPdfFont/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magetrend_CustomPdfFont" schema_version="2.0.0" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>
app/code/Magetrend/CustomPdfFont/etc/di.xml
<?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="Magetrend\CustomPdfFont\Model\Invoice" />
</config>
app/code/Magetrend/CustomPdfFont/Model/Invoice.php
<?php
namespace Magetrend\CustomPdfFont\Model;

class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
    protected function _setFontRegular($object, $size = 7)
    {
        $font = \Zend_Pdf_Font::fontWithPath(
            $this->_rootDirectory->getAbsolutePath('lib/internal/traditional-arabic/trado.ttf')
        );
        $object->setFont($font, $size);
        return $font;
    }

    protected function _setFontBold($object, $size = 7)
    {
        $font = \Zend_Pdf_Font::fontWithPath(
            $this->_rootDirectory->getAbsolutePath('lib/internal/traditional-arabic/tradbdo.ttf')
        );
        $object->setFont($font, $size);
        return $font;
    }

    protected function _setFontItalic($object, $size = 7)
    {
        $font = \Zend_Pdf_Font::fontWithPath(
            $this->_rootDirectory->getAbsolutePath('lib/internal/traditional-arabic/trado.ttf')
        );
        $object->setFont($font, $size);
        return $font;
    }
}

Change the fonts

To change the fonts, open Invoice.php file and update files locations. They are marked in red.

app/code/Magetrend/CustomPdfFont/Model/Invoice.php
<?php
namespace Magetrend\CustomPdfFont\Model;

class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
    .....

    $this->_rootDirectory
        ->getAbsolutePath('lib/internal/traditional-arabic/trado.ttf')
        
    ....


    $this->_rootDirectory
        ->getAbsolutePath('lib/internal/traditional-arabic/tradbdo.ttf')


    ....

    $this->_rootDirectory
        ->getAbsolutePath('lib/internal/traditional-arabic/trado.ttf')

    ....

}

If all steps are completed, run standard Magento 2 commands to install new extension:

php bin/magento setup:upgrade;
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy

That’s all for this article. I hope it helps you to solve language issues in Magento 2 PDFs.

Leave a Comment