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

 Version   v1      

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

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

###  On this page 

1. [ Getting started ](#content-getting-started)
2. [ Supported options ](#content-supported-options)
3. [ Waiting for readiness ](#content-waiting-for-readiness)
4. [ Headers and footers ](#content-headers-and-footers)

The Gotenberg driver uses [Gotenberg](https://gotenberg.dev) to generate PDFs. Gotenberg is an open-source, Docker-based API for converting HTML, Markdown, and Office documents to PDF. It runs as a standalone service using headless Chromium under the hood.

This is a great choice when you want to offload PDF generation to a separate service, especially in containerized or microservice environments.

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

1. Start a Gotenberg instance using Docker:

```
docker run --rm -p 3000:3000 gotenberg/gotenberg:8
```

Or add it to your `docker-compose.yml`:

```
services:
  gotenberg:
    image: gotenberg/gotenberg:8
    ports:
      - "3000:3000"
```

2. Set the driver on your application's default builder:

```
use spatie_pdf::drivers::GotenbergDriver;

pdf.defaults(pdf.builder().set_driver(GotenbergDriver::new("http://localhost:3000")));
```

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

- `format()` for paper format (A4, Letter, etc.)
- `paper_size()` for custom paper dimensions
- `margins()` for page margins
- `landscape()` / `orientation()` for page orientation
- `scale()` for page rendering scale
- `page_ranges()` for specific pages to include
- `tagged()` to generate tagged (accessible) PDF
- `document_outline()` to generate a document outline (bookmarks)
- `header_view()` / `header_html()` for page headers (repeated on every page)
- `footer_view()` / `footer_html()` for page footers (repeated on every page)
- `wait_until_ready()` to wait for a JavaScript readiness signal before capturing

Waiting for readiness
-----------------------------------------------------------------------------------------------------------------------

Because Gotenberg renders with headless Chromium, it can wait for a JavaScript expression to become truthy before capturing the PDF. Call `wait_until_ready()` on the builder to defer capturing until your view signals it is done rendering:

```
use serde_json::json;

pdf.view("pdfs.report", json!({"report": report}))
    .wait_until_ready()
    .save("report.pdf").await?;
```

The readiness expression is sent to Gotenberg as its `waitForExpression` field. The HTTP timeout has at least the requested timeout plus five seconds of headroom. Configure the service's API timeout to allow enough time too. See [Waiting for readiness](../advanced-usage/waiting-for-readiness) for the full details.

Headers and footers
-----------------------------------------------------------------------------------------------------------------

Gotenberg supports repeating headers and footers on every page, just like the Browsershot and Cloudflare drivers. Header and footer HTML is sent as separate HTML documents to Gotenberg.

Note that Gotenberg requires header and footer HTML to be complete HTML documents. You can use CSS classes like `pageNumber` and `totalPages` for dynamic content:

```
<html>
<head><style>p { font-size: 10px; }</style></head>
<body>
    <p>Page <span class="pageNumber"></span> of <span class="totalPages"></span></p>
</body>
</html>
```

Headers and footers do not inherit CSS from the main HTML. Set their font size explicitly and leave enough space in the top and bottom margins.

The adapter has a 60-second HTTP timeout and 64 MiB response limit by default. Redirects are not followed. Use `basic_auth` on the driver if your service requires authentication.

 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)
