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 http://google.co.uk /Users/me/Sites/App/src/pdffolder/file.pdf’)
The resulting file is owned by me and belongs to group _www
Conclusion.
Wkhtmltopdf works from shell and from shell called within a php file within a browser
User of command line and php from browser can execute wkhtmltopdf and save files to the pdffolder
CakePHP can execute wkhtmltopdf AND save files to the pdffolder eg pdf from google.co.uk) but NOT if the webpage asked for is from a view.ctp
Users:
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.
Permissions:
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?
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.
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.
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
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.