Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Sail install puppeteer chromium

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.

like image 457
Lukerayner Avatar asked Mar 02 '23 16:03

Lukerayner


1 Answers

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');
like image 147
Koen Hendriks Avatar answered Mar 05 '23 16:03

Koen Hendriks