Waiting for readiness | laravel-pdf | Spatie

 SPATIE

  Laravel PDF
==============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-pdf](https://spatie.be/docs/laravel-pdf/v2)  Advanced-usage  Waiting for readiness

 Version   v2   v1

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

  Waiting for readiness
- [ Introduction ](https://spatie.be/docs/laravel-pdf/v2/introduction)
- [ Support us ](https://spatie.be/docs/laravel-pdf/v2/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-pdf/v2/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-pdf/v2/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-pdf/v2/questions-issues)
- [ Alternatives ](https://spatie.be/docs/laravel-pdf/v2/alternatives)
- [ Changelog ](https://spatie.be/docs/laravel-pdf/v2/changelog)
- [ About us ](https://spatie.be/docs/laravel-pdf/v2/about-us)

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

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

Drivers
-------

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

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

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

 Waiting for readiness
=====================

###  On this page

1. [ Signalling readiness from your view ](#content-signalling-readiness-from-your-view)
2. [ Waiting for the flag ](#content-waiting-for-the-flag)
3. [ Using a custom expression ](#content-using-a-custom-expression)
4. [ Setting a timeout ](#content-setting-a-timeout)
5. [ Driver support ](#content-driver-support)

When you render JavaScript heavy views (charts, maps, web fonts, or content loaded asynchronously), the PDF can be captured before everything has finished rendering. The result is an empty chart or a missing font.

Instead of guessing with arbitrary delays, you can wait for an explicit readiness signal. Set a flag in your view once everything is done, and the PDF will only be captured after that flag becomes true.

Signalling readiness from your view
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

In your Blade view, set `window.pdfReady` to `true` once the page is ready to be captured.

```

    renderChart('#chart').then(() => {
        window.pdfReady = true;
    });

```

Waiting for the flag
--------------------------------------------------------------------------------------------------------------------

Call `waitUntilReady()` on the builder. The PDF will be captured only after `window.pdfReady === true`.

```
use Spatie\LaravelPdf\Facades\Pdf;

Pdf::view('pdf.report', ['report' => $report])
    ->waitUntilReady()
    ->save('report.pdf');
```

Using a custom expression
-----------------------------------------------------------------------------------------------------------------------------------

If you prefer your own readiness condition, pass any JavaScript expression that evaluates to a truthy value.

```
Pdf::view('pdf.report')
    ->waitUntilReady('window.charts.every(chart => chart.rendered)')
    ->save('report.pdf');
```

Setting a timeout
-----------------------------------------------------------------------------------------------------------

By default the renderer waits up to 30 seconds. You can pass a custom timeout (in milliseconds) as the second argument.

```
Pdf::view('pdf.report')
    ->waitUntilReady('window.pdfReady === true', timeout: 5000)
    ->save('report.pdf');
```

Driver support
--------------------------------------------------------------------------------------------------

Waiting for readiness requires a driver that can execute JavaScript. It is supported by the [Browsershot](/docs/laravel-pdf/v2/drivers/customizing-browsershot), [Chrome](/docs/laravel-pdf/v2/drivers/using-the-chrome-driver), and [Gotenberg](/docs/laravel-pdf/v2/drivers/using-the-gotenberg-driver) drivers.

When using Gotenberg, the readiness expression is passed as its `waitForExpression` field, and the timeout is applied to the HTTP request so it does not abort before Gotenberg finishes rendering.

Calling `waitUntilReady()` while a driver that cannot run JavaScript is active (such as DOMPDF or WeasyPrint) throws a `CouldNotGeneratePdf` exception.

 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)
