# Convert a file

Turn a file into another format, such as a Word document into a PDF, a deck into pictures or a workbook into CSV. See what the file can become, then convert it.

`cv login` saves a key, or export one as `CV_API_KEY`. Every `cv` command reads it.

```sh
export CV_API_KEY=cv_sk_…
```

## See what it can become

`cv targets` lists the formats a file can become, each with what the conversion keeps and drops. It reads the format from the file's content on your machine and uploads nothing.

```sh
cv targets report.pdf
```

```text
pdf ->
  docx     The text is rebuilt as an editable document with headings, paragraphs and lists, without the page layout. …
  pdf      Only the changes you ask for are made. …
  pdf      join: The PDFs joined in the order given. …
  md       Headings, paragraphs, lists, links and tables whose cells line up in rows and columns are recovered from the text layout. …
  …
```

In the API:

`/v1/targets` needs no key.

```sh
curl "https://api.convert.dev/v1/targets?from=pdf"
```

Only these conversions exist. Any other target fails with `unsupported_conversion`, and its `try:` line lists the ones that do.

## Convert it

Name the output, and its extension picks the format. Or pass `--to` and leave the output out, and the result goes to stdout. An output file that already exists is replaced only with `--force`.

```sh
cv convert report.docx report.pdf
```

stderr:

```text
48213 bytes -> report.pdf
```

In the API:

```sh
curl -X POST https://api.convert.dev/v1/jobs \
  -H "Authorization: Bearer $CV_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Prefer: wait=30" \
  -d '{"inputs": ["file_01JC9W…"], "to": "pdf"}'
```

Response:

```json
{
  "object": "job",
  "status": "done",
  "outputs": [{"object": "file", "name": "report.pdf", "url": "https://…"}]
}
```

In the API, upload the file, start a job with the format in `to`, and download each output from its `url`. The link needs no key and works for an hour. See [Files](https://convert.dev/docs/api/files.md) and [Jobs](https://convert.dev/docs/api/jobs.md).

A file already in the format you ask for comes back as it was, at no cost. That output is your own upload, so it has no `url`: keep the file you sent. See [Outputs without a link](https://convert.dev/docs/api/jobs.md#unchanged).

A format goes by its id or an alias, so `jpg` is `jpeg` and `markdown` is `md`. See [Formats](https://convert.dev/docs/formats.md).

## Many files from one

A conversion that makes one file for each page, slide or sheet, such as a PDF to PNG, needs a directory as the output. `--pages` picks the pages, such as `1-3,7`.

```sh
cv convert slides.pdf pages/ --to png --pages 1-3
```

stderr:

```text
3 files -> pages/
```

In the API:

```sh
curl -X POST https://api.convert.dev/v1/jobs \
  -H "Authorization: Bearer $CV_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Prefer: wait=30" \
  -d '{"inputs": ["file_01JC9W…"], "to": "png", "options": {"pages": "1-3"}}'
```

Response:

```json
{
  "object": "job",
  "status": "done",
  "outputs": [
    {"object": "file", "index": 0, "url": "https://…"},
    {"object": "file", "index": 1, "url": "https://…"},
    {"object": "file", "index": 2, "url": "https://…"}
  ],
  "output_count": 3,
  "archive": {"size": 412870, "url": "https://…"}
}
```

An output file or stdout asks for exactly one file, so `cv` fails and its `try:` line shows the directory form. In the API, each output has its `index`, and `archive` holds them all in one zip. A job lists its first 100 outputs; see [Several outputs](https://convert.dev/docs/api/jobs.md#outputs) for the rest.

## Set options

`--opt key=value` sets an option of the conversion, such as the quality of a picture. Repeat it for several. In the API, options go in the job's `options`.

```sh
cv convert photo.png web.webp --opt quality=80 --opt strip=true
```

stderr:

```text
48120 bytes -> web.webp
```

In the API:

```sh
curl -X POST https://api.convert.dev/v1/jobs \
  -H "Authorization: Bearer $CV_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Prefer: wait=30" \
  -d '{"inputs": ["file_01JC9W…"], "to": "webp", "options": {"quality": 80, "strip": true}}'
```

Each conversion takes only some options. `cv targets <file> --json` lists them with their ranges and defaults, and one the conversion does not take fails with `invalid_options`. See [Options](https://convert.dev/docs/options.md).

## Several files

Several inputs with a directory as the output are converted one by one. With one output file they are joined into one, in the order given.

```sh
cv convert a.pdf b.pdf merged.pdf
```

stderr:

```text
412870 bytes -> merged.pdf
```

In the API:

```sh
curl -X POST https://api.convert.dev/v1/jobs \
  -H "Authorization: Bearer $CV_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Prefer: wait=30" \
  -d '{"inputs": ["file_01JC9W…", "file_01JC9X…"], "to": "pdf", "output": {"shape": "one"}}'
```

In the API, `output.shape` set to `one` joins the inputs. Files converted one by one cost a conversion each, and PDFs joined into a PDF cost one. See [Join several files](https://convert.dev/docs/write.md#join) and [What a job costs](https://convert.dev/docs/limits.md#cost).

## Text from a scan

A scan holds pictures of text. A conversion that takes `ocr` reads the text with `--opt ocr=on`, such as a scanned PDF to an editable Word document. Add `language` when the text is not English. Without either, `ocr` is `auto`, which reads only the text layer the file already has.

```sh
cv convert scan.pdf scan.docx --opt ocr=on --opt language=en,ko
```

In the API:

```sh
curl -X POST https://api.convert.dev/v1/jobs \
  -H "Authorization: Bearer $CV_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Prefer: wait=30" \
  -d '{"inputs": ["file_01JC9W…"], "to": "docx", "options": {"ocr": "on", "language": "en,ko"}}'
```

`cv targets scan.pdf --json` shows which conversions take `ocr`. To read a scan as text, see [Scanned pages](https://convert.dev/docs/read.md#scans).

## Large files and long jobs

`cv` waits for the job up to `--wait` seconds, 300 by default. When the wait runs out the job keeps running, and the same command again waits for it instead of starting and paying for a new one.

In the API, a file over 32 MiB goes up in parts, and a job that takes longer than the wait answers `202`. See [Upload a large file](https://convert.dev/docs/api/files.md#large) and [Waiting and retries](https://convert.dev/docs/api/waiting.md).

A file locked with a password fails with `input_encrypted`. See [Files with a password](https://convert.dev/docs/read.md#password).
