Search code examples
phputf-8dompdf

Displaying Japanese Characters using DOMpdf


Problem:

I want to generate a pdf using DomPDF that includes Japanese text. In order to do so I know that I need a font that supports Japanese characters and to load the font into DomPDF.
I have read the UnicodeHowTo of DomPDF and several similar questions on SO but I can not seem to get it to work. I get ? at the place where the Japanese characters should be.

In my dompdf config I have set def("DOMPDF_UNICODE_ENABLED", true); and it shows true in the admin interface as well. The font I use (I have tried several) should support japanese characters and is loaded in DomPDF using the load_font.php script. I do have to note for the sake of completeness that generating the font files gives me 2 identical warnings

Warning: strftime(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in ..

Minimal (not) working example:

Relevant html stored in PHP variabe $template:

<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<META http-equiv="X-UA-Compatible" content="IE=8">
<TITLE>Test voucher</TITLE>
<STYLE type="text/css">


@font-face {
      font-family: \'mgenplus\';
      font-style: normal;
      font-weight: 400;
      src: url(dompdf/fonts/rounded-mgenplus-1c-regular.ttf) format(\'truetype\');
 }
.ft0{font: 14px;line-height: 16px;}
*{ font-family: mgenplus !important;}
</STYLE>
</HEAD>

<BODY>
<P>ねん だい かい にほんごのうりょくしけん</P>
<!-- .... -->

Relevant PHP:

require_once("dompdf/dompdf_config.inc.php");
$template = mb_convert_encoding($template, 'HTML-ENTITIES', 'UTF-8');
$dompdf = new DOMPDF();
$dompdf->load_html($template, 'UTF-8');
$dompdf->render();
$dompdf->stream("test.pdf",array('Attachment'=>0));

If anybody has a suggestion or see if I missed something, it would be appreciated!


Solution

  • Never mind, I found out why it did not work and I will post it here for anybody facing the same problem.

    The problem was in this piece:

    @font-face {
          font-family: \'mgenplus\';
          font-style: normal;
          font-weight: 400;
          src: url(dompdf/fonts/rounded-mgenplus-1c-regular.ttf) format(\'truetype\');
     }
    .ft0{font: 14px;line-height: 16px;}
    *{ font-family: mgenplus !important;}
    

    I had to remove the @font-face block, it is not needed since you load the font from DomPDF and not from the file. The line that caused all the trouble was .ft0{font: 14px;line-height: 16px;} it sets the font-family to the browser default apparently and the overwrite afterwards is not taken into account by DomPDF (no support for !important?).

    Changing the line to .ft0{font-size: 14px;line-height: 16px;} fixed my problem.