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

 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)

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

###  On this page

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

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

Publishing the Configuration File
-----------------------------------------------------------------------------------------------------------------------------------------------------------

To publish the configuration file, run:

```
php artisan vendor:publish --tag=pdf-config
```

This will create a `config/laravel-pdf.php` file in your application.

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

The `driver` option determines which PDF generation backend to use:

```
'driver' => env('LARAVEL_PDF_DRIVER', 'browsershot'),
```

Supported values: `browsershot`, `cloudflare`, `dompdf`, `gotenberg`, `weasyprint`.

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

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

```
'browsershot' => [
    'node_binary' => env('LARAVEL_PDF_NODE_BINARY'),
    'npm_binary' => env('LARAVEL_PDF_NPM_BINARY'),
    'include_path' => env('LARAVEL_PDF_INCLUDE_PATH'),
    'chrome_path' => env('LARAVEL_PDF_CHROME_PATH'),
    'node_modules_path' => env('LARAVEL_PDF_NODE_MODULES_PATH'),
    'bin_path' => env('LARAVEL_PDF_BIN_PATH'),
    'temp_path' => env('LARAVEL_PDF_TEMP_PATH'),
    'write_options_to_file' => env('LARAVEL_PDF_WRITE_OPTIONS_TO_FILE', false),
    'no_sandbox' => env('LARAVEL_PDF_NO_SANDBOX', false),
],
```

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

Configure the Cloudflare Browser Rendering API credentials:

```
'cloudflare' => [
    'api_token' => env('CLOUDFLARE_API_TOKEN'),
    'account_id' => env('CLOUDFLARE_ACCOUNT_ID'),
],
```

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

Configure the Gotenberg API URL:

```
'gotenberg' => [
    'url' => env('GOTENBERG_URL', 'http://localhost:3000'),
],
```

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

Configure the WeasyPrint driver options:

```
'weasyprint' => [
    'binary' => env('LARAVEL_PDF_WEASYPRINT_BINARY', 'weasyprint'),
    'timeout' => 10,
],
```

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

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

Configure the DOMPDF driver options:

```
'dompdf' => [
    'is_remote_enabled' => env('LARAVEL_PDF_DOMPDF_REMOTE_ENABLED', false),
    'chroot' => env('LARAVEL_PDF_DOMPDF_CHROOT'),
],
```

- **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.

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

You can use environment variables to configure PDF generation:

```
# Driver selection
LARAVEL_PDF_DRIVER=browsershot

# Browsershot settings
LARAVEL_PDF_NODE_BINARY=/usr/local/bin/node
LARAVEL_PDF_NPM_BINARY=/usr/local/bin/npm
LARAVEL_PDF_INCLUDE_PATH=/usr/local/bin
LARAVEL_PDF_CHROME_PATH=/usr/bin/google-chrome-stable
LARAVEL_PDF_NODE_MODULES_PATH=/path/to/node_modules
LARAVEL_PDF_BIN_PATH=/usr/local/bin
LARAVEL_PDF_TEMP_PATH=/tmp
LARAVEL_PDF_WRITE_OPTIONS_TO_FILE=true
LARAVEL_PDF_NO_SANDBOX=true

# Cloudflare settings
CLOUDFLARE_API_TOKEN=your-api-token
CLOUDFLARE_ACCOUNT_ID=your-account-id

# Gotenberg settings
GOTENBERG_URL=http://localhost:3000

# WeasyPrint settings
LARAVEL_PDF_WEASYPRINT_BINARY=/usr/local/bin/weasyprint

# DOMPDF settings
LARAVEL_PDF_DOMPDF_REMOTE_ENABLED=false
LARAVEL_PDF_DOMPDF_CHROOT=/path/to/chroot

```

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

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

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

// Use the Cloudflare driver for this specific PDF
Pdf::view('invoice', $data)
    ->driver('cloudflare')
    ->save('invoice.pdf');

// Use the default driver from config
Pdf::view('invoice', $data)
    ->save('invoice.pdf');
```

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

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

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

// This PDF will use the configuration defaults plus the scale override
Pdf::view('invoice', ['invoice' => $invoice])
    ->withBrowsershot(function (Browsershot $browsershot) {
        $browsershot->scale(0.8);
    })
    ->save('invoice.pdf');
```

The `withBrowsershot()` closure runs after configuration defaults are applied, allowing you to modify or override any setting.
