# URL to PDF API

> Save a web page or your own HTML as a PDF through an API, with your paper size and page numbers. 1 credit per capture.

Send the address of a web page or your own HTML and get a PDF back, in the print layout of the page or as it looks on screen.

```bash
# API key: switchfile.io/dashboard/keys. The answer is the capture as JSON;
# result.file.url is the download link, valid for 24 hours.
curl "https://api.switchfile.io/v1/captures?wait=30" \
  -H "Authorization: Bearer $SWITCHFILE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url":"https://example.com","output_format":"pdf","options":{"page_format":"a4"}}'
```

```js
// npm i switchfile
import Switchfile from "switchfile";

const switchfile = new Switchfile({ apiKey: process.env.SWITCHFILE_API_KEY });
const capture = await switchfile.captures.create(
  { url: "https://example.com", output_format: "pdf", options: { page_format: "a4" } },
  { wait: true },
);
await switchfile.download(capture.result.file, "example.pdf");
```

```python
# pip install switchfile-sdk
import os
import switchfile
switchfile.configure(api_key=os.environ["SWITCHFILE_API_KEY"])
capture = switchfile.Capture.create(
    {"url": "https://example.com", "output_format": "pdf", "options": {"page_format": "a4"}}, wait=True)
switchfile.download(capture["result"]["file"], "example.pdf")
```

```php
<?php // The answer is the capture as JSON with result.file.url.
$ch = curl_init("https://api.switchfile.io/v1/captures?wait=30");
curl_setopt_array($ch, [
  CURLOPT_HTTPHEADER => ["Authorization: Bearer " . getenv("SWITCHFILE_API_KEY"), "Content-Type: application/json"],
  CURLOPT_POSTFIELDS => json_encode(["url" => "https://example.com", "output_format" => "pdf", "options" => ["page_format" => "a4"]]),
  CURLOPT_RETURNTRANSFER => true,
]);
$capture = json_decode(curl_exec($ch), true);
echo $capture["result"]["file"]["url"] ?? $capture["status"], PHP_EOL;
```

[Get a free API key](https://switchfile.io/signup) (Each capture costs 1 credit.) [Try it in the browser](https://switchfile.io/tools/save-website-as-pdf).

## What you get

- **Paper and margins**: page_format sets the paper size, page_orientation turns it, and margins are set in mm per side.
- **Print or screen layout**: By default the page uses its print layout. css_media_type set to screen keeps the look of the browser.
- **Your own header and footer**: header_template and footer_template print HTML on every page. An element with the class pageNumber gets the page number.
- **Your own HTML**: Send html instead of url, for example an invoice, and JavaScript runs in it. Images and fonts need absolute https addresses.

## Options

| Option | Meaning |
|---|---|
| `page_format` | Paper size, for example a4 or letter. |
| `page_orientation` | portrait (default) or landscape. |
| `margin_top` | Top margin in mm. Each other side has its own option, for example margin_left. |
| `css_media_type` | print (default) or screen. |
| `footer_template` | Footer as HTML on every page. header_template does the same at the top. |
| `display_header_footer` | Add a header with the date and title and a footer with page numbers. |

Without the wait parameter, the request answers at once with 202 and the capture keeps running. Fetch links.self later to get the result.

## Where your files go

- **Captured on our server**: Captures run on our server in the EU. Stored files and results are deleted after 24 hours. [Hosting in the privacy policy](https://switchfile.io/privacy#hosting)
- **Data processing agreement**: We sign a data processing agreement (DPA) under Art. 28 GDPR on request. Write to hello@switchfile.io. [Security](https://switchfile.io/security#dpa)

## Pricing

Free: 20 files/day, free API key
Credit pack: from €7, 500 credits, one-time, no subscription
Monthly: from €4, 500 credits a month, cancel at any time
Each capture costs 1 credit, however long the page takes to load.
A capture uses 1 credit, which costs €0.0043 to €0.014 depending on the plan or pack.
[Pricing](https://switchfile.io/pricing)

## SDKs

Client libraries and integrations: [SDK overview](https://switchfile.io/developers#sdks).

## Questions

### What are the limits?

A page has 30 seconds to load and may load up to 50 MB. A full-page image is cut at 20000 pixels in height (webp: 16383), and the notes of the result say so. Each API key can start 30 captures per minute with credits, and 5 without credits or with a sandbox key. A PDF can have up to 200 pages, and HTML sent in html up to 1 MB.

### Can it capture pages behind a login?

Yes. Send the session as cookies or a token in headers. Both are stored encrypted and appear as [redacted] in every response and webhook. They are deleted when the job finishes, or after 24 hours if it fails.

### Does JavaScript run?

Yes. The page is loaded like a normal browser visit from our servers, with JavaScript on. This also applies to HTML you send in html.

### Can every website be captured?

No. The address must be reachable from the internet, and private networks are blocked. Websites that block automated visitors cannot be captured. For a check that finishes on its own we wait a few seconds; we never solve captchas.

### How long do you keep my files?

The result is kept for 24 hours, then it is deleted. You can delete it earlier through the API.
