Creating PDFs | browsershot | Spatie

 SPATIE

  Browsershot
==============

spatie.be/open-source

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

 Version   v4   v3

 Other versions for crawler [v4](https://spatie.be/docs/browsershot/v4) [v3](https://spatie.be/docs/browsershot/v3)

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

Usage
-----

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

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

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

 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 will save a pdf if the path passed to the `save` method has a `pdf` extension.

```
// a pdf will be saved
Browsershot::url('https://example.com')->save('example.pdf');
```

Alternatively you can explicitly use the `savePdf` method:

```
Browsershot::url('https://example.com')->savePdf('example.pdf');
```

You can also pass some html which will be converted to a pdf.

```
Browsershot::html($someHtml)->savePdf('example.pdf');
```

If you need the base64 version of a PDF you can use the `base64pdf` method. This can come in handy when you don't want to save the screenshot on disk in environments like Heroku that don't allow you to save a file. You can then proceed to create the file and upload it directly as a base64 string using a package like [Laravel Media Library](https://spatie.be/docs/laravel-medialibrary/v9/api/adding-files#addmediafrombase64).

```
$base64Data = Browsershot::url('https://example.com')
    ->base64pdf();
```

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

It is the responsibility of the user to validate the URLs and HTML passed to browsershot. Only pass URLs and HTML that you trust.

Sizing the pdf
--------------------------------------------------------------------------------------------------

You can specify the width and the height.

```
Browsershot::html($someHtml)
   ->paperSize($width, $height)
   ->save('example.pdf');
```

Optionally you can give a custom unit to the `paperSize` as the third parameter.

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

You can use the `format` method and provide a format size:

```
Browsershot::url('https://example.com')->format('A4')->save('example.pdf');
```

The `format` options available by puppeteer are:

```
Letter: 8.5in  x  11in
Legal: 8.5in  x  14in
Tabloid: 11in  x  17in
Ledger: 17in  x  11in
A0: 33.1in  x  46.8in
A1: 23.4in  x  33.1in
A2: 16.54in  x  23.4in
A3: 11.7in  x  16.54in
A4: 8.27in  x  11.7in
A5: 5.83in  x  8.27in
A6: 4.13in  x  5.83in
```

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

Margins can be set.

```
Browsershot::html($someHtml)
   ->margins($top, $right, $bottom, $left)
   ->save('example.pdf');
```

Optionally you can give a custom unit to the `margins` as the fifth parameter.

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

By default a PDF will not show the header and a footer generated by Chrome. Here's how you can make the header and footer appear. You can also provide a custom HTML template for the header and footer.

```
Browsershot::html($someHtml)
   ->showBrowserHeaderAndFooter()
   ->headerHtml($someHtml)
   ->footerHtml($someHtml)
   ->save('example.pdf');
```

In the header and footer HTML, any tags with the following classes will have its printing value injected into its contents.

- `date` formatted print date
- `title` document title
- `url` document location
- `pageNumber` current page number
- `totalPages` total pages in the document

To hide the header or footer, you can call either `hideHeader` or `hideFooter`.

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

By default, the resulting PDF will not show the background of the html page. If you do want the background to be included you can call `showBackground`:

```
Browsershot::html($someHtml)
   ->showBackground()
   ->save('example.pdf');
```

If you want a transparent background color instead of the default white, you can call `transparentBackground`:

```
Browsershot::html($someHtml)
   ->transparentBackground()
   ->save('example.pdf');
```

Tagged (accessible) pdf
-------------------------------------------------------------------------------------------------------------------------

Call `taggedPdf` if you want the resulting pdf to be tagged (accessible).

```
Browsershot::html($someHtml)
    ->taggedPdf()
    ->save('example.pdf');
```

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

Call `landscape` if you want to resulting pdf to be landscape oriented.

```
Browsershot::html($someHtml)
   ->landscape()
   ->save('example.pdf');
```

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

Scale can be set. Defaults to 1. Scale amount must be between 0.1 and 2.

```
Browsershot::html($someHtml)
    ->scale(0.5)
    ->save('example.pdf');
```

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

You can control which pages should be export by passing a print range to the `pages` method. Here are some examples of valid print ranges: `1`, `1-3`, `1-5, 8, 11-13`.

```
Browsershot::html($someHtml)
   ->pages('1-5, 8, 11-13')
   ->save('example.pdf');
```

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

You can set the initial page number with `initialPageNumber`. Here's an example that shows how to set the first page to number 8.

```
Browsershot::html($someHtml)
   ->showBrowserHeaderAndFooter()
   ->hideFooter()
   ->headerHtml('')
   ->initialPageNumber(8)
   ->save('example.pdf');
```

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

You can output the PDF directly to the browser using the `pdf()` method.

```
$pdf = Browsershot::url('https://example.com')->pdf()
```
