Skip to content

Instantly share code, notes, and snippets.

Last active May 25, 2023 13:38
Show Gist options
  • Save roydejong/7f36c1ded1dda627eeee9a0b332e9eff to your computer and use it in GitHub Desktop.
Save roydejong/7f36c1ded1dda627eeee9a0b332e9eff to your computer and use it in GitHub Desktop.
Set up wkhtmltopdf 0.12.6 on Ubuntu server + xvfb runtime + PHP integration

1. Install wkhtmltopdf

Find the download URL for the latest binary from the official Downloads page.

At the time of writing, this is v0.12.6, which was released on June 11, 2020.

Download the binary, and then install the package and its dependencies. For example:

sudo dpkg -i wkhtmltox_0.12.6.1-2.jammy_amd64.deb

This will give you an installation error - that's expected. To complete the install, run:

sudo apt install -f

2. Install xvfb package:

sudo apt-get install xvfb

3. Set up xvfb as service:


Create file /etc/systemd/system/xvfb.service and add the contents:

Description=Virtual Frame Buffer X Server

ExecStart=/usr/bin/Xvfb :42 -screen 0 1x1x24 -ac +extension GLX +render -noreset


Activate the service and start it:

sudo systemctl enable xvfb.service
sudo systemctl start xvfb.service
sudo systemctl status xvfb.service

4. Test if it works at all

Try to do a test run, and fix any errors you have on the command line before trying to get things to work in PHP:

wkhtmltopdf test.pdf

Expected output looks like this:

Loading pages (1/6)
Counting pages (2/6)                                               
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)

5. PHP integration

⚠️ Be sure to restart PHP before trying to get this to work.

Our library of choice for this is

Typically integration is really simple once wkhtmltopdf is up and running:

$pdf = new Pdf(/** your html or url goes here **/);

If you're having trouble, you can try setting xserver options manually:

$pdf = new Pdf([
    'commandOptions' => array(
        'procEnv' => array( 'DISPLAY' => ':0' ),
Copy link

This still works on Ubuntu 17.10. There's a wkhtmltopdf package in the official repositories too, but I haven't had any luck getting that to work.

Copy link

Installation is a little different now, from the looks of it.

Download the binary, then install it and its dependencies.

sudo dpkg -i wkhtmltox_0.12.5-1.xenial_amd64.deb
apt-get install -f

You can find the download URL for the latest binaries in the official GitHub repo:

You can verify installation and see installed version with:

$ wkhtmltopdf -V
wkhtmltopdf 0.12.5 (with patched qt)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment