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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测