Creating PDFs | browsershot-rust | Spatie       

 SPATIE  

browsershot-rust
================

spatie.be/open-source

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

 Version   v1      

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

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

Usage
-----

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

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

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

```
Browsershot::url("https://example.com").save_pdf("example.pdf")?;
```

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

```
Browsershot::html(some_html).save_pdf("example.pdf")?;
```

If you need the base64 version of a PDF you can use the `base64_pdf` method. This can come in handy when you don't want to save the PDF on disk.

```
let base64_data = Browsershot::url("https://example.com")
    .base64_pdf()?;
```

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.

Local file requests from `html()` are blocked by default. If your trusted HTML needs local assets, explicitly opt in with `allow_local_file_access(true)`. See [local file access](/docs/browsershot-rust/v1/usage/introduction#local-file-access) for details and security considerations.

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

You can specify the width and the height.

```
Browsershot::html(some_html)
   .paper_size(width, height)
   .save("example.pdf")?;
```

Optionally you can give a custom unit to `paper_size_with_unit` as the third parameter, using `Unit::Mm`, `Unit::Cm`, `Unit::In` or `Unit::Px`.

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 available `format` options 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(some_html)
   .margins(top, right, bottom, left)
   .save("example.pdf")?;
```

Optionally you can give a custom unit to `margins_with_unit` 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(some_html)
   .show_browser_header_and_footer()
   .header_html(some_html)
   .footer_html(some_html)
   .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 `hide_header` or `hide_footer`.

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 `show_background`:

```
Browsershot::html(some_html)
   .show_background()
   .save("example.pdf")?;
```

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

```
Browsershot::html(some_html)
   .transparent_background()
   .save("example.pdf")?;
```

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

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

```
Browsershot::html(some_html)
    .tagged_pdf()
    .save("example.pdf")?;
```

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

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

```
Browsershot::html(some_html)
   .landscape(true)
   .save("example.pdf")?;
```

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

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

```
Browsershot::html(some_html)
    .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(some_html)
   .pages("1-5, 8, 11-13")
   .save("example.pdf")?;
```

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

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

```
Browsershot::html(some_html)
   .show_browser_header_and_footer()
   .hide_footer()
   .header_html("")
   .initial_page_number(8)
   .save("example.pdf")?;
```

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

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

```
let pdf = Browsershot::url("https://example.com").pdf()?;
```

 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)
