4

I am trying to generate a PDF that will contain Chinese characters using dompdf. Here is my code:

require('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
mb_internal_encoding('UTF-8');
def("DOMPDF_UNICODE_ENABLED", true);
$html = ' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
 <style>
     *{ font-family: DejaVu Sans, font-size: 12px;}
 </style> </head> <body>
 忠烈祠
  </body>
 </html>';
 $dompdf->load_html($html);
 $dompdf->render();
$output = $dompdf->output();
 $filename = 'a.pdf';
$path = $filename;
file_put_contents($path, $output);

The problem is the the generated PDF show only squares when i open it with chrome or adobe reader, but it looks ok in Mozilla firefox.

Any sugestions?

1 Answers1

2

First, move def("DOMPDF_UNICODE_ENABLED", true); above the require since the def function included with dompdf only defines the constant if it doesn't exist. When you include dompdf_config.inc.php that constant will be set at that time. Also, use define instead.

Second, you need a different font. The DejaVu fonts included with dompdf do not support CJK. For an overview read my answer to Dompdf and set different font-family.

If you do not currently have a font you might try looking up Firefly Sung. Whatever font you decide to use it should be TTF in order for dompdf to use it.

Here's an example using Firefly Sung via the @font-face CSS rule, no installation required:

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style>
    @font-face {
      font-family: 'Firefly Sung';
      font-style: normal;
      font-weight: 400;
      src: url(http://eclecticgeek.com/dompdf/fonts/cjk/fireflysung.ttf) format('truetype');
    }
    * {
      font-family: Firefly Sung, DejaVu Sans, sans-serif;
    }
  </style>
</head>

<body>
  <div>忠烈祠</div>
</body>

</html>
Community
  • 1
  • 1
BrianS
  • 13,284
  • 15
  • 62
  • 125
  • I tried the above html with dompdf0.7 but still the characters as boxes. I am testing in ubuntu 16.04 and PHP 7.0 with all the required extensions enabled. What am I doing wrong? – Suraj Oct 16 '16 at 15:53
  • @Suraj Dompdf does not include support all characters out of the box. Have you installed a font that supports your characters? – BrianS Oct 17 '16 at 14:20
  • I am using exactly the same code above. I think it does not require any installation as the font src url is external. Please advice. – Suraj Oct 18 '16 at 14:45
  • Is `allow_url_fopen` set to true? – BrianS Oct 18 '16 at 17:26
  • Yes allow_url_fopen is set to true. It seems to work now. But when I tried with japanese characters there are still boxes for few characters. I think the font included in src does not support all japanese characters. Also the pdf size is too large 15.7mb. Please check the html here http://54.153.85.27/testhtml1.html. The pdf generated url is http://54.153.85.27/document_test.pdf. Thanks. – Suraj Oct 19 '16 at 06:25
  • You have to specify a separate font file for each weight. I don't have a bold version of that font, but if you duplicate the `@font-face` declaration but change the weight to bold you will at least see the characters. As for PDF size, enable font subsetting. – BrianS Oct 20 '16 at 14:03
  • this answer does not work for me, PDF show only squares – Yevgeniy Afanasyev Apr 27 '18 at 02:52