A recent Laravel project for an insurance company required the creation of a PDF policy document on the fly, containing data captured through the sales process. In the past, I’ve used mPDF but in this case, I wanted something that I could render in the browser to preview and style easily using TailwindCSS.
Browsershot, by the fantastic team at Spatie in Belgium, is a PHP library that uses a headless version of Google Chrome powered by Puppeteer to convert web pages to an image or pdf.
The syntax is very clean, for example, this single line will capture the web page and save it as a PDF file:
Installation instructions are provided within the Browsershot repository for Forge provisioned Ubuntu 16.04 servers, however, in this case, I am working with Plesk Obsidian on a server running CentOS 7 and the installation process is slightly different.
I’m noting the commands I used here for posterity, and because I’ll undoubtedly use Browsershot again and need the same instructions in the future:
sudo yum -y update
Install Node.js 12 LTS
curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash - sudo yum clean all && sudo yum makecache fast sudo yum install -y gcc-c++ make sudo yum install -y nodejs
sudo npm install -g puppeteer --unsafe-perm=true
Install Chromium dependencies
sudo yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc
One point to note is that using Browsershot in this environment means we need to disable sandboxing, so
->noSandbox() should be called, like this:
Browsershot::url('https://example.com') ->noSandbox() ...