Configuration | 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  Configuration

 Version   v1      

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

  Configuration    
- [ 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)

 Configuration
=============

###  On this page 

1. [ Registering configuration ](#content-registering-configuration)
2. [ Selecting a Driver ](#content-selecting-a-driver)
3. [ Browsershot Configuration ](#content-browsershot-configuration)
4. [ Cloudflare Configuration ](#content-cloudflare-configuration)
5. [ Chrome Configuration ](#content-chrome-configuration)
6. [ Gotenberg Configuration ](#content-gotenberg-configuration)
7. [ WeasyPrint Configuration ](#content-weasyprint-configuration)
8. [ DOMPDF Configuration ](#content-dompdf-configuration)
9. [ Environment Variables ](#content-environment-variables)
10. [ Runtime Driver Switching ](#content-runtime-driver-switching)
11. [ Overriding Browsershot Configuration ](#content-overriding-browsershot-configuration)

PDF supports configuration-based customization, allowing you to set default options that apply to all PDF generation in your application.

Registering configuration
-----------------------------------------------------------------------------------------------------------------------------------

Register `service_provider(factory)` to bind an application-local `PdfFactory` and a typed `PdfConfig` section. The provider reads environment defaults before application config files run. Application container bindings can replace the factory entirely.

```
use laravel::{Application, config::Env};
use spatie_pdf::{PdfBuilder, PdfConfig, PdfFactory, service_provider};
use spatie_pdf::drivers::{ChromeConfig, ChromeDriver};

let app = Application::configure()
    .replace_env(Env::default())
    .with_providers(|providers| {
        let factory = PdfFactory::new(PdfBuilder::new(
            ChromeDriver::trusted_network(ChromeConfig::default()),
        ));
        providers.add(service_provider(factory));
    })
    .with_config(|config, _| {
        let pdf = config.section_mut::<PdfConfig>();
        pdf.cache.automatic = true;
        pdf.cache.prefix = "invoices".into();
        pdf.chrome.flags.push("--disable-extensions".into());
    })
    .create();
```

Set `pdf.driver = Some("chrome".into())` to use the section's Chrome settings instead of an explicitly supplied driver. Without a selected name, the factory's driver is unchanged.

Selecting a Driver
--------------------------------------------------------------------------------------------------------------

Choose the default builder explicitly:

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

let pdf = PdfFactory::new(PdfBuilder::new(
    ChromeDriver::trusted_network(ChromeConfig::default()),
));
```

Register names with `register_driver` to support per-call selection. An unknown driver name is an error.

Browsershot Configuration
-----------------------------------------------------------------------------------------------------------------------------------

Configure paths to Node.js, npm, Chrome, and other binaries used by the Browsershot driver:

```
use serde_json::json;
use spatie_pdf::drivers::{PhpBridgeDriver, PhpEngine};

let mut driver = PhpBridgeDriver::new(PhpEngine::Browsershot, "/app/vendor/autoload.php");
driver.config = json!({
    "node_binary": "/usr/local/bin/node",
    "npm_binary": "/usr/local/bin/npm",
    "include_path": "/usr/local/bin",
    "chrome_path": "/usr/bin/google-chrome-stable",
    "node_modules_path": "/app/node_modules",
    "bin_path": "/app/vendor/spatie/browsershot/bin/browser.cjs",
    "temp_path": "/app/storage/tmp",
    "write_options_to_file": true,
    "no_sandbox": false,
});
```

Cloudflare Configuration
--------------------------------------------------------------------------------------------------------------------------------

Configure the Cloudflare Browser Rendering API credentials:

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

let driver = CloudflareDriver::new(account_id, api_token)?;
```

The driver stores credentials privately, does not follow redirects, and omits remote response bodies from errors.

Chrome Configuration
--------------------------------------------------------------------------------------------------------------------

Configure the Chrome driver 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**: Path to the Chrome or Chromium executable. Defaults to `chromium` on `$PATH`, rather than auto-discovery.
- **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.
- **operation\_timeout**: Maximum time to wait for Chrome operations, including setting HTML and generating PDF output.
- **flags**: Additional Chrome command-line flags.
- **environment**: Environment variables passed to the Chrome process.

Timeouts use `Duration`. A fresh profile is used for every render rather than a shared `user_data_dir`. There is no separate HTML-loading timeout. See [the Chrome driver](using-the-chrome-driver) for defaults.

Gotenberg Configuration
-----------------------------------------------------------------------------------------------------------------------------

Configure the Gotenberg API URL:

```
use spatie_pdf::drivers::GotenbergDriver;

let driver = GotenbergDriver::new("http://localhost:3000");
```

Use `basic_auth` if the service requires it. Set `timeout` and `max_bytes` to your deployment's limits.

WeasyPrint Configuration
--------------------------------------------------------------------------------------------------------------------------------

Configure the WeasyPrint driver options:

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

let driver = WeasyPrintDriver {
    binary: "weasyprint".into(),
    timeout: Duration::from_secs(10),
    ..Default::default()
};
```

- **binary**: Path to the WeasyPrint binary. Defaults to `weasyprint` (found via `$PATH`).
- **timeout**: Maximum time for PDF generation. Defaults to 30 seconds.

Additional arguments can be passed through the driver's `arguments` field.

DOMPDF Configuration
--------------------------------------------------------------------------------------------------------------------

Configure the DOMPDF driver options:

```
use serde_json::json;
use spatie_pdf::drivers::{PhpBridgeDriver, PhpEngine};

let mut driver = PhpBridgeDriver::new(PhpEngine::Dompdf, "/app/vendor/autoload.php");
driver.config = json!({
    "is_remote_enabled": false,
    "chroot": "/app/public",
});
```

- **is\_remote\_enabled**: Set to `true` to allow DOMPDF to fetch external resources (images, CSS) via URLs.
- **chroot**: The base path for local file access. Defaults to DOMPDF's built-in setting.

Remote loading is disabled by default. You can also configure `font_dir` and `font_cache`; see [the DOMPDF driver](using-the-dompdf-driver).

Environment Variables
-----------------------------------------------------------------------------------------------------------------------

The provider reads these variables through `Env`, without changing process-global variables:

- `LARAVEL_PDF_DRIVER`: `chrome`, `browsershot`, `dompdf`, `weasyprint`, `cloudflare`, `gotenberg`, `lambda`, or a name registered on your factory. Unset preserves the supplied factory driver.
- `LARAVEL_PDF_CACHE_AUTOMATIC`, `LARAVEL_PDF_CACHE_STORE`, and `LARAVEL_PDF_CACHE_TTL` (seconds). Defaults are false, the framework's default store, and 86400 seconds. `.dont_cache()` opts out per document; `.cache_for()` overrides the lifetime.
- `LARAVEL_PDF_CHROME_BINARY`, `LARAVEL_PDF_CHROME_NO_SANDBOX`, `LARAVEL_PDF_CHROME_STARTUP_TIMEOUT` (seconds), and `LARAVEL_PDF_CHROME_OPERATION_TIMEOUT` (milliseconds).
- `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`.
- `GOTENBERG_URL`, `GOTENBERG_USERNAME`, and `GOTENBERG_PASSWORD`.
- `LARAVEL_PDF_WEASYPRINT_BINARY`.
- `LARAVEL_PDF_PHP_BINARY` and `LARAVEL_PDF_PHP_AUTOLOAD` for the PHP bridge.
- `LARAVEL_PDF_NODE_BINARY`, `LARAVEL_PDF_NPM_BINARY`, `LARAVEL_PDF_INCLUDE_PATH`, `LARAVEL_PDF_CHROME_PATH`, `LARAVEL_PDF_NODE_MODULES_PATH`, `LARAVEL_PDF_BIN_PATH`, `LARAVEL_PDF_TEMP_PATH`, `LARAVEL_PDF_WRITE_OPTIONS_TO_FILE`, and `LARAVEL_PDF_NO_SANDBOX` for Browsershot and Lambda.
- `LARAVEL_PDF_DOMPDF_REMOTE_ENABLED`, `LARAVEL_PDF_DOMPDF_CHROOT`, `LARAVEL_PDF_DOMPDF_FONT_DIR`, and `LARAVEL_PDF_DOMPDF_FONT_CACHE`.

Use the typed section for flags, child-process environment, WeasyPrint timeout/arguments, and other settings without environment variables. Invalid booleans and numeric values cause framework startup validation to fail. Invalid selected drivers or missing selected cloud credentials fail factory resolution; unselected cloud drivers need no credentials.

The native Chrome driver always uses a temporary profile. It has one operation timeout rather than a separate HTML-loading timeout. Configure queue job types with `PdfBuilder::job::<YourJob>()` and encryption implementations with `.encrypter(...)` on factory defaults. `PdfFactory::decrypt` and `decrypt_file` use that configured encrypter.

Runtime Driver Switching
--------------------------------------------------------------------------------------------------------------------------------

You can switch drivers on a per-PDF basis using the `driver` method:

```
// Use the Cloudflare driver for this specific PDF
pdf.driver("cloudflare")?
    .view("invoice", data.clone())
    .save("invoice.pdf").await?;

// Use the factory's default driver
pdf.view("invoice", data)
    .save("invoice.pdf").await?;
```

Select a registered driver before calling `view`, or call `set_driver(driver)` on an existing builder. Builder options and factory defaults are retained when switching drivers. Unsupported settings return errors where checked by the adapter.

Overriding Browsershot Configuration
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

When using the Browsershot driver, PDF option defaults can be overridden on a per-PDF basis using the `browser_options()` method:

```
use serde_json::json;

// This PDF will use the configuration defaults plus the scale override
pdf.view("invoice", json!({"invoice": invoice}))
    .browser_options(|options| {
        options.insert("scale".into(), json!(0.8));
    })
    .save("invoice.pdf").await?;
```

These options override mapped PDF defaults. They are serialized and included in cache fingerprints and queue payloads. Application-wide bridge configuration belongs to the driver and must match between producer and worker.

 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)
