Using the Cloudflare driver | laravel-pdf | Spatie

 SPATIE

  Laravel PDF
==============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-pdf](https://spatie.be/docs/laravel-pdf/v2)  Drivers  Using the Cloudflare driver

 Version   v2   v1

 Other versions for crawler [v2](https://spatie.be/docs/laravel-pdf/v2) [v1](https://spatie.be/docs/laravel-pdf/v1)

- [ Introduction ](https://spatie.be/docs/laravel-pdf/v2/introduction)
- [ Support us ](https://spatie.be/docs/laravel-pdf/v2/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-pdf/v2/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-pdf/v2/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-pdf/v2/questions-issues)
- [ Alternatives ](https://spatie.be/docs/laravel-pdf/v2/alternatives)
- [ Changelog ](https://spatie.be/docs/laravel-pdf/v2/changelog)
- [ About us ](https://spatie.be/docs/laravel-pdf/v2/about-us)

Basic usage
-----------

- [ Creating PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/creating-pdfs)
- [ Responding with PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/responding-with-pdfs)
- [ Formatting PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/formatting-pdfs)
- [ Saving PDFs to disks ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/saving-pdfs-to-disks)
- [ Queued PDF generation ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/queued-pdf-generation)
- [ Testing PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/testing-pdfs)
- [ Setting defaults ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/setting-defaults)

Drivers
-------

- [ Configuration ](https://spatie.be/docs/laravel-pdf/v2/drivers/configuration)
- [ Customizing Browsershot ](https://spatie.be/docs/laravel-pdf/v2/drivers/customizing-browsershot)
- [ Using the Cloudflare driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-cloudflare-driver)
- [ Using the DOMPDF driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-dompdf-driver)
- [ Generating PDFs on AWS Lambda ](https://spatie.be/docs/laravel-pdf/v2/drivers/generating-pdfs-on-aws-lambda)
- [ Using the Gotenberg driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-gotenberg-driver)
- [ Using the WeasyPrint driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-weasyprint-driver)
- [ Custom drivers ](https://spatie.be/docs/laravel-pdf/v2/drivers/custom-drivers)

Advanced usage
--------------

- [ Creating PDFs with multiple pages ](https://spatie.be/docs/laravel-pdf/v2/advanced-usage/creating-pdfs-with-multiple-pages)
- [ Using Tailwind ](https://spatie.be/docs/laravel-pdf/v2/advanced-usage/using-tailwind)
- [ Extending with Macros ](https://spatie.be/docs/laravel-pdf/v2/advanced-usage/extending-with-macros)

 Using the Cloudflare driver
===========================

###  On this page

1. [ Getting started ](#content-getting-started)
2. [ Supported options ](#content-supported-options)
3. [ Using Cloudflare for specific PDFs only ](#content-using-cloudflare-for-specific-pdfs-only)
4. [ Limits ](#content-limits)
5. [ Limitations ](#content-limitations)

The Cloudflare driver uses [Cloudflare's Browser Rendering API](https://developers.cloudflare.com/browser-rendering/) to generate PDFs. Unlike the Browsershot driver, it does not require Node.js or a Chrome binary on your server. Instead, it makes a simple HTTP call to Cloudflare's API, which renders your HTML and returns a PDF.

This approach was inspired by this tweet by [Dries Vints](https://x.com/driesvints/status/2016131972477632850).

Getting started
-----------------------------------------------------------------------------------------------------

1. Make sure you have a [Cloudflare account](https://dash.cloudflare.com/sign-up)
2. In the Cloudflare dashboard, go to **Manage account &gt; Account API tokens** in the sidebar
3. Click **Create Token** and create a token with the **Account.Browser Rendering** permission
4. Your Account ID can be found in the address bar of the Cloudflare dashboard URL

![Cloudflare API tokens page](/docs/laravel-pdf/v2/images/cloudflare-api-tokens.jpg)

5. Add the following to your `.env` file:

```
LARAVEL_PDF_DRIVER=cloudflare
CLOUDFLARE_API_TOKEN=your-api-token
CLOUDFLARE_ACCOUNT_ID=your-account-id
```

That's it. Your existing PDF code will now use Cloudflare for generation:

```
use Spatie\LaravelPdf\Facades\Pdf;

// This will use Cloudflare — no code changes needed
Pdf::view('pdfs.invoice', ['invoice' => $invoice])
    ->format('a4')
    ->save('invoice.pdf');
```

Supported options
-----------------------------------------------------------------------------------------------------------

The Cloudflare driver supports the following PDF options:

- `format()` — Paper format (a4, letter, etc.)
- `paperSize()` — Custom paper dimensions
- `margins()` — Page margins
- `landscape()` / `orientation()` — Page orientation
- `headerView()` / `headerHtml()` — Page headers
- `footerView()` / `footerHtml()` — Page footers

Using Cloudflare for specific PDFs only
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If you want to use Browsershot as your default driver but switch to Cloudflare for specific PDFs, you can use the `driver` method:

```
use Spatie\LaravelPdf\Facades\Pdf;

Pdf::view('pdfs.invoice', ['invoice' => $invoice])
    ->driver('cloudflare')
    ->format('a4')
    ->save('invoice.pdf');
```

Make sure you have the Cloudflare credentials configured in your `config/laravel-pdf.php` or `.env`, even if Cloudflare is not the default driver.

Limits
--------------------------------------------------------------------------

The free Cloudflare Workers plan is limited to 6 REST API requests per minute and 10 minutes of Browser Rendering usage per day. The paid Workers plan offers significantly higher limits. Check the [Cloudflare Browser Rendering limits page](https://developers.cloudflare.com/browser-rendering/platform/limits/) for the latest details.

Limitations
-----------------------------------------------------------------------------------------

- The Cloudflare driver only supports PDF output. Saving as PNG (screenshots) is not supported.
- The `withBrowsershot()` and `onLambda()` methods have no effect when using the Cloudflare driver.
- JavaScript execution depends on Cloudflare's Browser Rendering API behavior — complex scripts may behave differently than in a local Chromium instance.
