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.
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
Works for me. And its much easier than the other link suggested.
I wanted to add – if anyone is experiencing an error
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.
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!
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.
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.