KnpLabs / snappy

PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage

Home Page:https://knplabs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with japanese characters

eved42 opened this issue · comments

commented

Hello,

I use your bundle (v 1.5) on a Symfony 3.4 project. I manage the generation of a PDF into multiple languages. The user selects a language and the PDF is generated with the corresponding translations.

I have no problem with accented characters (French, Spanish...) or with cyrillic characters (Russian, Ukrainian...) but my Japanese translations (kanjis) are not displayed at all whereas they are displayed correctly in the browser (in the same font).

  • If I used "TT Norms" font : no characters are displayed.
  • If I use "Open Sans" font : I have bordered squares instead of kanjis.

I have no problem in the browser with these two fonts, characters are displayed properly.

config.yml

knp_snappy:
    pdf:
        enabled: true
        binary: '/bin/wkhtmltopdf'
        options:
           encoding: utf-8
           print-media-type: true
           page-size: A4
           dpi: 300
           image-quality: 80
           margin-left:   15mm
           margin-right:  15mm
           margin-top:    50mm
           margin-bottom: 25mm

Controller :

use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;

$html = $this->renderView('public/products/pdf/content.html.twig', array(
    'lang'  => $lang
));

return new PdfResponse(
    $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
    'product.pdf'
);

I added <meta charset="UTF-8"> and <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> inside the <head> of my Twig view.

If I do a locale -a on my server, here is the result :

C
C.UTF-8
fr_FR.utf8
POSIX

Examples of text I want to display :

  • 参照
  • 製品ライン
  • アイテム
  • コード化

Do you have an idea of the problem ? I don't if it's a problem of encoding or if it comes from the font or another thing.

Hello,
It's probably due to a missing font server side.
Are you sure the font you need to use to display Japanese characters is installed on the server/container executing your pdf generation code?

commented

I notice that, no matter which font I set (Arial, Century Gothic, ...), the style of the kanjis doesn't change on the browser. They seem to be displayed as is. Maybe they are displayed like symbols ? So maybe wkhtmltopdf doesn't display symbols ?

I need a little bit more time to check on that and I'm lacking of that at the moment.
In the meantime can you send me an example HTML so that I can try to reproduce the issue and debug it?

I just made a quick test on an Ubuntu container and I was able to correctly print the characters you put on your previous comment.
If you have a Ubuntu server/container as well you can try install the following packages (fonts):

  • fonts-wqy-microhei
  • ttf-wqy-microhei
  • fonts-wqy-zenhei
  • ttf-wqy-zenhei

It should fix your issue 😉

commented

Thank you, I also looked on my side and I found the same thing as you, there are missing fonts on my server. I'm gonna ask to our server admin to install the fonts.

Interesting topic : https://stackoverflow.com/questions/25833954/wkhtmltopdf-and-chinese-characters

EDIT:
After installing them, what should I write in my CSS file ? Do I need to write a font-family: ttf-wqy-microhei; ?

I think that defining sans-serif or system, at least as a fallback font, should be enough.
I'm not a CSS master though so better making some tests 😉

commented

Ok, it works! The kanjis are a little bit pixelized but they appear. Thank you for your help. 👍

In my CSS file, I just wrote font-family: "TT Norms", sans-serif;

image