Question

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:    []
Was it helpful?

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!

OTHER TIPS

You must copy it into directory : /usr/local/bin, make sur it's executable and add symlink of wkhtmltopdf.sh like :

1- the command :

sudo apt-get install wkhtmltopdf

2 - insert the binary in directory /usr/bin so the browser can't have permission to execute in this directory. You must copy the wkhtmltopdf.sh to directory /usr/local/bin cause the browser have permission in this directory like:

sudo cp /usr/bin/wkhtmltopdf.sh /usr/local/bin/wkhtmltopdf.sh

3 - After make sur the binary have permission of execution like :

sudo chmod a+x /usr/local/bin/wkhtmltopdf.sh

4 - so now you can test, it's work like:

/usr/local/bin/wkhtmltopdf.sh http://www.google.com google.pdf

it make download the pdf in the current directory in your terminal

5 - Optional now you can add symlink in your directory /usr/local/bin like

ln -s /usr/local/bin/wkhtmltopdf.sh /usr/local/bin/wkhtmltopdf

6 - copy to the /usr/bin/wkhtmltoimage into /usr/local/bin/wkhtmltoimage like:

sudo cp usr/local/wkhtmltoimage usr/local/bin/wkhtmltoimage

8- make sur this is executable too:

sudo chmod a+x /usr/local/bin/wkhtmltoimage

7 - change the directory of binary in configuration of symfony in config/packages/knp_snappy.yaml :

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

I Hope it's help you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top