1

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
lucio
  • 11
  • 4
  • Like [phantomjs screenshot font missing, boxes rendered instead](http://stackoverflow.com/questions/15029002/phantomjs-screenshot-font-missing-boxes-rendered-instead) or [Phantomjs renders black boxes on Azure Websites](http://stackoverflow.com/questions/22965791/phantomjs-renders-black-boxes-on-azure-websites)? – Artjom B. Mar 21 '15 at 22:48
  • Thanks for the links. I've already read them while trying to fix my problem, and it did not help. On my end, there's just no chinese text at all... – lucio Mar 22 '15 at 08:12

3 Answers3

0

The inconsistency suggests this is a path issue.

  1. Make sure that you use the same phantomjs version, if you have multiple versions installed. You can either use absolute paths for this or check which phantomjs from commandline and Laravel.

  2. Make sure you use the same path in both. You can get the full PATH using echo $PATH in commandline. You would then copy this path and prepend it to your command in Laravel: export PATH=<copied path>; phantomjs ....

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Thanks again for the suggestion. I'll try to tinker with the path, but tbh, i'm a bit doubtful on this because for all my tests, i always use absolute paths (eg. `/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`) – lucio Mar 22 '15 at 08:51
0

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>
lucio
  • 11
  • 4
0

I had a similar problem on Ubuntu 16.04 and I have solved it by installing Noto as a package

sudo apt-get install fonts-noto
Pawel Furmaniak
  • 4,648
  • 3
  • 29
  • 33