Sebastian De Deyne
Designer & developer at Spatie

Generate pdfs with Google Chrome on a Forge provisioned server

This week I needed to export some charts generated with HTML & JavaScript as a pdf file. I already had implemented the charts on a webpage so I wanted a solution that allowed me to use my existing code for the pdfs.

Headless Chrome to the rescue! Chrome can run as a cli tool, and print a pdf file from a url. All I had to do was make some layout tweaks to make everything printer-friendly.

The command is pretty straight forward:

google-chrome --headless --disable-gpu \

That odd --disable-gpu flag isn’t really relevant but it’s necessary to use Chrome headless.

I wrote an Artisan command to generate a pdf from a set of results, and everything was up and running in my local environment in no time. Since Chrome ships with the CLI tool, all I had to do was write a little alias.

alias google-chrome="/Applications/Google\\ Chrome"

The next step was getting this to work on an actual server, in this case a freshly provisioned Forge server. There are a few guides for installing Chrome on Ubuntu, but here’s what ultimately did the trick for me.

sudo apt-get install libxss1 libappindicator1 libindicator7


# If there are errors running this, ignore them for now
sudo dpkg -i google-chrome*.deb

# This fixes the errors that appeared in the previous command
sudo apt-get install -f

That’s it! I created an environment variable to set the Chrome path in my application, and It Just Works™

If you enjoyed this post, you might be interested in my newsletter. I occasionally send a dispatch with personal stories, things I’ve been working on in the past month, and other interesting tidbits I come across online.