Giter VIP home page Giter VIP logo

sidecar-browsershot's Introduction

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

You can publish the config file with:

php artisan vendor:publish --tag="sidecar-browsershot-config"

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);

Warming

sidecar-browsershot supports warming for faster execution.

To enable this feature set the SIDECAR_BROWSERSHOT_WARMING_INSTANCES variable in your .env to the desired number of instances Sidecar should warm for you.

SIDECAR_BROWSERSHOT_WARMING_INSTANCES=5

Alternatively you can publish the sidecar-browsershot.php config file and change the warming setting yourself.

Reading source from S3

You can store an HTML file on AWS S3 and pass the path to Lambda for it to create the PDF or image from. This is necessary for large source files in order to avoid restrictions on the size of Lambda requests.

use Wnx\SidecarBrowsershot\BrowsershotLambda;

// Use an HTML file from S3 to generate a PDF
BrowsershotLambda::readHtmlFromS3('html/example.html')->save('example.pdf');

// You can also pass a disk name if required (default: 's3')
BrowsershotLambda::readHtmlFromS3('html/example.html', 's3files')->save('example.pdf');

Saving directly to S3

You can store your file directly on AWS S3 if you want to keep it there, or to avoid the size limit on Lambda responses.

You just need to pass a path and optional disk name (default: 's3') to the saveToS3 method.

  • You must have an S3 disk defined in config/filesystems.php
  • You must give S3 write permissions to your sidecar-execution-role
use Wnx\SidecarBrowsershot\BrowsershotLambda;

// an image will be saved on S3
BrowsershotLambda::url('https://example.com')->saveToS3('example.jpg');

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

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

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.

sidecar-browsershot's People

Contributors

stefanzweifel avatar marksalmon avatar dependabot[bot] avatar github-actions[bot] avatar bryanjamesmiller avatar robgaskell avatar aarondfrancis avatar admiral-enigma avatar orkhanahmadov avatar silver343 avatar yoeriboven avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.