Using the Cloudflare driver | laravel-pdf-rust | Spatie       

 SPATIE  

laravel-pdf-rust
================

spatie.be/open-source

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

 Version   v1      

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

  Using the Cloudflare driver    
- [ Introduction ](https://spatie.be/docs/laravel-pdf-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-pdf-rust/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-pdf-rust/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-pdf-rust/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-pdf-rust/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-pdf-rust/v1/changelog)
- [ Testing ](https://spatie.be/docs/laravel-pdf-rust/v1/testing)
- [ About us ](https://spatie.be/docs/laravel-pdf-rust/v1/about-us)

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

- [ Creating PDFs ](https://spatie.be/docs/laravel-pdf-rust/v1/basic-usage/creating-pdfs)
- [ Responding with PDFs ](https://spatie.be/docs/laravel-pdf-rust/v1/basic-usage/responding-with-pdfs)
- [ Formatting PDFs ](https://spatie.be/docs/laravel-pdf-rust/v1/basic-usage/formatting-pdfs)
- [ Saving PDFs to disks ](https://spatie.be/docs/laravel-pdf-rust/v1/basic-usage/saving-pdfs-to-disks)
- [ Attaching PDFs to mails ](https://spatie.be/docs/laravel-pdf-rust/v1/basic-usage/attaching-pdfs-to-mails)
- [ Queued PDF generation ](https://spatie.be/docs/laravel-pdf-rust/v1/basic-usage/queued-pdf-generation)
- [ Testing PDFs ](https://spatie.be/docs/laravel-pdf-rust/v1/basic-usage/testing-pdfs)
- [ Setting defaults ](https://spatie.be/docs/laravel-pdf-rust/v1/basic-usage/setting-defaults)
- [ Protecting PDFs with a password ](https://spatie.be/docs/laravel-pdf-rust/v1/basic-usage/protecting-pdfs-with-a-password)

Drivers
-------

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

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

- [ Creating PDFs with multiple pages ](https://spatie.be/docs/laravel-pdf-rust/v1/advanced-usage/creating-pdfs-with-multiple-pages)
- [ Using Tailwind ](https://spatie.be/docs/laravel-pdf-rust/v1/advanced-usage/using-tailwind)
- [ Extending with Macros ](https://spatie.be/docs/laravel-pdf-rust/v1/advanced-usage/extending-with-macros)
- [ Waiting for readiness ](https://spatie.be/docs/laravel-pdf-rust/v1/advanced-usage/waiting-for-readiness)
- [ Caching PDFs ](https://spatie.be/docs/laravel-pdf-rust/v1/advanced-usage/caching-pdfs)

 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 Run API](https://developers.cloudflare.com/browser-run/) 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 Run** permission (both read and edit/write).

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

Read the account ID and API token from application configuration:

```
use spatie_pdf::drivers::CloudflareDriver;

pdf.defaults(pdf.builder().set_driver(
    CloudflareDriver::new(account_id, api_token)?,
));
```

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

```
use serde_json::json;
use spatie_pdf::Format;

pdf.view("pdfs.invoice", json!({"invoice": invoice}))
    .format(Format::A4)
    .save("invoice.pdf").await?;
```

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

The Cloudflare driver supports the following PDF options:

- `format()` for paper format (A4, Letter, etc.)
- `paper_size()` for custom paper dimensions
- `margins()` for page margins
- `tagged()` to generate tagged (accessible) PDF
- `document_outline()` to generate a document outline (bookmarks)
- `landscape()` / `orientation()` for page orientation
- `header_view()` / `header_html()` for page headers
- `footer_view()` / `footer_html()` for page footers

Scale and page ranges are also supported. Custom paper size uses `@page` CSS and `preferCSSPageSize` rather than unsupported width and height fields.

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. Register a configured driver with `pdf.register_driver("cloudflare", driver)` during application setup:

```
use serde_json::json;
use spatie_pdf::Format;

pdf.driver("cloudflare")?
    .view("pdfs.invoice", json!({"invoice": invoice}))
    .format(Format::A4)
    .save("invoice.pdf").await?;
```

Make sure you have the Cloudflare credentials configured, even if Cloudflare is not the default driver.

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

Check the [Cloudflare Browser Run limits page](https://developers.cloudflare.com/browser-run/limits/) for the latest plan limits. The Rust adapter has a 60-second HTTP timeout and 64 MiB response limit by default; both are configurable fields.

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

- The Cloudflare driver only supports PDF output. Saving as PNG (screenshots) is not supported.
- Explicit readiness waiting is not supported by this adapter.
- JavaScript execution depends on Cloudflare's Browser Run API behavior. Complex scripts may behave differently than in a local Chromium instance.
- Redirects are not followed. HTTP errors contain the status, not potentially sensitive response bodies.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
