8

I have setup a Laravel Sail environment and I am trying to save a webpage as a pdf using puppeteer.

I am currently using this package to run puppeteer via laravel - https://packagist.org/packages/spatie/browsershot

There requirements section specifies you need to download puppeteer via npm.

Laravel Sail has npm setup so I have installed the puppeteer package but when I try and save a webpage as a screenshot I get the following error

The command "PATH=$PATH:/usr/local/bin NODE_PATH=`npm root -g` node '/var/www/html/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"https:\/\/google.com","action":"screenshot","options":{"type":"png","path":"\/var\/www\/html\/storage\/app\/public\/screenshot.png","args":[],"viewport":{"width":800,"height":600}}}'" failed. Exit Code: 1(General error) Working directory: /var/www/html/public Output: ================ Error Output: ================ Error: Could not find expected browser (chrome) locally. Run `npm install` to download the correct Chromium revision (856583). at ChromeLauncher.launch (/var/www/html/node_modules/puppeteer/lib/cjs/puppeteer/node/Launcher.js:80:27) at async callChrome (/var/www/html/vendor/spatie/browsershot/bin/browser.js:69:23)

Its basically saying it can't find my local version of chromium and I'm not sure how to resolve this, if it wasn't running via docker I could install it locally and point to it but I don't think this is the best solution while using docker.

Lukerayner
  • 412
  • 6
  • 23

1 Answers1

15

You need to install puppeteer with chromium inside the docker container. I've currently the exact same setup for Browsershot with Sail. You'll need to publish the sail config files which will allow you to edit the docker container.

sail artisan sail:publish

Then you can find the docker files in the root of your Laravel project under docker/8.0 or docker/7.4 depending on your PHP version.

Edit the docker file and add the installation command for puppeteer with chromium:

RUN apt-get update \
    && apt-get install -y gconf-service libasound2 libappindicator3-1 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libgconf-2-4 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 libappindicator1 libnss3 lsb-release xdg-utils wget libgbm-dev libatk-bridge2.0-0 \
    && npm install --global --unsafe-perm puppeteer \
    && chmod -R o+rx /usr/lib/node_modules/puppeteer/.local-chromium

Then rebuild the dockerfile:

sail build --no-cache

Because puppeteer is running in docker we need to disable the sandbox. Bear in mind that docker will be a lot slower to generate the PDF then on your host machine so it might be wise to up the default timeout a bit as well.

use Spatie\Browsershot\Browsershot;


Browsershot::html($html)
    ->noSandbox()
    ->timeout(360)
    ->save('your.pdf');
Koen Hendriks
  • 599
  • 4
  • 10
  • Great solution. But indeed it's a *lot* slower than running this on a Homestead-setup using VirtualBox as VM-provider. Even the smallest VM with only 1GB RAM is working fine - Docker-based generation takes forever and I don't even get the reason for it. – SPQRInc May 12 '21 at 18:39
  • Worked for me, tks – Mateus Galasso Apr 14 '22 at 19:28
  • 1
    Is this the right way of doing that when using sail locally, what is recommended by the Laravel Docs. Or is there a better approach? What needs be modified when deploying to production? – PeterPan Jun 15 '22 at 11:55
  • I am getting a `chmod: cannot access '/usr/lib/node_modules/puppeteer/.local-chromium': No such file or directory` error occur in the console after running the `sail build --no-cache`. Do you have any idea why that might be happening? – johncarter Jan 11 '23 at 13:24
  • @johncarter Sounds like the binary of puppeteer has either been renamed or moved since my original answer. You can lookup how to install puppeteer in docker (ubuntu) and update the `RUN` command accordingly. – Koen Hendriks Jan 16 '23 at 09:40
  • 1
    Thank you @KoenHendriks I actually found a solution: https://github.com/spatie/browsershot/discussions/681 – johncarter Jan 16 '23 at 13:04