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:
How the reports are being generated:
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
exec
/shell_exec
;@font-face
(within the HTML file then in an external css file) with local font filesI 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
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 :
@font-face
. The font declaration is directly injected into the view, inside <style></style>
, and after the external stylesheet.@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.<base>
tag in the <head>
section provides the base URL of all assets call.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>