Laravel Sail install puppeteer chromium
Asked Answered
A

1

9

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.

Azotobacter answered 19/3, 2021 at 13:11 Comment(1)
I've the same issue... Do you have resolve this problem?Brittan
S
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');
Syndic answered 21/4, 2021 at 11:50 Comment(6)
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.Lippmann
Worked for me, tksCastro
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?Loden
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?Oskar
@Oskar 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.Syndic
Thank you @KoenHendriks I actually found a solution: github.com/spatie/browsershot/discussions/681Oskar

© 2022 - 2024 — McMap. All rights reserved.