Ehsan Bagherzadegan
Back to articles
5 min read

Generating PDFs and Screenshots in Laravel Without Node.js: directorytree/screenshot

A new PHP-only package that captures Blade views and URLs as images or PDFs using Chrome for Testing — no Node.js, no Puppeteer, just Composer and an Artisan command.

Generating PDFs and screenshots in Laravel has traditionally meant pulling in Node.js and Puppeteer (via Browsershot or Spatie's `laravel-screenshot`). That adds a whole runtime, build step, and deployment complexity just to render a Blade view to PDF. **directorytree/screenshot** changes that. Released by Steve Bauman (DirectoryTree) in October 2026, it launches and controls Chrome directly from PHP using `chrome-php/chrome` — no Node.js required. You `composer require` it, run an Artisan command to fetch Chrome for Testing, and you're rendering. ## Installation ```bash composer require directorytree/screenshot php artisan screenshot:install ``` The install command downloads a matching Chrome for Testing binary into your project (or a shared location). In CI, you can pin the version or use the system Chrome if preferred. ## Basic Usage Capture a Blade view to an image: ```php use DirectoryTree\Screenshot\Facades\Screenshot; Screenshot::view('certificates.show', ['certificate' => $certificate]) ->size(1440, 900) ->jpeg(quality: 85) ->store("certificates/{$certificate->id}/preview.jpg", 's3'); ``` Capture a live URL with JavaScript rendering to PDF: ```php Screenshot::url($product->public_url) ->javascript() ->size(1280, 720) ->pdf() ->paper(PaperSize::A4) ->landscape() ->save(storage_path("app/product-{$product->id}.pdf")); ``` The fluent API chains naturally: source → options → output format → destination. ## Key Features ### Blade Views and Raw HTML ```php // Blade view with data Screenshot::view('emails.invoice', ['invoice' => $invoice]) // Raw HTML string Screenshot::html('

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