Testing PDF creation with headless chrome and PHP

I had the task the other day to use a headless chrome to generate PDF files from websites in a PHP app.

The plan was to use chrome-php with a headless chrome to generate the PDF.

Usually you would install chrome/chromium on a linux server via apt and just run chrome from the PATH with chrome.
Since i was on shared hosting i was not sure if this was possible since i was not allowed to run apt commands.
So i tried to use Puppeteer which ships a headless chrome executable and use just this directly.
I installed Puppeteer with npm locally and uploaded the chrome executable to the shared hosting.
Puppeteer will place the headless chrome in the .cache dir in your home directory, f.e.:

~/.cache/puppeteer/chrome/linux-113.0.5672.63/chrome-linux64/chrome

Upload:

scp -r ~/.cache/puppeteer/chrome/linux-113.0.5672.63/chrome-linux64 me@sharedhosting:/usr/home/test

I also created a nice little command line tool to create the pdf with chrome-php.
I used minicli which is a “minimalist, dependency-free framework for building CLI-centric PHP applications”.
So you can create cli commands fast and without all the clutter, exactly what i needed for this small test.
You can find the code on Github: https://github.com/ivoba/chrome-php-minicli
The command is here: https://github.com/ivoba/chrome-php-minicli/blob/main/command.php

Then i tried to generate the pdf:
CHROME_PATH="/usr/home/test/chrome" php command.php pdf url=https://ivo-bathke.name

However this failed because the server lacked required dependencies to run the chrome executable.
You can check this by running this command:

me@sharedhosting:/usr/home/test$ ldd chrome-linux64/chrome | grep not
libatk-1.0.so.0 => not found
libatk-bridge-2.0.so.0 => not found
libxkbcommon.so.0 => not found
libatspi.so.0 => not found
libXcomposite.so.1 => not found
libXrandr.so.2 => not found
libgbm.so.1 => not found

Ok, so it wont work with the uploaded chrome executable.
What a pity but still a finding.

In the end i contacted the support and fortunatly they were willing and able to install chromium via apt. Nice!

Eventually it worked with:
CHROME_PATH="chromium" php command.php pdf url=https://ivo-bathke.name

Pdf created :)