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

###  On this page

1. [ Getting started ](#content-getting-started)
2. [ Supported options ](#content-supported-options)
3. [ Configuration ](#content-configuration)
4. [ Using DOMPDF for specific PDFs only ](#content-using-dompdf-for-specific-pdfs-only)
5. [ How margins work ](#content-how-margins-work)
6. [ How headers and footers work ](#content-how-headers-and-footers-work)
7. [ Limitations ](#content-limitations)

The DOMPDF driver uses [dompdf/dompdf](https://github.com/dompdf/dompdf) to generate PDFs entirely in PHP. Unlike the Browsershot and Cloudflare drivers, it does not require Node.js, Chrome, Docker, or any external service. It works everywhere PHP runs.

This makes it a great choice for simple PDFs, shared hosting environments, or situations where you can't install external binaries.

**Note:** DOMPDF does not support all CSS. It handles CSS 2.1 and some CSS 3 properties, but modern layout features like flexbox and grid are not supported. Use tables or floats for layout. If you need full CSS support, use the Browsershot or Cloudflare driver instead.

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

Install the dompdf package:

```
composer require dompdf/dompdf
```

Then set the driver in your `.env` file:

```
LARAVEL_PDF_DRIVER=dompdf
```

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

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

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

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

The DOMPDF driver supports the following PDF options:

- `format()` — Paper format (a4, letter, legal, etc.)
- `paperSize()` — Custom paper dimensions
- `margins()` — Page margins (injected as `@page` CSS)
- `landscape()` / `orientation()` — Page orientation
- `headerHtml()` / `headerView()` — Prepended to the body content
- `footerHtml()` / `footerView()` — Appended to the body content

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

The DOMPDF driver accepts these configuration options in `config/laravel-pdf.php`:

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

- **is\_remote\_enabled**: Set to `true` if your HTML references external images or CSS files via URLs.
- **chroot**: The base path for local file access. Defaults to DOMPDF's built-in setting.

Using DOMPDF for specific PDFs only
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

If you want to use another driver as your default but switch to DOMPDF for specific PDFs, use the `driver` method:

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

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

How margins work
--------------------------------------------------------------------------------------------------------

DOMPDF does not have a dedicated margin API. Instead, this driver injects an `@page { margin: ... }` CSS rule into your HTML. This means margins are applied via CSS, which works well for most use cases.

How headers and footers work
--------------------------------------------------------------------------------------------------------------------------------------------

Unlike the Chromium-based drivers, DOMPDF does not support repeating HTML headers and footers on every page. Instead, the header HTML is prepended and the footer HTML is appended to the body content. They will appear at the top and bottom of the first page.

If you need repeating headers and footers on every page, consider using the Browsershot or Cloudflare driver instead.

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

- **CSS support**: DOMPDF supports CSS 2.1 and some CSS 3 properties, but does not support modern layout features like flexbox or grid. Use tables or floats for layout.
- **JavaScript**: DOMPDF does not execute JavaScript. All content must be static HTML/CSS.
- **Headers/footers**: Not repeated on every page — they are prepended/appended to the body.
- **External resources**: Fetching external images and CSS is disabled by default. Set `is_remote_enabled` to `true` in the config if needed.
- The `withBrowsershot()` and `onLambda()` methods have no effect when using the DOMPDF driver.
