Queued PDF generation | 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)  Basic-usage  Queued PDF generation

 Version   v1      

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

  Queued PDF generation    
- [ 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)

 Queued PDF generation
=====================

###  On this page 

1. [ Basic usage ](#content-basic-usage)
2. [ Queue configuration ](#content-queue-configuration)
3. [ Replacing the generation job ](#content-replacing-the-generation-job)
4. [ Limitations ](#content-limitations)

`GeneratePdfJob` is a framework `Queueable` job registered as `spatie.pdf.generate`. Register `service_provider(factory)` in the producer and worker applications so the handler can resolve the factory and its driver configuration.

Basic usage
-----------------------------------------------------------------------------------------

Render views before dispatch, then let the framework execute the job and resolve its named storage disk:

```
pdf.html("<h1>Invoice 391</h1>").disk("local")
    .save_queued("pdfs", "invoices/391.pdf").await?;
```

The configured default queue connection is used. A `sync` connection executes immediately; a database or Redis connection waits for a worker.

Queue configuration
-----------------------------------------------------------------------------------------------------------------

For a particular connection, dispatch the typed job through the real framework API:

```
use laravel::queue;
use spatie_pdf::{PdfBuilder, testing::FakeDriver};
let job = PdfBuilder::new(FakeDriver).html("Invoice").disk("local").queued("invoice.pdf")?;
queue::dispatch(job).on_connection("database").on_queue("pdfs").await?;
```

Use framework chains and typed callback jobs for follow-ups. Framework workers own retries, acknowledgement, and terminal failure handling.

Replacing the generation job
--------------------------------------------------------------------------------------------------------------------------------------------

Wrap `GeneratePdfJob` in your own native job and select it on the factory's default builder. This applies to every builder produced by that factory, including named drivers. Your handler can call `run(&factory)` to retain driver and encrypter resolution, and can define a native `failed` hook.

```
use laravel::{container::Inject, queue::{self, Queueable, job}, time::ToSpan};
use spatie_pdf::{GeneratePdfJob, PdfBuilder, PdfFactory};

#[derive(Queueable)]
struct InvoicePdf { pdf: GeneratePdfJob }

impl From<GeneratePdfJob> for InvoicePdf {
    fn from(pdf: GeneratePdfJob) -> Self { Self { pdf } }
}

#[job(name = "app.invoice-pdf", queue = "invoices", tries = 5, timeout = 2.minutes())]
impl InvoicePdf {
    async fn handle(self, factory: Inject<PdfFactory>) -> laravel::Result<()> {
        self.pdf.run(&factory).await?;
        Ok(())
    }
}

let factory = PdfFactory::new(PdfBuilder::new(driver).job::<InvoicePdf>());
factory.html("Invoice 391").disk("local")
    .on_connection("database")
    .enqueue("invoices/391.pdf").await?;
```

`enqueue` preserves the custom job's default queue. `.on_queue("urgent")` or `save_queued("urgent", path)` overrides it. To use native chains, delays, groups, or deduplication, wrap the payload returned by `.queued(path)?` yourself and pass your job to `queue::dispatch` or `queue::chain`.

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

The worker must register a driver and encrypter whose identities match the producer. Custom post-processors cannot be serialized and are rejected by `queued`. Queue input contains rendered HTML and may contain encryption passwords; secure queue storage accordingly. Queued generation does not use the builder's cache. The framework's job test runner tests payload encoding, restoration, and execution; the package also tests dispatch through the real sync connection and saving through a real disk.

 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)
