Creating PDFs | browsershot-go | Spatie       

 SPATIE  

browsershot-go
==============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Browsershot-go](https://spatie.be/docs/browsershot-go/v1)  Usage  Creating PDFs

 Version   v1      

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

  Creating PDFs    
- [ Introduction ](https://spatie.be/docs/browsershot-go/v1/introduction)
- [ Support us ](https://spatie.be/docs/browsershot-go/v1/support-us)
- [ Requirements ](https://spatie.be/docs/browsershot-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/browsershot-go/v1/installation-setup)
- [ Upgrading ](https://spatie.be/docs/browsershot-go/v1/upgrading)
- [ Questions and issues ](https://spatie.be/docs/browsershot-go/v1/questions-issues)
- [ Testing ](https://spatie.be/docs/browsershot-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/browsershot-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/browsershot-go/v1/changelog)
- [ About us ](https://spatie.be/docs/browsershot-go/v1/about-us)

Usage
-----

- [ Introduction ](https://spatie.be/docs/browsershot-go/v1/usage/introduction)
- [ Creating images ](https://spatie.be/docs/browsershot-go/v1/usage/creating-images)
- [ Creating PDFs ](https://spatie.be/docs/browsershot-go/v1/usage/creating-pdfs)
- [ Creating HTML ](https://spatie.be/docs/browsershot-go/v1/usage/creating-html)

Miscellaneous options
---------------------

- [ Adding extra headers to every request ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/adding-extra-headers-to-every-request)
- [ Adding extra headers to the navigational request ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/adding-extra-headers-to-the-navigational-request)
- [ Changing the language of the browser ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/changing-the-language-of-the-browser)
- [ Changing the value of a dropdown ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/changing-the-value-of-a-dropdown)
- [ Clicking on the page ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/clicking-on-the-page)
- [ Connection to a remote chromium/chrome instance ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/connection-to-a-remote-chrome-instance)
- [ Disable sandboxing ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/disable-sandboxing)
- [ Fixing CORS issues ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/fixing-cors-options)
- [ Getting console output ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/getting-console-ouput)
- [ Getting failed requests ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/getting-failed-requests)
- [ Ignore HTTPS errors ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/ignore-https-errors)
- [ Passing environment variables to the browser ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/passing-environment-variables-to-the-browser)
- [ Performance ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/performance)
- [ Prevent unsuccessful responses ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/prevent-unsuccessful-responses)
- [ Sending POST requests ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/sending-post-requests)
- [ Setting an arbitrary option ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/setting-an-arbirary-option)
- [ Setting the CSS media type of the page ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/setting-the-css-media-type-of-the-page)
- [ Setting the timeout ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/setting-the-timeout)
- [ Setting the user agent ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/setting-the-user-agent)
- [ Specifying a proxy server ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/specifying-a-proxy-server)
- [ Typing on the page ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/typing-on-the-page)
- [ Using a pipe instead of a WebSocket ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/using-a-pipe-instead-of-a-websocket)
- [ Using cookies ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/using-cookies)
- [ Using HTTP authentication ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/using-http-authentication)
- [ Using URL for HTML content ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/using-url-for-html-content)
- [ Writing options to a file ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/writing-options-to-a-file)
- [ Disabling redirects ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/disabling-redirects)
- [ Read redirect history ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/read-redirect-history)
- [ Evaluating a script before page load ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/evaluating-a-script-before-page-load)

 Creating PDFs
=============

###  On this page 

1. [ A note on security ](#content-a-note-on-security)
2. [ Sizing the PDF ](#content-sizing-the-pdf)
3. [ Using a predefined format ](#content-using-a-predefined-format)
4. [ Setting margins ](#content-setting-margins)
5. [ Headers and footers ](#content-headers-and-footers)
6. [ Backgrounds ](#content-backgrounds)
7. [ Tagged (accessible) PDF ](#content-tagged-accessible-pdf)
8. [ Landscape orientation ](#content-landscape-orientation)
9. [ Scale ](#content-scale)
10. [ Only export specific pages ](#content-only-export-specific-pages)
11. [ Set the initial page number ](#content-set-the-initial-page-number)
12. [ Output directly to the browser ](#content-output-directly-to-the-browser)

Browsershot saves a PDF if the path passed to `Save` has a `pdf` extension:

```
err := browsershot.URL("https://example.com").Save(ctx, "example.pdf")
```

Alternatively, use `SavePDF` explicitly, or pass HTML:

```
err := browsershot.HTML("<h1>Hello world</h1>").SavePDF(ctx, "example.pdf")
```

`PDF(ctx)` returns PDF bytes and `Base64PDF(ctx)` returns their base64 representation.

A note on security
--------------------------------------------------------------------------------------------------------------

Only pass URLs and HTML you trust. Local file requests from `HTML` are blocked by default. If trusted HTML needs local assets, explicitly call `AllowLocalFileAccess()`. See [local file access](/docs/browsershot-go/v1/usage/introduction#local-file-access).

Sizing the PDF
--------------------------------------------------------------------------------------------------

Use `PaperSize(width, height)` with millimeters, or pass a custom unit as the third argument, such as `PaperSize(8.5, 11, "in")`.

Using a predefined format
-----------------------------------------------------------------------------------------------------------------------------------

```
page := browsershot.URL("https://example.com").Format("A4")
```

Chrome supports Letter, Legal, Tabloid, Ledger and A0 through A6. A4 is 210 × 297 mm. Letter is 8.5 × 11 inches.

Setting margins
-----------------------------------------------------------------------------------------------------

`Margins(top, right, bottom, left)` uses millimeters. Pass a fifth unit argument to change units.

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

Chrome's header and footer are hidden by default. Enable them and provide HTML templates:

```
page := browsershot.HTML("<h1>Report</h1>").ShowBrowserHeaderAndFooter().
    HeaderHTML(`<span class="title"></span>`).
    FooterHTML(`<span class="pageNumber"></span> / <span class="totalPages"></span>`).
    Margins(15, 10, 15, 10)
```

Template classes `date`, `title`, `url`, `pageNumber` and `totalPages` receive printing values from Chrome. Use `HideHeader` or `HideFooter` to hide one template. Reserve enough space with margins.

Backgrounds
-----------------------------------------------------------------------------------------

PDFs omit CSS backgrounds by default. `ShowBackground()` includes them. `TransparentBackground()` requests a transparent page background instead of white.

Tagged (accessible) PDF
-------------------------------------------------------------------------------------------------------------------------

Call `TaggedPDF()` to request a tagged PDF.

Landscape orientation
-----------------------------------------------------------------------------------------------------------------------

Call `Landscape()` for landscape, or `Landscape(false)` for portrait.

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

`Scale(0.5)` changes the print scale. Chrome accepts values between 0.1 and 2.

Only export specific pages
--------------------------------------------------------------------------------------------------------------------------------------

`Pages("1-5, 8, 11-13")` sets the print range.

Set the initial page number
-----------------------------------------------------------------------------------------------------------------------------------------

`InitialPageNumber(8)` inserts blank pages and excludes them from the output so Chrome's page-number template starts at eight.

Output directly to the browser
--------------------------------------------------------------------------------------------------------------------------------------------------

```
pdf, err := browsershot.HTML("<h1>Report</h1>").PDF(ctx)
```

Check the error, set the HTTP `Content-Type` to `application/pdf`, then write the bytes. Use `WaitUntilNetworkIdle`, `WaitForFunction`, `WaitForSelector` or `Delay` for pages that need additional rendering time. Output methods take a context so requests and jobs can cancel browser work.

 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)
