Serverless PDF Generation with Spatie Laravel-PDF and Cloudflare
Overview
Generating PDFs in PHP has long been a source of frustration, often requiring heavy server-side dependencies like Chrome, Node.js, or complex Docker configurations. The release of Spatie Laravel-PDF v2 by Spatie changes this by introducing a Cloudflare browser rendering driver. This allows developers to offload the entire rendering process to the edge, removing the need for local binary installations while maintaining high-fidelity output from Blade templates.
Prerequisites
To follow this guide, you should have a baseline understanding of Laravel and environment configuration. You will need a Cloudflare account with the Browser Rendering API enabled. No local Chromium or Puppeteer installation is required on your server.
Key Libraries & Tools
- Spatie Laravel-PDF v2: The primary package for generating PDFs from Blade views.
- Cloudflare Browser Rendering API: The backend engine that handles the heavy lifting of HTML-to-PDF conversion.
- Tailwind CSS: Used via CDN or Vite for styling the documents.
Code Walkthrough
To start, configure your .env file with your Cloudflare credentials and set the driver to cloudflare:
LARAVEL_PDF_DRIVER=cloudflare
CLOUDFLARE_ACCOUNT_ID=your_id
CLOUDFLARE_API_TOKEN=your_token

Within your controller, you can use the fluent API to stream or download a PDF directly from a Blade view. The package handles the API communication behind the scenes:
use Spatie\LaravelPdf\Facades\Pdf;
public function download()
{
return Pdf::view('invoices.show', ['data' => $data])
->name('invoice.pdf')
->download();
}
Syntax Notes
The Spatie package utilizes a clean, descriptive syntax that mirrors Laravel's built-in view responses. It supports method chaining for adding metadata or choosing between inline display and forced downloads.
Tips & Gotchas
When setting up your Cloudflare API token, ensure the permissions are set to Account > Browser Rendering > Edit. A common mistake is selecting 'Read' only, which will cause the API request to fail during the generation process. Additionally, while this method simplifies DevOps, monitor your Cloudflare Workers usage to avoid unexpected costs, as the service operates on a paid tiered model.
- Cloudflare
- 36%· companies
- Laravel
- 14%· products
- Spatie
- 14%· companies
- Dries Vints
- 7%· people
- Freek Van der Herten
- 7%· people
- Other topics
- 21%

NEW Spatie/Laravel-PDF v2 with Cloudflare API Driver
WatchLaravel Daily // 4:48