symfony + knp_snappy:gtk-WARNING无法打开显示

时间:2015-11-11 12:04:23

标签: symfony gtk pdf-generation

在symfony应用程序中,我使用knp_snappy生成PDF:

return new Response(
         $this->get('knp_snappy.pdf')->getOutput($url),
         200,
         array(
                 'Content-Type'          => 'application/pdf',
                'Content-Disposition'   => 'attachment; filename="file.pdf"'
                )
        );

但是我收到了这个错误:

The exit status code '1' says something went wrong:
stderr: "Loading pages (1/6)java version "1.7.0_79"

[more here ... cut for clarity]

OpenJDK Runtime Environment (IcedTea 2.5.6) (7u79-2.5.6-0ubuntu1.14.04.1)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

(wkhtmltopdf:12934): Gtk-WARNING **: cannot open display:
"
stdout: ""
command: wkhtmltopdf --lowquality 'http://symfony.dev/app_dev.php/application/reporter/674/inventory' '/tmp/knp_snappy5643248854b563.21161953.pdf'. 

请注意:

  • 给定的$url确实有效并且是绝对URL(因为它包含CSS的链接)。
  • 如果我使用 $this->get('knp_snappy.pdf')->getOutputFromHtml($html)代替$this->get('knp_snappy.pdf')->getOutput($url) 这一切都很好,但后来我无法加载CSS(这是正常的行为)

1 个答案:

答案 0 :(得分:0)

The point was that the requested URL was behind a firewall, so wkhtmltopdf was recieving a 302 access denied.

For knp_snappy users facing the same problem, the solution is really well explained here and here.

Hope that helps!