wnx/sidecar-browsershot
Run Spatie Browsershot on AWS Lambda via Sidecar in Laravel—no need to install Node, Puppeteer, or Chrome on your servers. Deploy a Lambda function and generate PDFs/screenshots with headless Chrome handled remotely.
## Getting Started
### Minimal Setup
1. **Prerequisites**: Ensure `spatie/browsershot` and `hammerstone/sidecar` are installed (skip Chrome/Puppeteer installation).
2. **Install Package**:
```bash
composer require wnx/sidecar-browsershot
php artisan vendor:publish --tag="sidecar-browsershot-config"
\Wnx\SidecarBrowsershot\Functions\BrowsershotFunction::class to sidecar.php under 'functions'.php artisan sidecar:deploy --activate
Replace Browsershot with BrowsershotLambda for Lambda-based rendering:
use Wnx\SidecarBrowsershot\BrowsershotLambda;
// Generate PDF from URL
BrowsershotLambda::url('https://example.com')->save('example.pdf');
// Generate image from HTML
BrowsershotLambda::html('<h1>Hello</h1>')->save('image.png');
Invoke Lambda:
BrowsershotLambda::url('https://example.com')->save('output.pdf');
S3 Integration:
BrowsershotLambda::readHtmlFromS3('s3/path/to/file.html')->save('output.pdf');
BrowsershotLambda::url('https://example.com')->saveToS3('s3/path/output.pdf');
Image Manipulation (requires spatie/image):
BrowsershotLambda::url('https://example.com')
->windowSize(1920, 1080)
->fit(\Spatie\Image\Enums\Fit::Contain, 200, 200)
->save('resized.png');
Warming Instances: Enable faster execution by pre-warming Lambda instances:
// In .env
SIDECAR_BROWSERSHOT_WARMING_INSTANCES=3
Or via config: sidecar-browsershot.php > 'warming' => 3.
Custom Fonts:
Place fonts in resources/sidecar-browsershot/fonts/ (configurable via sidecar-browsershot.fonts).
The package auto-includes them in the Lambda deployment.
Error Handling:
Wrap calls in try-catch to handle Lambda timeouts or failures:
try {
BrowsershotLambda::url('https://example.com')->save('output.pdf');
} catch (\Exception $e) {
Log::error('BrowsershotLambda failed: ' . $e->getMessage());
// Fallback logic (e.g., retry or use local Browsershot)
}
Queue Jobs: Offload heavy rendering to queues (e.g., Laravel Queues) to avoid timeouts:
RenderPdfJob::dispatch('https://example.com', 'output.pdf');
Lambda Timeouts:
sidecar.php or use ->timeout(30) on the chain.S3 Permissions:
s3:GetObject (for reading) and s3:PutObject (for writing) permissions.AWS Access Denied when using readHtmlFromS3 or saveToS3.Image Manipulation on S3:
fit() or other image manipulations download the file locally, process it, and re-upload.Cold Starts:
SIDECAR_BROWSERSHOT_WARMING_INSTANCES) or provisioned concurrency.Deprecated Methods:
chromium.font() was removed in v3.0.0. Use the resources/sidecar-browsershot/fonts/ folder instead.Layer Updates:
sidecar-browsershot-layer).Lambda Logs:
BrowsershotFunction to debug failures.'debug' => env('APP_DEBUG', false),
in sidecar.php.Local Testing:
sidecar:local to test Lambda functions locally:
php artisan sidecar:local
$this->mock(BrowsershotLambda::class)->shouldReceive('url')
->andReturnSelf()
->shouldReceive('save')
->andReturn(true);
Payload Size Limits:
saveToS3 for large outputs or split files.Environment Variables:
.env includes:
SIDECAR_BROWSERSHOT_WARMING_INSTANCES=1
AWS_REGION=us-east-1
Custom Lambda Layers:
sidecar-browsershot.php:
'layers' => [
'arn:aws:lambda:us-east-1:123456789012:layer:custom-chrome-layer:1',
],
Pre/Post-Processing:
BrowsershotLambda by creating a decorator:
class CustomBrowsershot extends BrowsershotLambda {
public function addWatermark(string $text) {
$this->html = $this->html . "<div style='position: absolute; bottom: 10px;'>$text</div>";
return $this;
}
}
Event Listeners:
BrowsershotLambda::url('https://example.com')
->save('output.pdf')
->then(function () {
event(new PdfGenerated('output.pdf'));
});
Fallback Mechanism:
try {
BrowsershotLambda::url($url)->save($path);
} catch (\Exception $e) {
\Spatie\Browsershot\Browsershot::url($url)->save($path);
}
Font Path:
resources/sidecar-browsershot/fonts/.sidecar-browsershot.php:
'fonts' => 'custom/path/to/fonts',
Timeout Handling:
sidecar.php:
'timeout' => 15, // seconds
BrowsershotLambda::url('https://example.com')->timeout(30)->save('output.pdf');
Node.js Runtime:
Runtime.ImportModuleError if Node versions mismatch.Browsershot Version:
composer require spatie/browsershot:^5.0
---
How can I help you explore Laravel packages today?