dongxuandong2045 2015-03-21 22:26
浏览 185
已采纳

phantomjs根据它的调用方式不一致

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
  • 写回答

3条回答 默认 最新

  • douchen4534 2015-03-22 19:31
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器