• Stars
    star
    195
  • Rank 199,374 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Sidecar function to run Browsershot on Lambda.

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

Image Manipulation

Like the original Browsershot package, you can manipulate the image size and format.

Note
If you're using fit() in combination with saveToS3, the image will be downloaded from S3 to your local disc, manipulated and then uploaded back to S3.

// Take screenshot at 1920x1080 and scale it down to fit 200x200 
BrowsershotLambda::url('https://example.com')
    ->windowSize(1920, 1080)
    ->fit(Manipulations::FIT_CONTAIN, 200, 200)
    ->save('example.jpg');
    
// Take screenshot at 1920x1080 and scale it down to fit 200x200 and save it on S3
// Note: To do the image manipulation, BrowsershotLambda will download the image
// from S3 to the local disc of your app, manipulate it and then upload it back to S3. 
BrowsershotLambda::url('https://example.com')
    ->windowSize(1920, 1080)
    ->fit(Manipulations::FIT_CONTAIN, 200, 200)
    ->saveToS3('example.jpg');

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.

More Repositories

1

git-auto-commit-action

Automatically commit and push changed files back to GitHub with this GitHub Action for the 80% use case.
Shell
1,970
star
2

laravel-stats

πŸ“ˆ Get insights about your Laravel or Lumen Project
PHP
1,707
star
3

screeenly

πŸ“Έ Screenshot as a Service
PHP
493
star
4

laravel-sends

A package to keep track of outgoing emails in your Laravel application.
PHP
175
star
5

laravel-backup-restore

A package to restore database backups made with spatie/laravel-backup.
PHP
162
star
6

changelog-updater-action

A GitHub Action to automatically update a "Keep a Changelog" CHANGELOG with the latest release notes.
Shell
122
star
7

diary-app

πŸ”An End-To-End Encrypted Diary Web App
PHP
93
star
8

laravel-github-actions-demo

A demo application to showcase useful GitHub Actions for PHP developers
PHP
30
star
9

kirby-json-feed

Kirby Plugin to serve a JSON Feed
PHP
26
star
10

reusable-workflows

A collection of reusable GitHub Actions workflows I use in my public and private projects.
26
star
11

dotfiles

🐼 My dotfiles
Shell
23
star
12

imgubox

[Deprecated] Store Imgur favorites in Dropbox
PHP
22
star
13

php-changelog-updater

PHP CLI to add latest release notes to a CHANGELOG
PHP
22
star
14

ScreeenlyClient

PHP API Wrapper for Screeenly
PHP
22
star
15

laravel-phpinsights-action

Run PHP Insights in Laravel in Github Actions
Dockerfile
21
star
16

php-swiss-cantons

πŸ‡¨πŸ‡­ Find Swiss Canton by abbreviation, name or zipcode.
PHP
17
star
17

alfred-emoji-pack

PHP Script to generate Snippets for Alfred with all available Emojis.
PHP
14
star
18

commonmark-mark-extension

Render ==highlighted== texts as <mark> elements in league/commonmark
PHP
13
star
19

mp3-to-m4r-converter

Bulk-convert mp3 to m4r-files
Shell
13
star
20

deployer-on-github-actions-example

Example Repository showcasing how to run deployer on GitHub Actions
PHP
13
star
21

laravel-file-encryption-example

Example project to showcase backend file encryption
PHP
12
star
22

laravel-download-statistics-app

Aggregated download statistics for Laravel.
PHP
12
star
23

phpinsights-action

Run PHP Insights in Github Actions
Dockerfile
11
star
24

dropshare-landingpage

A minimal landingpage for Dropshare.app
HTML
7
star
25

uberspaceScripts

My personal collection of useful scripts, when hosting on uberspace.de
Shell
7
star
26

esbuild-mix-manifest-plugin

esbuild plugin to generate mix-manifest.json file compatible with Laravel Mix.
TypeScript
7
star
27

commonmark-markdown-renderer

Render a league/commonmark AST back to Markdown.
PHP
6
star
28

dirby

An opinionated Kirby theme for documentations
CSS
6
star
29

laravel-encryption-key-generator

A simple app to generate new Laravel encryption keys for you.
PHP
6
star
30

vue-tailwind-css-modules-demo

Demo application highlighting the use of Tailwind CSS in Vue Components
JavaScript
6
star
31

dropshare-tailwind-landingpage

Simple landingpage to use with Dropshare
JavaScript
5
star
32

js-swiss-cantons

πŸ‡¨πŸ‡­ Find Swiss Canton by abbreviation or name
JavaScript
5
star
33

getting-started-with-bash-testing

Example Bash Project to get started with testing with Bats.
Shell
5
star
34

kirby-laravel-mix-helper

Helper to use Version Busting of Laravel Mix
PHP
5
star
35

icq-christmas-card

Revive one of the old ICQ Christmas Cards from 2002.
HTML
4
star
36

multi-photo-crop

πŸ™ A tool to crop and split photos from a single image
PHP
4
star
37

swiss-canton-iconfont

Serve the 26 Swiss Cantons in a simple icon font
CSS
3
star
38

example-advanced-eloquent-with-pivot

Example project mentioned in an article of mine.
PHP
3
star
39

asana-expander-extension

Browser Extension to automatically expand long comments in Asana
TypeScript
3
star
40

photon-the-archive-theme

A "The Archive" theme based on the PHPStorm Theme "Photon" by Brent Roose
3
star
41

alfred-emoji-pack-node

Emojis at your fingertips as Alfred Snippets
JavaScript
2
star
42

radio-srf-menubarapp

πŸ“» A simple menubar application to play Radio SRF stations
Swift
2
star
43

faker-swiss-locations

Provider to generate valid Swiss location data for Faker PHP.
PHP
2
star
44

php-search-string-parser

[In Development] Use Search Input Strings similar to Github
PHP
2
star
45

git-auto-commit-action-demo-app

A demo application to test git-auto-commit Github Action
PHP
2
star
46

oh-dear-request-run-action

Trigger Oh Dear runs through GitHub Actions.
2
star
47

life-expectancy-visualisation

A small application to visualise the life expectancy of different people around the world.
TypeScript
2
star
48

scratchpad

Minimal Scratchpad
HTML
1
star
49

stefanzweifel

1
star
50

chrome-facebook-to-wikipedia-redirect

Read a Wikipedia Article instead of your Newsfeed
JavaScript
1
star
51

user-agent-analyzer

User Agent analysis as a Service.
PHP
1
star
52

git-auto-commit-demo-app

A demo application to test the git-auto-commit Action.
1
star
53

sidecar-browsershot-layer

AWS Lambda Layer containing puppeteer-core. Used by sidecar-browsershot
Shell
1
star