spatie/browsershot
Convert web pages or raw HTML to images and PDFs in PHP using Puppeteer-driven headless Chrome. Capture screenshots, generate PDFs, fetch rendered body HTML, and inspect triggered network requests—ideal for reports, previews, and testing.
evaluateOnNewDocument feature (v5.4.0) enables JavaScript execution in the context of a blank PDF document, unlocking advanced PDF manipulation capabilities such as:
page.evaluateOnNewDocument() under the hood, reducing the need for raw Node.js/Puppeteer code.Browsershot and use dependency injection for evaluateOnNewDocument).pdf_generation_script column in a templates table) for dynamic PDF customization.| Risk Area | Mitigation Strategy |
|---|---|
| JavaScript Execution Overhead | Benchmark memory/CPU usage for evaluateOnNewDocument in staging; optimize with ->setNodeModulePath() and Docker. |
| Security Risks | Sanitize all JavaScript payloads to prevent PDF-based attacks (e.g., malicious scripts injected via evaluateOnNewDocument). Use a whitelist or sandboxed execution. |
| Cross-Platform Issues | Test on target environments (e.g., Ubuntu 24.04, Windows Server) for Chromium/PDF rendering consistency. |
| Debugging Complexity | Leverage ->debug() or ->saveDebugScreenshot() to inspect intermediate PDF states during JavaScript execution. |
| Performance | Cache static JavaScript logic (e.g., reusable PDF templates) to avoid reprocessing. |
evaluateOnNewDocument be used for dynamic form filling, PDF post-processing, or client-side-rendered content injection?evaluateOnNewDocument be used in high-volume scenarios (e.g., 1,000+ PDFs/hour)? If so, test queue performance under load.evaluateOnNewDocument logic, visual regression for PDFs)?--enable-features=PDF) for PDF manipulation support?evaluateOnNewDocument extends Laravel’s PDF capabilities without Node.js context-switching.evaluateOnNewDocument for PDF post-processing while keeping core generation in PHP.templates table with script column) for dynamic PDF customization.generated_at, script_hash).evaluateOnNewDocument with static HTML templates to validate output.evaluateOnNewDocument for scalability.->addChromiumArguments(['--disable-gpu']) to improve performance in headless environments.| Component | Compatibility Notes |
|---|---|
| Laravel Versions | No changes; continues to support Laravel 10+. |
| PHP Versions | Requires PHP 8.1+ (unchanged). |
| Node.js | Still requires Node.js 22+ and Puppeteer 23+ (no breaking changes). |
| Chrome/Chromium | Requires Chrome 120+ with PDF support (e.g., --enable-features=PDF). |
| Storage Systems | Unchanged; works with local/S3/PSR-3 storage. |
| Queue Systems | Supports Laravel’s default queues (Redis, database, etc.). |
composer.json to spatie/browsershot:^5.4 and run composer update.npm install puppeteer@latest).Browsershot::html($html)
->evaluateOnNewDocument('
document.addEventListener("DOMContentLoaded", () => {
document.body.style.fontSize = "12pt";
});
')
->save('output.pdf');
Browsershot::html($html)
->evaluateOnNewDocument('
document.forms[0].elements[0].value = "'.e($customerName).'";
')
->save('invoice.pdf');
Browsershot::dispatch($html)
->evaluateOnNewDocument($script)
->onQueue('pdfs')
->save('storage/app/pdf/' . $filename);
try {
Browsershot::html($html)->evaluateOnNewDocument($script)->save($path);
} catch (\Exception $e) {
Log::error("PDF generation failed: " . $e->getMessage());
// Fallback to static PDF or notify user
}
--enable-features=PDF is supported).evaluateOnNewDocument support (e.g., new Chromium features).--enable-features=PDF (or equivalent) is set for PDF manipulation.How can I help you explore Laravel packages today?