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

 Version   v1      

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

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

###  On this page 

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

The WeasyPrint driver uses [WeasyPrint](https://doc.courtbouillon.org/weasyprint/stable/) to generate PDFs. WeasyPrint is a Python-based PDF generation tool with excellent [CSS Paged Media](https://www.w3.org/TR/css-page-3/) support, making it a strong choice for documents that need repeating headers and footers, page counters, and fine-grained page layout control.

Unlike the Browsershot and Cloudflare drivers, it does not use a headless browser. Unlike DOMPDF, it supports modern CSS features beyond CSS 2.1.

It sits in a sweet spot between the two: no browser required, but with much better CSS support than DOMPDF.

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

First, install WeasyPrint on your system. Follow the [official installation guide](https://doc.courtbouillon.org/weasyprint/stable/first_steps.html) for your operating system.

Set the driver on your application's default builder:

```
use spatie_pdf::drivers::WeasyPrintDriver;

pdf.defaults(pdf.builder().set_driver(WeasyPrintDriver::default()));
```

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

- `format()` for paper format (A4, Letter, etc.)
- `paper_size()` for custom paper dimensions
- `margins()` for page margins (injected as `@page` CSS)
- `landscape()` / `orientation()` for page orientation
- `header_html()` / `header_view()` for repeating page headers via CSS Paged Media
- `footer_html()` / `footer_view()` for repeating page footers via CSS Paged Media

Tagged output requires a WeasyPrint version with `--pdf-tags`. Heading bookmarks are produced by WeasyPrint by default.

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

The WeasyPrint driver accepts these configuration 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`, which uses the system `$PATH`.
- **timeout**: Maximum time for PDF generation. Defaults to 30 seconds.
- **arguments**: Additional CLI arguments.

The process receives HTML through stdin, not a shell command. The adapter creates a private temporary stylesheet and deletes it after the render.

Using WeasyPrint for specific PDFs only
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

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

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

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

WeasyPrint supports repeating headers and footers on every page using CSS Paged Media. The driver wraps your content in elements that use CSS `position: running()` and places them in `@page` margin boxes. This means headers and footers will repeat on every page automatically.

Page numbers and total pages use CSS counters (`counter(page)` and `counter(pages)`).

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

- WeasyPrint supports CSS 2.1 and large parts of CSS 3, including Paged Media. It does not support JavaScript or every browser layout feature.
- Scale, page ranges, browser overrides, and readiness return an error rather than being silently ignored.
- HTML can reference local files and network URLs. Use only trusted documents or a worker with restricted filesystem and egress.
- This is a direct binary adapter. No PHP wrapper is required.

 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)
