wkhtmltopdf with laravel homestead using xvfb-run

Lately i’ve been experimenting with generating a pdf from a html/css page and stumbled upon wkhtmltopdf. It’s a nice library full of options and I was impressed with the results.

I wanted to use the library in my laravel homestead environment and was running into some problems. Specifically:

QXcbConnection: Could not connect to display

After some digging I found: xvfb-run

xvfb-run is a simplified wrapper for xvfb which is a virtual framebuffer that can run right on your server. Since you don’t have a display or might not even have a video card running on your server, you’ll be able to “emulate” your screen viewing the page wkhtmltopdf is trying to generate a pdf from.

I was able to install both these libraries super easy:
sudo apt-get xvfb-run
sudo apt-get wkhtmltopdf

Then I was able to successfully get wkhtmltopdf running on my laravel homestead server running a command like this:

xvfb-run --auto-servernum --server-num=0 wkhtmltopdf --print-media-type --page-size letter http://yourwebsitehere.com yourdocument.pdf

Hope this helped save someone some time !