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

 Version   v1      

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

  Formatting 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)

 Formatting PDFs
===============

###  On this page 

1. [ Setting a header and footer ](#content-setting-a-header-and-footer)
2. [ Page orientation ](#content-page-orientation)
3. [ Paper format ](#content-paper-format)
4. [ Paper size ](#content-paper-size)
5. [ Page margins ](#content-page-margins)
6. [ Scale ](#content-scale)
7. [ Page ranges ](#content-page-ranges)
8. [ Tagged PDF ](#content-tagged-pdf)
9. [ Document outline ](#content-document-outline)
10. [ PDF metadata ](#content-pdf-metadata)
11. [ Conditional formatting ](#content-conditional-formatting)
12. [ Debugging ](#content-debugging)
13. [ Background color ](#content-background-color)

Setting a header and footer
-----------------------------------------------------------------------------------------------------------------------------------------

Use `HeaderView` and `FooterView` for application templates, or `HeaderHTML` and `FooterHTML` for HTML strings:

```
b := pdf.From(ctx).View("pdf/invoice", invoice).
    HeaderView("pdf/header", company).
    FooterHTML(`<div style="font-size:10px">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>`).
    Margins(20, 12, 20, 12, pdf.Millimeter)
```

The `pageNumber` and `totalPages` template functions produce those spans. Browser headers and footers have separate styles from the body. Give them an explicit font size and enough page margin. DOMPDF inserts header and footer blocks into the document flow, rather than repeating browser-style templates. WeasyPrint uses running elements in page margin boxes.

### Display images in headers and footers

Use `{{inlinedImage "/absolute/path/logo.png"}}` in a view. It returns a complete image element with a data URI, not just the URI. The Go function `pdf.InlinedImage(ctx, pathOrURL, client)` accepts an optional HTTP client. Only pass trusted paths and URLs. Images are limited to 16 MiB.

Page orientation
--------------------------------------------------------------------------------------------------------

Call `Landscape()`, `Portrait()` or `Orientation(pdf.Landscape)`.

Paper format
--------------------------------------------------------------------------------------------

```
b.Format(pdf.A4)
```

Available formats are Letter, Legal, Tabloid, Ledger and A0 through A6. String values such as `pdf.Format("a4")` also work. Unknown formats return `ErrInvalid` at render time.

Paper size
--------------------------------------------------------------------------------------

```
b.PaperSize(210, 297, pdf.Millimeter)
```

A custom size takes precedence over a named format. Units are `Millimeter`, `Centimeter`, `Inch`, `Pixel` and `Point`.

Page margins
--------------------------------------------------------------------------------------------

```
b.Margins(10, 12, 14, 16, pdf.Millimeter)
```

Margins are top, right, bottom and left. Negative margins and non-finite sizes return `ErrInvalid`.

Scale
-----------------------------------------------------------------------

`b.Scale(0.8)` scales browser output. Values must be between 0.1 and 2. The zero value leaves the engine default unchanged. WeasyPrint and DOMPDF reject explicit scale options.

Page ranges
-----------------------------------------------------------------------------------------

`b.PageRanges("1-3, 5")` selects browser pages. WeasyPrint and DOMPDF reject page ranges instead of silently ignoring them.

Tagged PDF
--------------------------------------------------------------------------------------

Call `Tagged()` for accessible browser output. WeasyPrint passes `--pdf-tags` to the installed binary. DOMPDF rejects this option. Tags alone do not make a document accessible. Its headings, reading order and alternative text still need checking.

Document outline
--------------------------------------------------------------------------------------------------------

Call `DocumentOutline()` to request a browser-generated outline. WeasyPrint uses its own heading-based bookmarks. DOMPDF rejects this option.

PDF metadata
--------------------------------------------------------------------------------------------

```
b.Meta(pdf.Metadata{
    Title: "Invoice 42",
    Author: "Spatie",
    Subject: "December invoice",
    Keywords: "invoice, December",
    Creator: "Billing application",
    CreationDate: "D:20261202120000Z",
})
```

Metadata is written after rendering and before encryption for every driver. Text supports Unicode. Empty fields leave existing engine metadata untouched. `CreationDate` uses PDF date syntax. `Meta` replaces the builder's previous metadata value.

Conditional formatting
--------------------------------------------------------------------------------------------------------------------------

```
b.When(wideTable, func(b *pdf.Builder) { b.Landscape() })
```

Use `Apply` for a reusable function that configures a builder. It replaces dynamic PHP macros with a typed Go function.

Debugging
-----------------------------------------------------------------------------------

`request, err := b.RenderRequest(ctx)` returns rendered body, header, footer and options without launching a renderer. Do not log it when it contains personal data. Use `errors.Is(err, pdf.ErrInvalid)` and `errors.Is(err, pdf.ErrUnsupported)` for input and capability failures.

Background color
--------------------------------------------------------------------------------------------------------

Browser drivers print backgrounds by default. Add a background with CSS in your template. A full-page background may need zero margins and an inner content wrapper for spacing.

 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)
