CakePHP: WKHTMLTOPDF didn’t return any data

Yesterday I was trying to generate some PDFs using CakePHP and awesome CakePDF plugin. After some reading around and testing I concluded WkHtmlToPdf should be the best PDF engine to use.

So I installed it on my Debian system running

sudo apt-get install wkhtmltopdf

It installed successfully and I was able to execute the example code from wkhtmltopdf website without any errors

wkhtmltopdf https://google.com google.pdf

But when I was trying to run it from CakePHP, I was greeted by an error message “WKHTMLTOPDF didn’t return any data”.

Stack trace was showing this:

CakePdf\Pdf\Engine\WkHtmlToPdfEngine->output
ROOT/vendor/friendsofcake/cakepdf/src/Pdf/CakePdf.php, line 257

Closer inspection showed the actual error returned from wkhtmltopdf command was:

wkhtmltopdf: cannot connect to X server

So after some fiddling around I was able to find a working solution – you need to install program called xvbf:

sudo apt-get install xvfb

And modify the output() method’s line calling the wkhtmltopdf executable, located in cakepdf/src/Pdf/Engine/WkHtmlToPdfEngine.php

Replace the line:

$content = $this->_exec($this->_getCommand(), $this->_Pdf->html());

With the following:

$content = $this->_exec('xvfb-run -a -s "-screen 0 640x480x16" ' . $this->_getCommand(), $this->_Pdf->html());

All done 🙂

Note: you cannot just add this part to the “binary” parameter in the config without modifications on the class itself, because it will fail with “wkhtmltopdf binary is not found or not executable” message.

6 thoughts on “CakePHP: WKHTMLTOPDF didn’t return any data

  1. Carlos Almeida Jr December 18, 2015 / 12:59 pm

    Hi,

    It’s a good solution, but the rendered file become different if compared with the wkhtmltopdf “old”.

    In this* discussion there are a better solution: use the newest version of wkhtmltopdf (0.12.2).

    In debian like distros:
    wget http://download.gna.org/wkhtmltopdf/0.12/0.12.2/[choose a file].deb
    sudo dpkg -i /[choosed file].deb

    *http://stackoverflow.com/questions/9604625/wkhtmltopdf-cannot-connect-to-x-server

    Best regards

  2. Sharif Khan December 22, 2015 / 11:30 am

    Works for me. And its much easier than the other link suggested.

  3. Cakey February 23, 2016 / 9:31 am

    I wanted to add – if anyone is experiencing an error

    error while loading shared libraries: libjpeg.so.8: cannot open shared object file: No such file or directory

    on Debian 8, You need to download and install this package: http://ftp.us.debian.org/debian/pool/main/libj/libjpeg8/libjpeg8_8d1-2_amd64.deb
    I could not get wkhtmltopdf working otherwise and installing libjpeg-dev as suggested elsewhere did not help.

    I finally managed to get everything working, after struggling with this the whole morning. At first the wkhtmltopdf did not work on the server because it didn’t have xserver running, and then there was problems on my pc due to missing libjpeg.so.8.

  4. Cosmin October 21, 2016 / 8:45 am

    I tried to add that in binary ‘xvfb-run -a -s “-screen 0 640x480x16/usr/bin/wkhtmltopdf’ but is not working… how should it look ?

    ‘engine’ => ‘CakePdf.WkHtmlToPdf’,
    ‘binary’ => ‘?’,

    Thanks,the article is great!

    • Madars October 21, 2016 / 8:51 am

      You can’t specify this command as “binary” in the config file, because the code checks if the binary is executable and fails if you try to do it that way.

      You have to either modify the code in the “WkHtmlToPdfEngine.php” file as suggested in the article, or use the method CARLOS suggests in the first comment, which, by the way, seems like a better way to solve this problem.

  5. B. Wright August 4, 2017 / 10:11 pm

    Thank you so much for publishing this article. I spent the better part of the day trying to find some workaround for this same issue.

Leave a Reply

Your email address will not be published. Required fields are marked *