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

 Version   v1      

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

  Using the DOMPDF 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 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. The Rust adapter calls it through the PHP bridge.

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

**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 PHP 8.2+ and the dompdf package in your application:

```
composer require dompdf/dompdf
```

Supply its Composer autoloader to the bridge and set it on your default builder:

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

pdf.defaults(pdf.builder().set_driver(
    PhpBridgeDriver::new(PhpEngine::Dompdf, "/app/vendor/autoload.php"),
));
```

That's it. Your existing PDF code will now use DOMPDF 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 DOMPDF driver supports the following PDF options:

- `format()` for paper format (A4, Letter, Legal, etc.)
- `paper_size()` for custom paper dimensions
- `margins()` for page margins (injected as `@page` CSS)
- `landscape()` / `orientation()` for page orientation
- `header_html()` / `header_view()` prepended to the body content
- `footer_html()` / `footer_view()` appended to the body content

Metadata and encryption run after rendering, as with all drivers.

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

The DOMPDF driver accepts these configuration options on the PHP bridge:

```
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",
    "font_dir": "/app/storage/fonts",
    "font_cache": "/app/storage/fonts",
});
```

- **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. Restrict it to the assets the document is allowed to read.
- **font\_dir**: The folder where DOMPDF stores fonts loaded via `@font-face`. Point it outside `vendor`, for example to your application's `storage/fonts`, so DOMPDF never writes into your vendor folder at runtime. The folder must exist and be writable.
- **font\_cache**: The folder where DOMPDF caches font metrics. Defaults to `font_dir`.

Configure `php`, `autoload`, and `timeout` on the driver.

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

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

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

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 are not Chromium's isolated repeating header and footer templates.

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

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

- DOMPDF supports CSS 2.1 and some CSS 3, not JavaScript, grid or full flexbox.
- Browser-only settings (scale, ranges, tags, outline, readiness and Puppeteer overrides) return an error.
- This adapter executes the installed DOMPDF engine through PHP.

 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)
