# Help with wkhtmltopdf in CakePHP

**URL:** https://discourse.cakephp.org/t/help-with-wkhtmltopdf-in-cakephp/6992
**Category:** Need Help
**Created:** [December 15, 2019, 11:13pm UTC](https://discourse.cakephp.org/t/help-with-wkhtmltopdf-in-cakephp/6992 "2019-12-15T23:13:54Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![djwaine](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@djwaine](https://discourse.cakephp.org/u/djwaine)
#### Post date: [December 15, 2019, 11:13pm UTC](https://discourse.cakephp.org/t/help-with-wkhtmltopdf-in-cakephp/6992/1 "2019-12-15T23:13:54Z")

</div>

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](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](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](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](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.

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 [google.co.uk](http://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?

Thanks for considering this.

---

<div class="post-metadata">

### Author: ![deanoj](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@deanoj](https://discourse.cakephp.org/u/deanoj)
#### Post date: [December 17, 2019, 5:01pm UTC](https://discourse.cakephp.org/t/help-with-wkhtmltopdf-in-cakephp/6992/2 "2019-12-17T17:01:36Z")

</div>

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: [https://book.cakephp.org/3/en/views/json-and-xml-views.html](https://book.cakephp.org/3/en/views/json-and-xml-views.html)

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.

---

<div class="post-metadata">

### Author: ![dakota](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dakota/32/1421_2.png) [@dakota](https://discourse.cakephp.org/u/dakota)
#### Post date: [December 18, 2019, 9:59am UTC](https://discourse.cakephp.org/t/help-with-wkhtmltopdf-in-cakephp/6992/3 "2019-12-18T09:59:47Z")

</div>

Do as @deanoj suggested and use [CakePDF plugin](http://github.com/friendsofcake/cakepdf).

@deanoj, CakePDF already has a PDF view class 🙂

---

<div class="post-metadata">

### Author: ![djwaine](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@djwaine](https://discourse.cakephp.org/u/djwaine)
#### Post date: [December 19, 2019, 11:19am UTC](https://discourse.cakephp.org/t/help-with-wkhtmltopdf-in-cakephp/6992/4 "2019-12-19T11:19:28Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dakota](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dakota/32/1421_2.png) [@dakota](https://discourse.cakephp.org/u/dakota)
#### Post date: [December 19, 2019, 1:44pm UTC](https://discourse.cakephp.org/t/help-with-wkhtmltopdf-in-cakephp/6992/5 "2019-12-19T13:44:52Z")

</div>

Any kind of error messages in logs?

---

<div class="post-metadata">

### Author: ![djwaine](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@djwaine](https://discourse.cakephp.org/u/djwaine)
#### Post date: [December 19, 2019, 7:53pm UTC](https://discourse.cakephp.org/t/help-with-wkhtmltopdf-in-cakephp/6992/6 "2019-12-19T19:53:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![djwaine](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@djwaine](https://discourse.cakephp.org/u/djwaine)
#### Post date: [December 21, 2019, 12:58pm UTC](https://discourse.cakephp.org/t/help-with-wkhtmltopdf-in-cakephp/6992/7 "2019-12-21T12:58:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![djwaine](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@djwaine](https://discourse.cakephp.org/u/djwaine)
#### Post date: [December 28, 2019, 8:55pm UTC](https://discourse.cakephp.org/t/help-with-wkhtmltopdf-in-cakephp/6992/8 "2019-12-28T20:55:38Z")

</div>

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.
