Magento – Hebrew words in PDF invoice are reversed and currency symbol not showing

fontsinvoicemagento-2.1.3magento2pdf

I'm using magento 2 CE 2.1.3

When I'm trying to prin PDF invoice the hebrew words are reversed, for example:
םולש instead of שלום.
And also the currency symbol (NIS ) is not showing.

enter image description here
I have seen this issue in several places:
https://stackoverflow.com/questions/25911650/rupee-symbol-not-rendering-properly-on-invoice-pdf-in-magento

How can I display `₹` properly in invoice pdf without changing core files

but these solutions doesn't work on magento 2.
The closest thing I have found to change the font in the PDF is this:
Adding new fonts to Pdf invoice Magento 2?

but I couldn't understand completely what I should place in the AddFontToPdf.php file…

I'll be glad for some help here..

Thanks.

Best Answer

For this answer: Adding new fonts to Pdf invoice Magento 2?, we can follow

app/code/Vendor/PdfFont/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">
    <type name="Magento\Sales\Model\Order\Pdf\AbstractPdf">
        <plugin name="add_font_to_pdf" type="Vendor\PdfFont\Plugin\AddFontToPdf" sortOrder="10" disabled="false"/>
    </type>
</config>

app/code/Vendor/PdfFont/Plugin/AddFontToPdf.php

<?php

namespace RoyalCopenhagen\PdfFont\Plugin;

use Magento\Framework\App\Filesystem\DirectoryList;

class AddFontToPdf
{
    /**
     * @var \Magento\Framework\Filesystem\Directory\ReadInterface
     */
    protected $_rootDirectory;

    /**
     * AddFontToPdf constructor.
     * @param \Magento\Framework\Filesystem $filesystem
     */
    public function __construct(
        \Magento\Framework\Filesystem $filesystem
    )
    {
        $this->_rootDirectory = $filesystem->getDirectoryRead(DirectoryList::ROOT);
    }

    public function beforeDrawLineBlocks($subject, $page, array $draw, array $pageSettings = [])
    {
        foreach ($draw as $key => $values) {
            if(isset($values['lines'])) {
                $lines = $values['lines'];
                foreach ($lines as $k => $v) {
                    foreach ($v as $k1 => $v1) {
                        $v[$k1]['font_file'] = $this->_rootDirectory->getAbsolutePath('lib/internal/Meiryo/Meiryo.ttf');
                        $lines[$k][$k1] = $v[$k1];
                    }
                }

                $draw[$key]['lines'] = $lines;
            }

        }
        // Update the $draw array to add your font
        return [$page, $draw, $pageSettings];
    }
}

We need to get the font from: $this->_rootDirectory->getAbsolutePath('lib/internal/Meiryo/Meiryo.ttf').
However, this way is for drawing item, not for shipping address info. Because the shipping address part used the protected functions to set font style.

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

protected function _setFontRegular()

protected function _setFontBold()

protected function _setFontItalic()
Related Topic