Search code examples
phppdffontsphantomjsfont-face

phantomjs is inconsistent based on how it's called


I'm having weird and inconsistent issue with PhantomJs. I'm stuck on this since yesterday and I can't think straight anymore ! I searched, wrote code, but essentially failed for the time being...

A bit of context:

  • An app i'm working on (with Laravel 4) need to generate PDF reports. These reports are being converted with phantomJS. Until now, all worked as intended.
  • I was asked to add a new language support for this app : simplified chinese. All is well on the browser.
  • However, PhantomJS cannot generate PDF with Chinese support.

How the reports are being generated:

  • The PDF generation is triggered by a (Laravel) controller method.
  • A complete (Laravel) view is being saved as a temp HTML file (ie. PhantomJs will basically print in PDF a local file in this case).
  • PHP helps me generate the PhantomJS command line and executes it.
  • The (Laravel) controller response gets the PDF and put it to download.

Where's the problem?

The text in the generated reports is blank, when in simplified chinese. But this only impact the chinese characters, not the numbers. Here is a PDF sample of what I'm trying to describe.

I could verify that reports for other available languages worked well (english, french, german).

What's making this inconsistent?

While trying to debug this, I dumped the command line generated by PHP and tried it from my SSH prompt ; and it worked ! All characters were printed, event the chinese ones. As a prood of concept, I tried to "print in PDF" external pages via the CLI, like random wikipedia pages in simplified chinese, and all worked well.

Also, while inspecting the PDF file, I see that the font(s) embedded in the faulty reports are the server's defaults (Nimbus Sans family), which does not support simplified chinese. However, on the "good" reports, the fonts used are the one supporting simplified chinese (SimSun, which I've installed in my home -eg. ~/.fonts/ - and added to the fontconfig cache with fc-cache -vf).

What I have tried

  • Install CJK fonts (see above) on the server (SimSun, arphic-uming, Source Han Sans,...) ;
  • the original PHP code used the Symfony Process package, I've also tried to code a 10-liners with exec/shell_exec ;
  • Use @font-face (within the HTML file then in an external css file) with local font files
  • ... searching throught PhantomJS issue tracker and mailing-list, google...

I might have omitted some experiments, do not hesitate to make me elaborate ! I guess there's no out-of-the-box solution, but I hope I can gather at least some new ideas on the issue.

Edit: What's the command line look like:

As written above, PHP is generating the PhantomJS command line and executes it ; it always use absolute paths. When I'm testing the PDF conversion from the SSH prompt, it's always based on the PHP results.

/home/lucio/my/app/path/Printer/PhantomJs/bin/phantomjs --ignore-ssl-errors=true --load-images=true --local-to-remote-url-access=true --web-security=false --ssl-protocol=tlsv1 --output-encoding=utf8 /home/lucio/my/app/path/Printer/PhantomJs/bin/generate-pdf.js /home/lucio/my/app/path/storage/tmp/1426973396.html /home/lucio/my/app/path/storage/tmp/1426973396.pdf

Solution

  • So, after playing around with phantomjs and some CJK fonts, I've fixed my problem. However, this is not a total victory as I have absolutely no idea why this is fixed and what was the problem in the first place...

    Here is a list of things that seems to have concurrently helped to solve this :

    • I gave another shot to @font-face. The font declaration is directly injected into the view, inside <style></style>, and after the external stylesheet.
    • The font imported with @font-face is Noto Sans CJK Simplified Chinese, in OTF format (graciously provided by google). The font is not installed server-side ; it's only available with the other assets.
    • A <base> tag in the <head> section provides the base URL of all assets call.
    • Do not delete static html view files (For no obvious reason, I was afraid there was a conflict with concurrent access between the html view file loading and PhantomJS generating the PDF...).

    Eg.

    <!-- Directly after <head>: -->
    <base href="https://lucio.domain.info">        
    <!-- other meta declarations & co -->
    {{ HTML::style('/assets/css/reportcards/chinese.css') }}
    <style>
    @font-face {
        font-family:"NotoSans";
        src: url('/assets/fonts/Source_han_sans/NotoSansCJKsc-Regular.otf') format('opentype');
        font-style: normal;
        font-weight:300;
        }
    </style>