Customizing Browsershot | 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)  Drivers  Customizing Browsershot

 Version   v1      

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

  Customizing Browsershot    
- [ 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)

 Customizing Browsershot
=======================

###  On this page 

1. [ Configuration-based customization ](#content-configuration-based-customization)
2. [ Per-PDF customization ](#content-per-pdf-customization)
3. [ Advanced Browsershot configuration ](#content-advanced-browsershot-configuration)
4. [ Testing your Browsershot configuration ](#content-testing-your-browsershot-configuration)

The default driver uses [Browsershot Go](https://github.com/spatie/browsershot-go). You can customize its process configuration globally or the browser builder for one PDF.

Configuration-based customization
-----------------------------------------------------------------------------------------------------------------------------------------------------------

`Config.Browsershot` configures the Node binary, npm binary, Chrome path, Node modules path, include path, custom runner script, temporary directory, option-file writing, sandbox and timeout. These correspond to the PHP driver's process configuration. The embedded Node runner is used unless you select a custom one.

```
pdf.New(func(c *pdf.Config) {
    c.Browsershot.NodeBinary = "/usr/local/bin/node"
    c.Browsershot.ChromePath = "/usr/bin/chromium"
    c.Browsershot.NodeModulesPath = "/app/node_modules"
    c.Browsershot.Timeout = time.Minute
})
```

Environment settings use the same `LARAVEL_PDF_` names as PHP. A custom path should be absolute. Install Puppeteer where the configured Node process can resolve it. Browsershot needs Node 22.12+, Puppeteer 23+ and Chrome, not a PHP runtime.

Per-PDF customization
-----------------------------------------------------------------------------------------------------------------------

Use `WithBrowsershot` with a function taking `*browsershot.Browsershot`. It receives a fresh builder with the document's HTML and PDF options already applied. Your callback can override options, use a remote browser or supply a custom runner.

```
import browsershot "github.com/spatie/browsershot-go"

err := pdf.From(ctx).HTML("<h1>Invoice</h1>").
    WithBrowsershot(func(b *browsershot.Browsershot) {
        b.SetContentURL("https://example.com").WaitUntilNetworkIdle()
    }).Save(ctx, "invoice.pdf")
```

Callbacks cannot be serialized. They are rejected by queued generation. For caching, supply an explicit key that changes when the callback's behavior changes. See [caching PDFs](../advanced-usage/caching-pdfs).

`WithBrowsershot` returns an unsupported-option error if the selected driver is not `BrowsershotDriver`. Supplied HTML blocks local file access by default. Use Browsershot's `AllowLocalFileAccess` only for trusted documents.

Advanced Browsershot configuration
--------------------------------------------------------------------------------------------------------------------------------------------------------------

Use Browsershot's `SetOption` for raw Puppeteer options, `SetRemoteInstance` or `SetWSEndpoint` for a remote browser, and `WithRunner` for a different execution service. Remote connection failures can fall back to local Chrome unless you enable `ThrowOnRemoteConnectionError`.

Set `Config.Browsershot.Customize` for a global callback. When caching or queuing, also set `Config.Browsershot.ConfigurationKey` to a stable value that changes with every output-affecting change to the callback. Without that key, cached and queued generation return an error. Workers must configure the same callback and key as producers. Creating a new document always creates a new Browsershot instance, so mutable builder state is not shared between requests.

Testing your Browsershot configuration
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Use a custom Browsershot runner to inspect its exported `Command` and return a result without starting Chrome. For full output checks, run with Node, Puppeteer and a real Chrome executable. This package's engine suite checks the generated PDF with Poppler rather than only asserting that PDF bytes were returned.

 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)
