Queued PDF generation | laravel-pdf-go | Spatie       

 SPATIE  

laravel-pdf-go
==============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-pdf-go](https://spatie.be/docs/laravel-pdf-go/v1)  Basic-usage  Queued PDF generation

 Version   v1      

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

  Queued PDF generation    
- [ Introduction ](https://spatie.be/docs/laravel-pdf-go/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-pdf-go/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-pdf-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-pdf-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-pdf-go/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-pdf-go/v1/changelog)
- [ Alternatives ](https://spatie.be/docs/laravel-pdf-go/v1/alternatives)
- [ About us ](https://spatie.be/docs/laravel-pdf-go/v1/about-us)
- [ Testing ](https://spatie.be/docs/laravel-pdf-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-pdf-go/v1/releasing)

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

- [ Creating PDFs ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/creating-pdfs)
- [ Responding with PDFs ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/responding-with-pdfs)
- [ Formatting PDFs ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/formatting-pdfs)
- [ Saving PDFs to disks ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/saving-pdfs-to-disks)
- [ Attaching PDFs to mails ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/attaching-pdfs-to-mails)
- [ Queued PDF generation ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/queued-pdf-generation)
- [ Testing PDFs ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/testing-pdfs)
- [ Setting defaults ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/setting-defaults)
- [ Protecting PDFs with a password ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/protecting-pdfs-with-a-password)

Drivers
-------

- [ Configuration ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/configuration)
- [ Customizing Browsershot ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/customizing-browsershot)
- [ Using the Cloudflare driver ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/using-the-cloudflare-driver)
- [ Using the DOMPDF driver ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/using-the-dompdf-driver)
- [ Generating PDFs on AWS Lambda ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/generating-pdfs-on-aws-lambda)
- [ Using the Chrome driver ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/using-the-chrome-driver)
- [ Using the Gotenberg driver ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/using-the-gotenberg-driver)
- [ Using the WeasyPrint driver ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/using-the-weasyprint-driver)
- [ Custom drivers ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/custom-drivers)

Advanced usage
--------------

- [ Creating PDFs with multiple pages ](https://spatie.be/docs/laravel-pdf-go/v1/advanced-usage/creating-pdfs-with-multiple-pages)
- [ Using Tailwind ](https://spatie.be/docs/laravel-pdf-go/v1/advanced-usage/using-tailwind)
- [ Extending with Macros ](https://spatie.be/docs/laravel-pdf-go/v1/advanced-usage/extending-with-macros)
- [ Waiting for readiness ](https://spatie.be/docs/laravel-pdf-go/v1/advanced-usage/waiting-for-readiness)
- [ Caching PDFs ](https://spatie.be/docs/laravel-pdf-go/v1/advanced-usage/caching-pdfs)

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

###  On this page 

1. [ Basic usage ](#content-basic-usage)
2. [ Callbacks ](#content-callbacks)
3. [ Queue configuration ](#content-queue-configuration)
4. [ Saving to a disk ](#content-saving-to-a-disk)
5. [ Customizing the job ](#content-customizing-the-job)
6. [ Limitations ](#content-limitations)
7. [ Testing ](#content-testing)

Generating a PDF can be resource intensive. Use `SaveQueued` to move that work to a Laravel queue worker.

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

```
id, err := pdf.From(ctx).View("pdf/invoice", invoice).
    SaveQueued(ctx, "invoices/42.pdf")
```

Views render before dispatch. The job carries HTML, headers, footers, options, metadata, encryption and destination. Workers need the same named driver and encrypter configuration as producers. A mismatched identity fails explicitly rather than rendering with a different backend.

Callbacks
-----------------------------------------------------------------------------------

Go does not serialize function closures. Use typed jobs in a native chain for success follow-up, and a custom job's `Failed(context.Context, error) error` method for failure handling:

```
job, err := pdf.From(ctx).View("pdf/invoice", invoice).
    Disk("private").Job(ctx, "invoices/42.pdf")
if err != nil {
    return err
}
_, err = queue.NewChain(job, SendInvoice{InvoiceID: 42}).Dispatch(ctx)
```

Register `SendInvoice` with your application's registrar. It should use the same path and disk. A later job runs only after PDF generation succeeds.

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

```
id, err := pdf.From(ctx).HTML("<h1>Invoice</h1>").
    SaveQueued(ctx, "invoice.pdf", queue.OnQueue("pdfs").Delay(time.Minute))
```

The optional `*queue.PendingDispatch` also supports Laravel's connection selection. Configure queue persistence and run workers using Laravel Go's queue documentation. Its default test queue runs synchronously unless you install a queue fake.

Saving to a disk
--------------------------------------------------------------------------------------------------------

Use `Disk("private")` before `SaveQueued`. A local path is relative to the worker, not the producer. Shared storage is usually a better destination when workers run on separate hosts.

Customizing the job
-----------------------------------------------------------------------------------------------------------------

Replace `Config.JobFactory`. The package registers the returned concrete type as well as using it during dispatch:

```
type InvoicePDFJob struct {
    PDF pdf.GenerateJob `json:"pdf"`
}

func (j InvoicePDFJob) Handle(ctx context.Context) error {
    return j.PDF.Handle(ctx)
}

func (InvoicePDFJob) JobOptions() queue.Options {
    return queue.Options{Tries: 3, Timeout: 2 * time.Minute}
}

// In package configuration
c.JobFactory = func(j pdf.GenerateJob) any { return InvoicePDFJob{PDF: j} }
```

The factory must return the same serializable job type for every input, including the empty registration value. Put application-specific fields, retry policies, failure handling and pre/post work on that type.

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

Named drivers can be queued. Per-document driver instances, Browsershot callbacks and post-processors cannot be serialized and are rejected. Configure them globally through named drivers instead. Queue payloads can contain PDF passwords and personal data. Use a protected queue backend. Like PHP, queued generation bypasses render caching.

Testing
-----------------------------------------------------------------------------

Use `queuetest.New(client)` to record jobs. Query `queued.Pushed[pdf.GenerateJob]()` or your configured replacement type, then run `client.Attempt(job).AssertSucceeded()` to test the worker path. The package tests cover serialization, encryption, metadata, named storage and a replacement job type.

 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)
