# PDF and image tasks

Merge, split, rotate and protect PDFs, create thumbnails and make files smaller with tasks in a job. Same task names and fields as the CloudConvert API.

Besides `convert`, a job can work on PDFs and images directly. The task names and fields are the same as in the CloudConvert API, so an existing job definition keeps working. Each task takes its files from `input` and its results can go into the next task, for example `import/url` → `convert` → `thumbnail` → `export/url`.

| Operation | What it does | Credits |
| --- | --- | --- |
| `merge` | One PDF from the PDFs of all inputs, in the order of `input` | 1 per started minute |
| `pdf/split-pages` | One PDF per page | 1 per file and started minute |
| `pdf/extract-pages` | A PDF with the chosen pages | 1 per file and started minute |
| `pdf/rotate-pages` | Rotates pages | 1 per file and started minute |
| `pdf/encrypt` | Adds a password and permissions | 1 per file and started minute |
| `pdf/decrypt` | Removes the password and permissions | 1 per file and started minute |
| `thumbnail` | A preview image of a PDF, a document or an image | Like `convert` to the same format |
| `optimize` | Makes PDF, JPG, PNG and WebP files smaller | 1 per file and started minute |

Every task takes `filename` for the name of the result when there is one result, `timeout` in seconds and `ignore_error`. Fields a task does not know are ignored and named in `notes` of the result, like with `convert`. A [sandbox key](https://switchfile.io/docs/sandbox) runs all of them without credits.

## Cost before running

[POST /v2/jobs/estimate](https://switchfile.io/docs/reference/post-v2-jobs-estimate) takes the same body as `POST /v2/jobs` and returns the credits per task, the total and the price. Nothing is created or charged:

```bash
curl https://api.switchfile.io/v2/jobs/estimate \
  -H "Authorization: Bearer $SWITCHFILE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"tasks": {"in": {"operation": "import/url", "url": "https://example.com/report.pdf"}, "small": {"operation": "optimize", "input": "in"}}}'
```

```json
{
  "object": "estimate",
  "tasks": { "small": { "operation": "optimize", "credits": { "min": 1, "max": 6 } } },
  "credits": { "min": 1, "max": 6 },
  "price": { "currency": "EUR", "min": 0.01, "max": 0.06 },
  "free": true
}
```

`credits.max` is the most a task can cost: one credit per started minute of its time limit. After `pdf/split-pages` the number of files depends on the page count, so the tasks behind it have `max: null` and so does the total. `free` says whether the job still fits into today's free quota. It is `false` while the maximum is open, and always `true` with a sandbox key.

## Pages of a PDF

`pages` takes single pages and ranges: `1-3`, `1,3,5` or `8-` for page 8 to the end. A page that the PDF does not have fails the task with `INVALID_OPTIONS` and a message such as "Page 9 does not exist. The PDF has 3 pages."

```json
{
  "tasks": {
    "in": { "operation": "import/url", "url": "https://example.com/contract.pdf" },
    "first-pages": { "operation": "pdf/extract-pages", "input": "in", "pages": "1-2" },
    "turned": { "operation": "pdf/rotate-pages", "input": "first-pages", "rotation": "+90" },
    "out": { "operation": "export/url", "input": "turned" }
  }
}
```

- `merge` takes PDFs only. Convert other files to `pdf` with a `convert` task first and give both tasks as `input`.
- `pdf/split-pages` names the files after the input and the page, for example `contract-page-01.pdf`, and makes at most 1000 files from one PDF.
- `pdf/extract-pages` keeps the chosen pages in page order.
- `pdf/rotate-pages` takes `rotation` as `+90`, `+180` or `-90`, clockwise, on the pages in `pages` or on all pages.

The page tasks need an unprotected PDF. For a protected one, put a `pdf/decrypt` task in front.

## PDF passwords

`pdf/encrypt` protects a PDF with AES-256. `set_password` is needed to open the file. `set_owner_password` is needed to change the permissions. Without it, a random owner password is set and the restrictions can no longer be lifted. Both take at least 4 characters.

| Field | Values | Default |
| --- | --- | --- |
| `allow_print` | `full`, `low` (low resolution only), `none` | allowed |
| `allow_modify` | `all`, `annotate`, `form`, `assembly`, `none` | `all` |
| `allow_extract` | `true`, `false`: copying text and images | `true` |

`pdf/decrypt` removes the password and the permissions. A PDF that only has permissions needs no `password`; one that needs a password to open fails with `PASSWORD_PROTECTED` when the password is missing or wrong. An unprotected PDF passes through unchanged with a note.

Passwords are stored encrypted until the job ends. Responses and webhooks never contain them; they show as `[redacted]`.

## Thumbnails

`thumbnail` creates one image per input file. `output_format` sets its format: `png`, `jpg` or `webp`. It takes PDFs and Office documents, and images in every format that `convert` reads.

| Field | Default | Meaning |
| --- | --- | --- |
| `width`, `height` | size of the page or image | Size in pixels, up to 10000 |
| `fit` | `max` | `max` fits inside width and height. `crop` fills both and cuts from the center (`smartcrop` works the same), `scale` stretches |
| `page` | `1` | Page of a PDF or document |
| `auto_orient` | `true` | Turns photos upright by their camera orientation |
| `input_format` | from the file name | Format of the input when the name has no extension |

```json
{
  "tasks": {
    "in": { "operation": "import/url", "url": "https://example.com/slides.pptx" },
    "pdf": { "operation": "convert", "input": "in", "output_format": "pdf" },
    "thumb": { "operation": "thumbnail", "input": "pdf", "output_format": "webp", "width": 400, "height": 300, "fit": "crop" },
    "out": { "operation": "export/url", "input": "thumb" }
  }
}
```

## Optimize

`optimize` makes a file smaller and keeps its format. The result is never larger than the input: when nothing can be saved, the input comes back with a note.

- **PDF**: the file is rewritten without any loss; images inside keep their quality. With the profile `web` the PDF also opens page by page in a browser before it has fully loaded.
- **Images** (JPG, PNG, WebP): the image is compressed again and its metadata (camera data, location) is removed. Images up to 120 megapixels work.

| Field | Default | Meaning |
| --- | --- | --- |
| `profile` | `web` | `web` for a good balance; `print` and `archive` keep more image quality; `max` and `mrc` give the smallest images |
| `quality` | from `profile` | Images only: 1 to 100, overrides the profile |

## Next

- [Jobs and tasks](https://switchfile.io/docs/jobs-and-tasks): The CloudConvert-compatible switchfile API (v2). Jobs made of tasks, task status, results, retries and moving over from CloudConvert.
- [Limits and quotas](https://switchfile.io/docs/limits): Rate limits, file sizes, time limits, the free daily quota, quotas per visitor for service keys and how long files are kept.
