stefanzweifel / sidecar-browsershot by stefanzweifel

A Sidecar function to run Browsershot on Lambda.
319,491
185
6
Package Data
Maintainer Username: stefanzweifel
Maintainer Contact: stefan@stefanzweifel.dev (Stefan Zweifel)
Package Create Date: 2022-01-30
Package Last Update: 2024-04-09
Home Page: https://stefanzweifel.dev/posts/2022/06/21/introducing-sidecar-browsershot
Language: PHP
License: MIT
Last Refreshed: 2024-04-18 15:00:43
Package Statistics
Total Downloads: 319,491
Monthly Downloads: 19,856
Daily Downloads: 947
Total Stars: 185
Total Watchers: 6
Total Forks: 23
Total Open Issues: 7

Run Browsershot on AWS Lambda with Sidecar for Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package allows you to run Browsershot on AWS Lambda through Sidecar.

You won't need to install Node, Puppeteer or Google Chrome on your server. The heavy lifting of booting a headless Google Chrome instance is happening on AWS Lambda.

Requirements

This package requires that spatie/browsershot and hammerstone/sidecar have been installed in your Laravel application.

Follow their installation and configuration instructions. (You can skip the installation of puppeteer and Google Chrome for Browsershot though.)

Installation

You can install the package via composer:

composer require wnx/sidecar-browsershot

Register the BrowsershotFunction::class in your sidecar.php config file.

/*
 * All of your function classes that you'd like to deploy go here.
 */
'functions' => [
    \Wnx\SidecarBrowsershot\Functions\BrowsershotFunction::class,
],

Deploy the Lambda function by running:

php artisan sidecar:deploy --activate

See Sidecar documentation for details.

Usage

You can use BrowsershotLambda like the default Browsershot-class coming from the Spatie package. All you need to do is replace Browsershot with BrowsershotLambda.

use Wnx\SidecarBrowsershot\BrowsershotLambda;

// an image will be saved
BrowsershotLambda::url('https://example.com')->save($pathToImage);

// a pdf will be saved
BrowsershotLambda::url('https://example.com')->save('example.pdf');

// save your own HTML to a PDF
BrowsershotLambda::html('<h1>Hello world!!</h1>')->save('example.pdf');

// Get HTML of a URL and store it on a given disk
$html = BrowsershotLambda::url('https://example.com')->bodyHtml();
Storage::disk('s3')->put('example.html', $html);

Testing

The testsuite makes connections to AWS and runs the deployed Lambda function. In order to run the testsuite, you will need an active AWS account.

We can use the native sidecar:configure artisan command to create the necessary AWS credentials for Sidecar. First copy the testbench.example.yaml file to testbench.yaml. Then run ./vendor/bin/testbench sidecar:configure to start the Sidecar setup process. (You only have to do the setup once)

cp testbench.example.yaml testbench.yaml
cp .env.example .env
./vendor/bin/testbench sidecar:configure

After finishing the Sidecar setup process, you will have received a couple of SIDECAR_* environment variables. Add these credentials to .env.

Now we can deploy our local BrowsershotFunction to AWS Lambda. Run the following command in your terminal, before executing the testsuite.

./vendor/bin/testbench sidecar-browsershot:setup

After the successful deployment, you can run the testsuite.

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.