Using the Chrome 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 Chrome driver

 Version   v1      

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

  Using the Chrome 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 Chrome driver
=======================

###  On this page 

1. [ Getting started ](#content-getting-started)
2. [ Supported options ](#content-supported-options)
3. [ Configuration ](#content-configuration)
4. [ Using Chrome for specific PDFs only ](#content-using-chrome-for-specific-pdfs-only)
5. [ Limitations ](#content-limitations)

The Chrome driver uses the DevTools protocol to generate PDFs directly from Rust. It is a good fit when you want a Chromium-based driver without bringing in Node.js or Puppeteer.

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

Make sure Chrome or Chromium is installed on the machine that generates the PDFs. The driver does not download or bundle a browser for you.

Set the driver on your application's default builder:

```
use spatie_pdf::drivers::{ChromeConfig, ChromeDriver};

pdf.defaults(pdf.builder().set_driver(
    ChromeDriver::trusted_network(ChromeConfig::default()),
));
```

That's it. Your existing PDF code will now use the Chrome driver:

```
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 Chrome driver supports the following PDF options:

- `format()` for paper format (A4, Letter, Legal, etc.)
- `paper_size()` for custom paper dimensions
- `margins()` for page margins
- `landscape()` / `orientation()` for page orientation
- `scale()` for page rendering scale
- `page_ranges()` for specific pages to include
- `header_view()` / `header_html()` for page headers
- `footer_view()` / `footer_html()` for page footers

It also supports readiness waiting. Current Chromium supports tagged PDFs and document outlines through DevTools flags. Unsupported browser versions return an error instead of silently ignoring those flags.

Configuration
-----------------------------------------------------------------------------------------------

The Chrome driver accepts these configuration options:

```
use spatie_pdf::drivers::ChromeConfig;
use std::time::Duration;

let config = ChromeConfig {
    binary: "/usr/bin/google-chrome-stable".into(),
    no_sandbox: false,
    startup_timeout: Duration::from_secs(30),
    operation_timeout: Duration::from_millis(5000),
    flags: vec![],
    environment: Default::default(),
};
```

- **binary**: The path to the Chrome or Chromium executable. Defaults to `chromium` on `$PATH`; configure the path on macOS and Windows.
- **no\_sandbox**: Disables Chrome's sandbox. Keep this false unless your already isolated worker cannot use Chromium's sandbox.
- **startup\_timeout**: Maximum time to wait for Chrome to start. Defaults to 30 seconds.
- **operation\_timeout**: Maximum time to wait for Chrome operations, including setting HTML and generating PDF output. Defaults to 30 seconds.
- **flags**: Additional Chrome command-line flags.
- **environment**: Environment variables passed to the Chrome process.

A fresh temporary profile is used for every render. Persistent user profiles are deliberately not shared between renders, and there is no separate HTML-loading timeout.

Using Chrome for specific PDFs only
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

If you want to use another driver as your default but switch to Chrome for specific PDFs, use the `driver` method. Register the configured driver with `pdf.register_driver("chrome", driver)` during application setup:

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

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

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

- Chrome can load URLs and local resources from supplied HTML. `trusted_network` is an explicit acknowledgement, not an SSRF filter. Run untrusted documents in an isolated worker with restricted filesystem and egress.
- In Docker or other locked-down environments, you may need to enable `no_sandbox`.
- This driver only documents and exposes the options supported by this package. If you need deeper Chromium customization, use the Browsershot driver instead.
- Puppeteer-specific overrides are rejected. Use `ChromeConfig` for process configuration.

 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)
