1

When working with Pdf invoice, I meet a problem about encoding Japanese font. I'm going to add a Japanese font(Gothic) to Pdf invoice, creditmemo, etc. What should I do?

We may rewrite abstract \Magento\Sales\Model\Order\Pdf\AbstractPdf class? Because there are some "hard code" functions to set fonts for Pdf.

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

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

There are some similar issues here and here.

2

According to the code it seems like the font can be set dynamically, see this code from the drawLineBlocks method:

$font = \Zend_Pdf_Font::fontWithPath($column['font_file']);
$page->setFont($font, $fontSize);

The $column['font_file'] comes from the $draw array given in the method parameters.

So what you could try is to declare a plugin on this method

app/code/Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <type name="Magento\Sales\Model\Order\Pdf\AbstractPdf">
        <plugin name="add_font_to_pdf" type="Vendor\Module\Plugin\AddFontToPdf" sortOrder="10" disabled="false"/>
    </type>
</config>

Then in app/code/Vendor/Module/Plugin/AddFontToPdf.php you can do something like this:

<?php

namespace Vendor\Module\Plugin;
class AddFontToPdf
{
    public function beforeDrawLineBlocks($subject, $page, array $draw, array $pageSettings = [])
    {
        $newDraw = $draw;
        // Update the $draw array to add your font
        return [$page, $newDraw, $pageSettings];
    }

}

NB: you will need to replace the comment with the code that will let you add the font to the draw array yourself obviously.

  • Which font is best suitable for PDFs? – Sejal Shah Jun 14 '17 at 12:29
  • i have did it but not working for me. – Sarfaraj Sipai Oct 30 '18 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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