Environment:
- PHP 8.3
- Laravel 11
- Ubuntu 24.04 LTS
- NGINX
- Node (v18.19.1) (latest for today)
- Spatie/Browsershot (Puppeteer, Chrome 128.0.6613.119)
Issues:
- Spatie wanting me to install outdated Node/Puppeteer versions (https://spatie.be/docs/browsershot/v4/requirements). And we all want the latest.
- Even if we follow Spatie instructions, we get Permissions errors (www-data user cannot run Chrome or has no permissions to create temp directories).
- Other Chrome versions (chromium-browser or non-snap
.deb
installations) methods do not work (php8.3-fpm does not belong to Snap cgroup or other issues with permissions). - Since we want to use the latest soft versions, some packages are missing.
1
How to make it work (“This is the way”):
-
Install LEMP. Just as you usually do.
-
Create a real www-data user with
/home/www-data
directory.
- Crete directory:
sudo mkdir /home/www-data
- Stop NGINX and php-fpm:
sudo service nginx stop
,sudo service php8.3-fpm stop
- Change www-data user settings:
sudo usermod -d /home/www-data www-data -s /bin/bash
- Maybe kill user related processes
kill {proocess_id}
- Start NGINX and php-fpm:
sudo service nginx start
,sudo service php8.3-fpm start
-
Install Laravel into
/home/www-data/laravel
directory. -
Install Puppeteer globally.
- Install node and npm:
sudo apt install nodejs
,sudo apt install npm
- Install Puppeteer:
npm install puppeteer --location=global
- Install related soft:
gconf-service, libasound2, libatk1.0-0, libc6, libcairo2, libcups2, libdbus-1-3, libexpat1, libfontconfig1, libgbm1, libgcc1, libgdk-pixbuf2.0-0, libglib2.0-0, libgtk-3-0, libnspr4, libpango-1.0-0, libpangocairo-1.0-0, libstdc++6, libx11-6, libx11-xcb1, libxcb1, libxcomposite1, libxcursor1, libxdamage1, libxext6, libxfixes3, libxi6, libxrandr2, libxrender1, libxss1, libxtst6, ca-certificates, fonts-liberation, libnss3, lsb-release, xdg-utils, wget, libgbm-dev, libxshmfence-dev, libatk-bridge-2.0.so.0, liboss4-salsa-asound2, alsa
.
Install it assudo apt-get install -y {package_name}
. I found some issues when trying to install it in a bulky way, so I recommend installing it one by one to make sure everything is installed.
-
Ask Puppeteer to install Chrome browser.
Run:sudo -u www-data npx puppeteer browsers install chrome
When it’s done, you will see the Chrome file location. For me it was:/home/www-data/.cache/puppeteer/chrome/linux-128.0.6613.119/chrome-linux64/chrome
. Use this path in your code withBrowsershot
:->setChromePath("/home/www-data/.cache/puppeteer/chrome/linux-128.0.6613.119/chrome-linux64/chrome")
-
Use Puppeteer Chrome browser to generate PDF.
Browsershot::html($html)
->setOption('args', ['--disable-web-security'])
->ignoreHttpsErrors()
->noSandbox()
->setCustomTempPath('/home/www-data/browsershot-html')
->addChromiumArguments([
'lang' => "en-US,en;q=0.9",
'hide-scrollbars',
'enable-font-antialiasing',
'force-device-scale-factor' => 1,
'font-render-hinting' => 'none',
'user-data-dir' => '/home/www-data/user-data',
'disk-cache-dir' => '/home/www-data/user-data/Default/Cache',
])
->setChromePath("/home/www-data/.cache/puppeteer/chrome/linux-128.0.6613.119/chrome-linux64/chrome")
->newHeadless()
->showBackground()
->savePdf($tmp_file);
You may not use some options from the example above to make your PDF generation more secure. Feel free to adjust it for your needs.
Hope my struggles will help someone save time.
This answer helped me a lot:
Unable to get Laravel Browsershot working with Puppeteer Ubuntu 20.04 Arm PHP 8.2 with Snap Chromium & Nginx
2