Hello
World
') ``` ### Element-Specific Capture Crop to a single element by CSS selector — useful for certificates, charts, or widgets: ```php Screenshot::view('dashboard.report', ['report' => $report]) ->element('#chart-container') ->png() ->capture(); // returns an Image object for further manipulation ``` The `capture()` method returns a `DirectoryTree\Screenshot\Image` instance (implements `Arrayable`, `Jsonable`), which you can save to multiple destinations: ```php $image = Screenshot::view('certificates.show', ['certificate' => $certificate]) ->element('#certificate') ->png() ->capture(); $image->save(storage_path("app/certificate.png")); $image->store("certificates/{$certificate->id}.png", 's3'); ``` ### JavaScript Rendering (Opt-In) By default, no JS executes — fast and deterministic. Enable it when you need chart libraries, SPAs, or dynamic content: ```php Screenshot::url($url)->javascript()->pdf() ``` ### Testing Support ```php // In your test Screenshot::fake(); // Assert a screenshot was taken Screenshot::assertViewed('certificates.show', ['certificate' => $certificate->id]); // Or inspect the captured image $image = Screenshot::captured()->first(); ``` ### Gotenberg Driver (Remote Rendering) For serverless or containerized environments where bundling Chrome is impractical, the package supports [Gotenberg](https://gotenberg.dev/) 8.38+ as a remote rendering driver. Configure it in `config/screenshot.php`: ```php 'driver' => env('SCREENSHOT_DRIVER', 'chrome'), // or 'gotenberg' 'gotenberg' => [ 'url' => env('GOTENBERG_URL', 'http://localhost:3000'), ], ``` ## Comparison with Alternatives | Feature | directorytree/screenshot | Spatie laravel-screenshot (Browsershot) | |---------|--------------------------|-----------------------------------------| | Node.js required | No | Yes | | Puppeteer required | No | Yes | | Chrome management | `php artisan screenshot:install` | Manual / `npx puppeteer browsers install` | | Blade view rendering | Native | Native | | Element capture | Yes | Yes | | JS rendering | Opt-in | Default on | | Testing fake | Built-in | Via Browsershot | | Gotenberg support | Yes | No (community forks) | | Laravel version | 12+ (PHP 8.2+), 13+ (PHP 8.3+) | 10+ | If your stack already runs Node.js for frontend tooling, Browsershot is battle-tested. If you want to keep the stack PHP-only — or deploy to environments where Node.js is unavailable — directorytree/screenshot is a clean alternative. ## Gotchas and When to Use It **Use it when:** - You need PDFs or screenshots in a PHP-only deployment (Laravel Cloud, Vapor, traditional VPS without Node.js) - You want deterministic, fast captures without JS by default - You prefer managing Chrome via Composer/Artisan rather than npm - You need Gotenberg as a remote fallback **Watch out for:** - The Chrome binary must match your platform/architecture. The install command handles this, but in multi-arch CI you may need to run it per platform. - JavaScript rendering adds latency. Keep it off unless necessary. - Very long pages may hit Chrome's default limits; adjust with `->fullPage()` or custom flags. - Font rendering depends on the host system's fonts. Install required fonts in your Docker image or CI. ## A Practical Example: Invoice PDFs A common use case: generate an invoice PDF from a Blade view, store it, and email it. ```php // app/Services/InvoicePdfGenerator.php class InvoicePdfGenerator { public function generate(Invoice $invoice): string { $path = "invoices/{$invoice->id}.pdf"; Screenshot::view('pdf.invoice', ['invoice' => $invoice]) ->pdf() ->paper(PaperSize::A4) ->margins(15, 15, 15, 15) ->save(storage_path("app/{$path}")); return $path; } } ``` The Blade view (`resources/views/pdf/invoice.blade.php`) uses standard HTML/CSS — including `@page` rules for headers/footers, page breaks, and print media queries. No special syntax needed. ## Closing Thought Removing Node.js from the PDF/screenshot pipeline simplifies Dockerfiles, CI pipelines, and server provisioning. For teams already invested in the PHP ecosystem, directorytree/screenshot is a pragmatic addition that keeps the stack coherent. ## Sources - [Laravel News: Screenshot: Capture Images and PDFs with PHP and Laravel](https://laravel-news.com/screenshot) (Oct 22, 2026) - [directorytree/screenshot on Packagist](https://packagist.org/packages/directorytree/screenshot) - [chrome-php/chrome](https://github.com/chrome-php/chrome) — the underlying Chrome control library - [Gotenberg](https://gotenberg.dev/) — remote rendering alternative