Help with wkhtmltopdf in CakePHP

Hi, please could you help me solve a problem using wkhtmltopdf?
I’m using CakePHP 3.7 on macOs. I’m running in developer mode (ie with bin/cake server). My app is in /Users/me/Sites/ rather than /Library/WebServer.

If I type this into command line, it makes a perfect pdf of the view.ctp and saves it in under 2 seconds:
wkhtmltopdf http://localhost/controller/action/id /Users/me/Sites/App/src/pdffolder/file.pdf
The resulting file is owned by me and belongs to group _www

If I point my browser to a (non CakePHP) php file which contains this, it works just fine too:
exec(‘wkhtmltopdf http://localhost/controller/action/id /Users/me/Sites/App/src/pdffolder/file.pdf’)
The resulting file is owned by _www and belongs to group _www

But if I call this from inside a controller action in CakePHP it hangs at 10% complete forever:
exec(‘wkhtmltopdf http://localhost/controller/action/id /Users/me/Sites/App/src/pdffolder/file.pdf’)

However, if I ask it to make a pdf of the google webpage from inside a controller, it works and saves fine:
exec(‘wkhtmltopdf /Users/me/Sites/App/src/pdffolder/file.pdf’)
The resulting file is owned by me and belongs to group _www


  1. Wkhtmltopdf works from shell and from shell called within a php file within a browser
  2. User of command line and php from browser can execute wkhtmltopdf and save files to the pdffolder
  3. CakePHP can execute wkhtmltopdf AND save files to the pdffolder eg pdf from but NOT if the webpage asked for is from a view.ctp

If run from CakePHP or the command line, the user is me (the user logged in to mac).
This user is not in _www group.

pdffolder is owned by me and belongs to group _www (both with read/write/execute permissions)

Can anyone help? Why does wkhtmltopdf work with everything except from within CakePHP to make a pdf of a CakePHP view?

Thanks for considering this.

First off make sure you have no issues running wkhtmltopdf on the command line. Past versions just haven’t worked for me period.

Once you are happy your version works for you - check this plugin out: friendsofcake/cakepdf

I ended up with a config like this:

// cake pdf
    'CakePdf' => [
        'engine' => [
            'className' => 'CakePdf.WkHtmlToPdf',
            'binary' => 'C:\software\wkhtmltopdf\bin\wkhtmltopdf.exe',
            'options' => [
                'print-media-type' => false,
                'outline' => true,
                'dpi' => 96,
                'zoom' => 1.25
        'margin' => [
            'bottom' => 0,
            'left' => 0,
            'right' => 0,
            'top' => 0
        'orientation' => 'portrait',
        'download' => false

Then you want to check this out and create your own PDF view:

Also, make you you define a cut down PDF version of the layout. You don’t want to be loading javascript or anything like that - perhaps that’s the reason you are getting a hanging controller.

Do as @deanoj suggested and use CakePDF plugin.

@deanoj, CakePDF already has a PDF view class :slight_smile:

1 Like

Thanks. I can get the CakePdf plugin to work with DomPDF, but the results are not fantastic. It does not work with wkhtmltopdf though. Having seen how well wkhtmltopdf renders a CakePhp view when executed from a php page or command line, I’m really keen to use this in my CakePhp project. I just can’t work out why it fails when I try using it from within the project.

Any kind of error messages in logs?

No errors appear while the development server is frozen. If I close it then restart it this appears:
wkhtmltopdf[17736:3185865] __agent_connection_block_invoke_2: Connection error - Connection invalid

Oh, actually I’ve been trying for another 2 days and no error messages are generated; just freezes at 10%.

OK, solved it. Well, got it working anyway. I ran the app having set-up the app folder as localhost without using the dev server (bin/cake server) and wkhtmltopdf worked fine. Must be some problem running it in dev server mode.

1 Like