wnx/sidecar-browsershot
Run Spatie Browsershot on AWS Lambda via Sidecar for Laravel. Generate PDFs/screenshots without installing Node, Puppeteer, or Chrome on your server—headless Chrome runs in a deployed Lambda function. Includes config publishing and Sidecar deploy steps.
Install Dependencies
composer require wnx/sidecar-browsershot spatie/browsershot hammerstone/sidecar
Skip Chrome/Puppeteer installation for spatie/browsershot as this package handles it via Lambda.
Configure Sidecar
Add BrowsershotFunction::class to config/sidecar.php:
'functions' => [
\Wnx\SidecarBrowsershot\Functions\BrowsershotFunction::class,
],
Deploy Lambda
php artisan sidecar:deploy --activate
First Use Case
Replace Browsershot with BrowsershotLambda in your code:
use Wnx\SidecarBrowsershot\BrowsershotLambda;
BrowsershotLambda::url('https://example.com')->save('screenshot.png');
URL/HTML to File
// Direct save to local storage
BrowsershotLambda::url('https://example.com')->save('output.pdf');
// HTML string to PDF
BrowsershotLambda::html('<h1>Test</h1>')->save('output.pdf');
S3 Integration
// Read HTML from S3
BrowsershotLambda::readHtmlFromS3('path/to/file.html')->save('output.pdf');
// Save directly to S3
BrowsershotLambda::url('https://example.com')->saveToS3('s3/path/output.pdf');
Image Manipulation
BrowsershotLambda::url('https://example.com')
->windowSize(1920, 1080)
->fit(\Spatie\Image\Enums\Fit::Contain, 200, 200)
->save('resized.png');
Warm Lambda Instances
Configure in .env:
SIDECAR_BROWSERSHOT_WARMING_INSTANCES=3
Reduces cold-start latency for frequent use.
Custom Fonts
Place fonts in resources/sidecar-browsershot/fonts/ and redeploy Lambda.
Error Handling
try {
BrowsershotLambda::url('https://example.com')->save('output.pdf');
} catch (\Wnx\SidecarBrowsershot\Exceptions\SidecarBrowsershotException $e) {
// Handle Lambda/Sidecar errors
}
Lambda Size Limits
readHtmlFromS3() for large files.saveToS3() for large outputs.Cold Starts
SIDECAR_BROWSERSHOT_WARMING_INSTANCES).Image Manipulation on S3
fit() operations require downloading the image locally before re-uploading.Font Loading
resources/sidecar-browsershot/fonts/ before redeploying Lambda.Lambda Logs Use AWS CloudWatch to inspect execution logs:
php artisan sidecar:logs
Sidecar Events
Enable Sidecar’s debug mode in .env:
SIDECAR_DEBUG=true
Common Errors
| Error | Cause | Solution |
|---|---|---|
TimeoutException |
Lambda timeout (default: 3s) | Increase timeout in sidecar.php or optimize payload. |
InvalidArgumentException |
Unsupported format | Ensure output format is pdf, png, jpg, etc. |
S3PermissionError |
Missing IAM permissions | Attach AmazonS3FullAccess to Sidecar’s execution role. |
Custom Layers
Override Chromium/Puppeteer layers in sidecar-browsershot.php:
'layers' => [
'arn:aws:lambda:us-east-1:123456789012:layer:custom-chrome:1',
],
Event Hooks
Extend BrowsershotFunction to add pre/post-processing:
class CustomBrowsershotFunction extends BrowsershotFunction {
protected function beforeExecute(): void {
// Add custom logic (e.g., logging, auth)
}
}
Testing Mock Lambda responses in tests:
$this->mockSidecarResponse('output.pdf', file_get_contents('tests/fixtures/example.pdf'));
How can I help you explore Laravel packages today?