Search code examples
symfonypdf-generationwkhtmltopdf

KnpSnappyBundle - stderr: "wkhtmltopdf: cannot connect to X server " stdout: ""


I'm trying to use the KnpSnappyBundle to create PDF files from twig templates.

I've followed this post to install wkhtmltopdf and it works when I do:

wkhtmltopdf http://www.google.com test.pdf

but when I try to create a PDF file from a controller:

$this->get('knp_snappy.pdf')->generateFromHtml(
$this->renderView('AcmePDFBundle:Default:template.html.twig'),
'../app/var/PDFfiles/PDF.pdf'
); 

I'm getting this error:

request.CRITICAL: RuntimeException: The exit status code '1' says something went wrong:
stderr: "wkhtmltopdf: cannot connect to X server
"
stdout: ""
command: /usr/bin/wkhtmltopdf --lowquality '/tmp/knp_snappy532ca2272fba44.73835084.html' '../app/var/files/PDF.pdf'. (uncaught exception) at /home/me/MyServer/project/vendor/knplabs/knp-snappy/src/Knp/Snappy/AbstractGenerator.php line 304 [] []

Any idea of how to solve it?

This is my configuration for KnpSnappyBundle:

knp_snappy:
    pdf:
        enabled:    true
        binary:     /usr/bin/wkhtmltopdf
        options:    []
    image:
        enabled:    false
        binary:     /usr/bin/wkhtmltoimage
        options:    []

Solution

  • Resolved installing a precompiled version. now you can get your version from here (inspired by this out of date answer ):

    http://wkhtmltopdf.org/downloads.html

    and changed my config.yml to:

    knp_snappy:
        pdf:
            enabled:    true
            binary:     /usr/local/bin/wkhtmltopdf
            options:    []
        image:
            enabled:    false
            binary:     /usr/local/bin/wkhtmltoimage
            options:    []
    

    and now it works!