Serverless PDF Generation with Spatie Laravel-PDF and Cloudflare

Laravel Daily////2 min read

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
Serverless PDF Generation with Spatie Laravel-PDF and Cloudflare
NEW Spatie/Laravel-PDF v2 with Cloudflare API Driver

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.

Topic DensityMention share of the most discussed topics · 14 mentions across 8 distinct topics
Cloudflare
36%· companies
Laravel
14%· products
Spatie
14%· companies
Dries Vints
7%· people
Other topics
21%
End of Article
Source video
Serverless PDF Generation with Spatie Laravel-PDF and Cloudflare

NEW Spatie/Laravel-PDF v2 with Cloudflare API Driver

Watch

Laravel Daily // 4:48

Tutorials, and demo projects with Laravel framework. Host: Povilas Korop

Who and what they mention most
Laravel
41.1%23
Filament
19.6%11
PHP
14.3%8
Composer
12.5%7
2 min read0%
2 min read