0

I have a website built on WordPress within I made a plugin to extend some specific functionality. One is to export a grid with custom header from a html content to PDF. I have to use MPdf (version 8) and NotoSansJP font which, I understand, has 75k+ glyphs. Anyway, I tried with multiple fonts and the result is almost the same. Some characters are shown as little squares. If I'm not forcing a specific font, everything is fine and the text is showing nice but the client is very unhappy with the font. Here is the code:

$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
        $fontDirs = $defaultConfig['fontDir'];

        $defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
        $fontData = $defaultFontConfig['fontdata'];

        if(!file_exists($filepath)) {
            $stock = $this->getReport();

            $pdf = new \Mpdf\Mpdf([
                'mode' => 'utf-8',
                'format' => 'A3-L',
                'margin_left' => 5,
                'margin_right' => 5,
                'margin_top' => 5,
                'margin_bottom' => 5,

                'fontDir' => array_merge($fontDirs, [
                    ASSETS_DIR ."fonts/NotoSansJP",
                ]),

                'fontdata' => $fontData + [
                    'Noto Sans JP' => [
                        'R' => 'NotoSansJP-Regular.otf',
                        //'I' => 'Meiryo-Italic.ttf',
                    ]
                ],
                'default_font' => 'Noto Sans JP',
                'languageToFont' => new CustomLanguageToFontImplementation()
            ]);

            $pdf->autoScriptToLang  = true;
            $pdf->autoLangToFont  = true;
            $pdf->showImageErrors = true;
            $pdf->allow_charset_conversion = true;
            $pdf->charset_in='UTF-8';
            $pdf->SetHTMLHeader();
            $pdf->SetHTMLFooter();
            $pdf->WriteHTML($this->getHtmlTemplate());

            $pdf->Output($filepath, 'F');

            return $filepath;

Also there is another class for language implementation:

class CustomLanguageToFontImplementation extends \Mpdf\Language\LanguageToFont {
    public function getLanguageOptions($llcc, $adobeCJK) {
        if ($llcc === 'ja') {
            return [false, 'Noto Sans JP'];
        }

        return parent::getLanguageOptions($llcc, $adobeCJK);
    }
}

Any suggestion?

Thanks in advance!

pictoru
  • 722
  • 6
  • 17

0 Answers0