Search code examples
symfonywkhtmltopdfovh

How to access /wkhtmltox/bin/wkhtmltopdf whithin an Symfony2 Application


I use KnpSnappyBundle to generate PDF in my Symfony2 application. It works fine on my local wamp with the configuration :

binary:     "\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\""

I trying with the application, but with a folder with wkhtmltox at the root with the configuration :

binary:     %kernel.root_dir%/../wkhtmltox/bin/wkhtmltopdf

It doesn't work. It get the following message :

The exit status code '127' says something went wrong:
stderr: "sh: wkhtmltox/bin/wkhtmltopdf: No such file or directory
"
stdout: ""
command: wkhtmltox/bin/wkhtmltopdf --lowquality 
'/tmp/knp_snappy595a4fa89a6676.24945632.html' 
'/tmp/knp_snappy595a4fa89a6a59.72414975.pdf'.

I'm using a OVH's webhosting server.


Solution

  • You could try to install wkhtmltopdf from https://github.com/h4cc/wkhtmltopdf-amd64

    Require the package for i386 with:

    composer require h4cc/wkhtmltopdf-i386 "0.12.3"

    And for amd64 with:

    composer require h4cc/wkhtmltopdf-amd64 "0.12.3"

    The binary will then be located at:

    vendor/h4cc/wkhtmltopdf-i386/bin/wkhtmltopdf-i386
    

    or

    vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64
    

    For latter, services.yml will look like this:

    knp_snappy:
        pdf:
            enabled:    true
            binary:     '%kernel.root_dir%/../vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'
            options:    []
    
        temporary_folder: '%kernel.cache_dir%/snappy'
    

    By installing it in the vendor folder you will no more be dependent on the machine you're running your app.