> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chromiumly.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Stages

> Post-process PDFs with an ordered list of typed stages: merge, split, stamp, encrypt, sign, PDF/A and PDF/UA, and more.

A stage is one operation on a PDF: merge, stamp, sign, make PDF/A, and so on. Gotenberg runs the stages in the order you list them, each on what the previous one produced. Pass them as `stages` to:

* the Chromium [converters](https://docs.chromiumly.dev/chromium/overview.md)' `convert()`, where they run on the printed PDF;
* [`LibreOffice.convert()`](https://docs.chromiumly.dev/libreoffice), where they run on the exported PDFs;
* [`PDF.pipe()`](https://docs.chromiumly.dev/pdf/overview), where they run on the PDFs you send.

Screenshots don't take stages. Each stage also has its own method on the [`PDF`](https://docs.chromiumly.dev/pdf/overview) class, such as `PDF.merge()` or `PDF.sign()`.

```typescript theme={null}
import { HtmlConverter } from "chromiumly";

const buffer = await new HtmlConverter().convert({
  html: "path/to/report.html",
  stages: [
    { op: "select", pages: "1-3" },
    { op: "stamp-text", text: "DRAFT", x: 72, y: 72, fontSize: 24 },
    { op: "set-metadata", title: "Quarterly report" },
    { op: "remediate", profile: "PDF/A-2b" },
  ],
});
```

Every stage is a typed object discriminated by its `op`, with the option names [Gotenberg documents](https://gotenberg.dev/docs/pdf/pipe). TypeScript checks each stage's options.

## Every stage

| Stage | Does | Page |
| - | - | - |
| `merge` | Appends the PDFs into one, in the alphanumeric order of their names | [Pages](https://docs.chromiumly.dev/pdf/pages.md#merge) |
| `split` | Cuts each PDF `every` n pages, or into `ranges`; the parts come back in a ZIP | [Pages](https://docs.chromiumly.dev/pdf/pages.md#split) |
| `select` | Keeps the `pages` listed, in the order listed | [Pages](https://docs.chromiumly.dev/pdf/pages.md#select-and-delete) |
| `delete` | Removes the `pages` listed | [Pages](https://docs.chromiumly.dev/pdf/pages.md#select-and-delete) |
| `insert` | Inserts the pages of another PDF (`source`) after page `at` | [Pages](https://docs.chromiumly.dev/pdf/pages.md#insert) |
| `compose` | Draws pages on new pages of a chosen size: labels, packing slips, n-up sheets | [Pages](https://docs.chromiumly.dev/pdf/pages.md#compose) |
| `rotate` | Turns `pages` by `angle`, a multiple of 90 | [Pages](https://docs.chromiumly.dev/pdf/pages.md#rotate) |
| `stamp-text` | Draws a line of text over pages | [Stamps](https://docs.chromiumly.dev/pdf/stamps.md) |
| `stamp-image` | Draws a PNG or JPEG `image` over pages | [Stamps](https://docs.chromiumly.dev/pdf/stamps.md) |
| `stamp-pdf` | Draws a page of another PDF (`source`) over pages: a letterhead, a seal | [Stamps](https://docs.chromiumly.dev/pdf/stamps.md) |
| `set-metadata` | Writes the document information | [Metadata](https://docs.chromiumly.dev/pdf/metadata.md) |
| `set-bookmarks` | Replaces the bookmarks | [Metadata](https://docs.chromiumly.dev/pdf/metadata.md#bookmarks) |
| `set-form` | Fills form fields | [Forms](https://docs.chromiumly.dev/pdf/forms.md) |
| `flatten-form` | Burns the form fields into the pages | [Forms](https://docs.chromiumly.dev/pdf/forms.md#flatten) |
| `flatten-transparency` | Replaces transparent content with opaque content | [Conformance](https://docs.chromiumly.dev/pdf/conformance.md#prepare-for-print) |
| `convert-colors` | Converts every color to `cmyk`, `rgb`, `gray` or `output-intent` | [Conformance](https://docs.chromiumly.dev/pdf/conformance.md#prepare-for-print) |
| `redact` | Removes the content under each region from the file itself | [Security](https://docs.chromiumly.dev/pdf/security.md#redact) |
| `autotag` | Infers a structure tree for an untagged PDF, for screen readers and PDF/UA | [Conformance](https://docs.chromiumly.dev/pdf/conformance.md#autotag) |
| `attach` | Embeds a `file` in the PDF | [Attachments](https://docs.chromiumly.dev/pdf/attachments.md) |
| `detach` | Removes an embedded file by `name` | [Attachments](https://docs.chromiumly.dev/pdf/attachments.md) |
| `extract-attachments` | Returns the embedded files next to the PDF | [Attachments](https://docs.chromiumly.dev/pdf/attachments.md#extract) |
| `extract-text` | Returns the text the pages show, as a `.txt` file next to the PDF | [Render and extract](https://docs.chromiumly.dev/pdf/render-extract.md#extract-text) |
| `optimize` | Shrinks the PDF without lowering image quality | [Render and extract](https://docs.chromiumly.dev/pdf/render-extract.md#optimize) |
| `encrypt` | Protects the PDF with passwords and `permissions` | [Security](https://docs.chromiumly.dev/pdf/security.md#encrypt) |
| `decrypt` | Opens encrypted PDFs and removes their encryption | [Security](https://docs.chromiumly.dev/pdf/security.md#decrypt) |
| `remediate` | Repairs the PDF until it conforms to a PDF/A, PDF/UA or `Factur-X` profile | [Conformance](https://docs.chromiumly.dev/pdf/conformance.md) |
| `sign` | Signs the PDF with an uploaded `certificate` | [Security](https://docs.chromiumly.dev/pdf/security.md#sign) |
| `timestamp` | Adds an RFC 3161 document timestamp | [Security](https://docs.chromiumly.dev/pdf/security.md#timestamp) |
| `render` | Draws pages as PNG or JPEG images, next to the PDF | [Render and extract](https://docs.chromiumly.dev/pdf/render-extract.md#render) |

When several files come out, the answer is a ZIP: a `split` into several parts, or `render` and the extracts, which return their files next to the PDF.

## Files a stage needs

An option that names a file takes a `StageFile`: `insert.source`, `stamp-image.image`, `stamp-pdf.source`, `attach.file`, `remediate.xml`, `sign.certificate` and `sign.key`. Chromiumly uploads it with the request and puts its name in the stage.

```typescript theme={null}
type StageFile =
  | string // a path, uploaded under its base name
  | URL // a file: URL, uploaded under its base name
  | { file: PathLikeOrReadStream; name: string } // a path, Buffer or ReadStream, uploaded under `name`
  | { name: string }; // a file the request already carries: through downloadFrom, archives, or another upload
```

```typescript theme={null}
const buffer = await new HtmlConverter().convert({
  html: "path/to/invoice.html",
  assets: [{ file: "path/to/logo.png", name: "logo.png" }],
  stages: [
    // Reuse the asset the HTML already uploads
    { op: "stamp-image", image: { name: "logo.png" }, x: 40, y: 760, width: 80 },
    // Upload a Buffer under a name of your choice
    {
      op: "attach",
      file: { file: invoiceXmlBuffer, name: "invoice.xml" },
      relationship: "Data",
    },
  ],
});
```

Two uploads under one name, such as an asset and a stage file, throw an error before the request is sent: reference the existing upload with `{ name }`, or give one of them another name.

`compose` is the exception: its `placements[].source` names one of the input PDFs, such as `"report.pdf"`, rather than a file to upload.

## Page selections

An option that takes pages, such as `select.pages`, accepts a comma-separated list of parts:

| Part | Pages |
| - | - |
| `3` | Page 3 |
| `2-5` | Pages 2 to 5 |
| `3-` | Page 3 to the last |
| `-3` | The first page to page 3 |
| `odd` | Odd pages |
| `even` | Even pages |
| `last` | The last page |
| `!1` | Every page but the first (an exclusion) |
| `all` | Every page, on its own |

`odd,!1` keeps the odd pages after the first, and `!last` every page but the last. Without an inclusion, the selection starts from every page. Pages count from 1. A page past the end, or a selection that leaves no page, is refused with a 400.

## Order rules

Gotenberg checks the order of the stages before it runs any, and refuses a list that breaks one of these rules with a 400 that names the rule:

* `decrypt` comes first, and `render`, `extract-attachments` or `extract-text` last.
* A list has at most one `remediate`, one `encrypt`, one `sign` and one `timestamp`. One `remediate` takes a PDF/A profile and a PDF/UA one together.
* After `remediate`, only `encrypt`, `optimize`, `sign`, `timestamp`, `render`, `extract-attachments` and `extract-text` may follow.
* After `encrypt`, only `optimize`, `render`, `extract-attachments` and `extract-text` may follow.
* After `sign` or `timestamp`, only `sign`, `timestamp`, `render`, `extract-attachments` and `extract-text` may follow.
* `encrypt` doesn't combine with `sign`, `timestamp`, or a PDF/A or Factur-X `remediate`.
* `decrypt` doesn't combine with `sign` or `timestamp`: sign the decrypted PDF in a request of its own.

Some stages rewrite the PDF whole, which drops signatures, so they refuse a signed PDF: `select`, `delete`, `insert`, `merge`, `split`, `compose`, `encrypt`, `decrypt`, `flatten-form`, `redact`, `remediate`, `convert-colors`, `flatten-transparency` and `optimize`. The others append an update and keep signatures.

See [Reference — stages](https://docs.chromiumly.dev/reference/stages.md) for every stage's options.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.