spatie/laravel-screenshot
Driver-based Laravel package for taking web page screenshots with great defaults. Use Browsershot (Chromium) or Cloudflare Browser Rendering, customize viewport/format/quality, save to files, and easily fake/assert screenshots in tests.
Screenshot generation can be slow, especially with the Browsershot or Cloudflare driver. If you don't need the screenshot immediately, you can dispatch the generation to a background queue using saveQueued().
use Spatie\LaravelScreenshot\Facades\Screenshot;
Screenshot::url('https://example.com')
->saveQueued('screenshot.png');
This will dispatch a queued job that takes the screenshot and saves it to the given path.
You can chain then() and catch() callbacks to react to the job's success or failure:
Screenshot::url('https://example.com')
->saveQueued('screenshot.png')
->then(fn (string $path, ?string $diskName) => Mail::to($user)->send(new ScreenshotMail($path)))
->catch(fn (Throwable $e) => Log::error('Screenshot failed', ['error' => $e->getMessage()]));
The then callback receives the path the screenshot was saved to and the disk name (or null for local saves). This makes it easy to retrieve the file afterwards:
->then(function (string $path, ?string $diskName) {
$contents = $diskName
? Storage::disk($diskName)->get($path)
: file_get_contents($path);
})
The catch callback receives the exception.
You can specify the connection and queue name directly:
Screenshot::url('https://example.com')
->saveQueued('screenshot.png', connection: 'redis', queue: 'screenshots');
Or use chained methods for full control — these are proxied to Laravel's PendingDispatch:
Screenshot::url('https://example.com')
->saveQueued('screenshot.png')
->onQueue('screenshots')
->onConnection('redis')
->delay(now()->addMinutes(5));
When using disk(), the queued job will save the screenshot to the specified disk:
Screenshot::url('https://example.com')
->disk('s3')
->saveQueued('screenshots/homepage.png')
->then(function (string $path, ?string $diskName) {
$url = Storage::disk($diskName)->url($path);
// ...
});
You can replace the job class used by saveQueued() in your config/laravel-screenshot.php:
'job' => \App\Jobs\GenerateScreenshotJob::class,
Your custom class should extend the default job:
namespace App\Jobs;
use Spatie\LaravelScreenshot\Jobs\GenerateScreenshotJob as BaseJob;
class GenerateScreenshotJob extends BaseJob
{
public int $tries = 3;
public int $timeout = 120;
public int $backoff = 30;
}
This lets you set defaults like retry attempts, timeouts, or a default queue for all queued screenshot jobs.
saveQueued() cannot be used with withBrowsershot(). The closure passed to withBrowsershot() may capture objects or state that cannot be reliably serialized for the queue. An exception will be thrown if you try.
How can I help you explore Laravel packages today?