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
Prerequisites
To follow this guide, you should have a baseline understanding of
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:
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
Tips & Gotchas
When setting up your