Testing PDFs | 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  Testing PDFs

 Version   v1      

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

  Testing PDFs    
- [ 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)

 Testing PDFs
============

###  On this page 

1. [ Faked PDF contents ](#content-faked-pdf-contents)
2. [ Saved and response assertions ](#content-saved-and-response-assertions)
3. [ Input assertions ](#content-input-assertions)
4. [ Queued PDF assertions ](#content-queued-pdf-assertions)
5. [ Real engine tests ](#content-real-engine-tests)

You can test document input without starting a browser. Install one `FakeDriver` in your isolated Laravel test application:

```
fake := &pdf.FakeDriver{}
client := laraveltest.New(t, laravel.Options{
    Packages: []laravel.Package{pdf.New(func(c *pdf.Config) {
        c.Driver = "fake"
        c.Drivers = map[string]pdf.Driver{"fake": fake}
    })},
})
```

Faked PDF contents
--------------------------------------------------------------------------------------------------------------

The fake returns a valid blank one-page PDF. Unlike PHP's HTML-based fake, the bytes do not contain the input HTML. Metadata and encryption still run, so those features can be tested without Chrome. `pdf.BlankPDF()` is available for your own fake drivers.

Saved and response assertions
-----------------------------------------------------------------------------------------------------------------------------------------------

Use Laravel's HTTP assertions for the response and `laraveltest.FakeDisk` for saved files. Read disk bytes with `storage.Use(name).Get`. Assert `Content-Type: application/pdf` and the expected `Content-Disposition` through a real application route.

Input assertions
--------------------------------------------------------------------------------------------------------

```
requests := fake.Requests()
if len(requests) != 1 || !strings.Contains(requests[0].HTML, "Invoice 42") {
    t.Fatal("invoice was not rendered")
}
```

Requests include rendered body, header, footer and all formatting options. To inspect one builder before rendering, use `RenderRequest(ctx)`. `Contains(ctx, texts...)` reports whether any supplied text occurs in its rendered HTML, not in the PDF's encoded byte streams.

Queued PDF assertions
-----------------------------------------------------------------------------------------------------------------------

Use Laravel's queue fake, then inspect `Pushed[pdf.GenerateJob]()` or the configured replacement type. Run the recorded job with `client.Attempt` to exercise serialization and execution. Do not test only that a job was pushed when the important behavior is the generated document.

Real engine tests
-----------------------------------------------------------------------------------------------------------

Run `go test -race ./...` for the unit and application suite. Set `PDF_TEST_CHROME` to a browser executable, `PDF_TEST_WEASYPRINT` to WeasyPrint and `PDF_TEST_DOMPDF_AUTOLOAD` to a Composer autoloader to enable the integration tests. They use Poppler's `pdfinfo` and `pdftotext` to check real output. Without those variables, external engines are skipped, not simulated.

 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)
