Custom drivers | 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  Custom drivers

 Version   v1      

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

  Custom drivers    
- [ 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)

 Custom drivers
==============

###  On this page 

1. [ Creating a driver ](#content-creating-a-driver)
2. [ Registering a driver ](#content-registering-a-driver)
3. [ Using the driver ](#content-using-the-driver)

You can create your own PDF generation driver by implementing the `PdfDriver` trait. This allows you to integrate any PDF generation service or library.

Creating a driver
-----------------------------------------------------------------------------------------------------------

A driver must implement the `PdfDriver` trait. For example, this driver delegates to WeasyPrint; replace the method body with your renderer's implementation:

```
use spatie_pdf::{drivers::PdfDriver, RenderRequest, Result};

struct WeasyPrintDriver {
    driver: spatie_pdf::drivers::WeasyPrintDriver,
}

impl PdfDriver for WeasyPrintDriver {
    fn generate_pdf(&self, request: &RenderRequest) -> Result<Vec<u8>> {
        self.driver.generate_pdf(request)
    }

    fn cache_identity(&self) -> String {
        self.driver.cache_identity()
    }
}
```

`RenderRequest` contains the rendered HTML, optional header and footer HTML, and `PdfOptions`. The `PdfOptions` object provides access to the formatting options set on the builder:

- `options.format`: paper format like `A4`, `Letter`, etc. (or `None`)
- `options.paper_size`: width, height, and unit (or `None`)
- `options.margins`: top, right, bottom, left, and unit (or `None`)
- `options.orientation`: `Landscape` or `Portrait`

The default `save_pdf` implementation writes the generated bytes. Override it if your renderer has a dedicated save operation. `cache_identity` must distinguish all output-affecting driver configuration. Hash credentials rather than returning them as readable text. Override `supports_readiness` only if the driver actually waits for the expression.

Registering a driver
--------------------------------------------------------------------------------------------------------------------

Register your driver on your application's factory in a service provider:

```
pdf.register_driver("weasyprint", driver);
```

There is no global registry. An unknown driver name returns an error.

Using the driver
--------------------------------------------------------------------------------------------------------

Once registered, you can use the driver on a per-PDF basis:

```
use serde_json::json;

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

To make it the default driver, set it on your factory's default builder in your service provider:

```
pdf.defaults(pdf.builder().set_driver(driver));
```

 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)
