# PDF (totalum-sdk/pdf)

> Turn HTML or a web page into a PDF stored in the project's files: invoices, tickets, reports. Each call returns the stored file's key and url, plus what it cost in usage.credits.

`import … from 'totalum-sdk/pdf'`, or the same namespace on `totalum` from `'totalum-sdk'`. Install, configuration and errors: [Totalum SDK](https://www.totalum.app/docs/sdk).

Turn HTML or a web page into a PDF stored in the project's files: invoices, tickets, reports. Each call returns the
stored file's `key` and `url`, plus what it cost in `usage.credits`.

```ts
const pdf = await totalum.pdf.fromHtml({ html: '<h1>Invoice 12</h1>', name: 'invoice-12' });
```

| Method or type | What it does |
|---|---|
| [`totalumPdf`](#totalumpdf) | Creates the `pdf` namespace (the same object as `totalum.pdf`, scope `files:write`): each call renders a PDF and stores it as a new public file of the project. |
| [`pdf.fromHtml`](#pdffromhtml) | Renders HTML (with its CSS) to a PDF in a real browser and stores it as a project file: invoices, tickets, certificates, reports. |
| [`pdf.fromUrl`](#pdffromurl) | Prints a web page to a PDF and stores it as a project file — archive a page, or render one of your own pages (a printable report route) as a PDF. |
| [`PdfFromHtmlInput`](#pdffromhtmlinput) | The input of `pdf.fromHtml()`. |
| [`PdfFromUrlInput`](#pdffromurlinput) | The input of `pdf.fromUrl()`. |
| [`PdfOutput`](#pdfoutput) | A PDF stored as a public project file, from `totalum.pdf`. |

## `totalumPdf`

```ts
totalumPdf(options?: TotalumClientOptions): TotalumPdf
```

Creates the `pdf` namespace (the same object as `totalum.pdf`, scope `files:write`): each call renders a PDF and
stores it as a new public file of the project.

**Parameters**

| Name | Type | Required | Description |
|---|---|---|---|
| `options` | [`TotalumClientOptions`](https://www.totalum.app/docs/sdk/types#totalumclientoptions) | no | The project key, API URL and environment; each falls back to its environment variable. |
| `options.key` | `string` | no | Project key `tlm_pk_…`; default `TOTALUM_PROJECT_KEY`. Server-side only. |
| `options.apiUrl` | `string` | no | The Totalum API's URL: default `TOTALUM_SDK_API_URL`, else `https://sdk-api.totalum.app`. |
| `options.env` | `'dev' \| 'live'` | no | Sent as `x-totalum-env` on every call (the database it targets, and dev vs live usage); default `TOTALUM_ENV`, else `live`. |

**Returns** [`TotalumPdf`](#totalumpdf) — The `pdf` namespace: the methods below.

**Example**

```ts
import { totalumPdf } from 'totalum-sdk/pdf';

const pdf = totalumPdf();
const invoice = await pdf.fromHtml({ html: '<h1>Invoice 12</h1>', name: 'invoice-12' });
```

The namespace's type is exported as `TotalumPdf` (`import type { TotalumPdf } from 'totalum-sdk/pdf'`).

## `pdf.fromHtml`

```ts
pdf.fromHtml(input: PdfFromHtmlInput): Promise<{ key, fileName, url, usage }>
```

Renders HTML (with its CSS) to a PDF in a real browser and stores it as a project file: invoices, tickets,
certificates, reports. Use inline `<style>` and absolute image URLs. Every call stores a new file, even with the
same `name`.

**Parameters**

| Name | Type | Required | Description |
|---|---|---|---|
| `input` | [`PdfFromHtmlInput`](#pdffromhtmlinput) | yes | The HTML, the file name and the page options. |
| `input.name` | `string` | yes | The file name; `.pdf` is added when missing. Allowed: at least 1 character. |
| `input.html` | `string` | no | The HTML to render (or pass `base64HtmlString`). At most 10 MB. |
| `input.base64HtmlString` | `string` | no | The same HTML, base64-encoded, instead of `html`. |
| `input.options` | `{ format?, landscape?, margin?, printBackground?, headerTemplate?, footerTemplate?, displayHeaderFooter? }` | no | Paper and layout options. |
| `input.options.format` | `string` | no | The paper size: `A4` (default), `Letter`, `Legal`, `A3`, `A5`, `Tabloid`. |
| `input.options.landscape` | `boolean` | no | `true` for landscape pages. |
| `input.options.margin` | `Record<string, string>` | no | Page margins as CSS lengths: `{ top, right, bottom, left }` (`"20mm"`). |
| `input.options.printBackground` | `boolean` | no | `true` prints background colours and images. |
| `input.options.headerTemplate` | `string` | no | HTML of the header on every page (with `displayHeaderFooter`); `<span class="pageNumber"></span>` and `<span class="totalPages"></span>` are filled in. |
| `input.options.footerTemplate` | `string` | no | HTML of the footer on every page (with `displayHeaderFooter`). |
| `input.options.displayHeaderFooter` | `boolean` | no | `true` prints the header and footer templates. |
| `input.waitForSelector` | `string` | no | Wait until this CSS selector is on the page before printing. |

**Returns** `Promise<{ key, fileName, url, usage }>` — The stored PDF's `key`, `url` and `fileName`, and the credits it cost.

| Field | Type | Always present | Description |
|---|---|---|---|
| `key` | `string` | yes | The stored file's key (see `totalum.files`). |
| `fileName` | `string` | yes | The file name (`invoice-12.pdf`). |
| `url` | `string` | yes | The PDF's public URL. |
| `usage` | `{ credits }` | yes | What the call cost. |
| `usage.credits` | `number` | yes | Credits charged for this call. |

**Errors** — besides the [errors every call can throw](https://www.totalum.app/docs/sdk#errors):

| Code | HTTP | When |
|---|---|---|
| `PDF_TOO_LARGE` | 413 | The HTML is over 10 MB. |
| `CREATE_PDF_FROM_HTML_ERROR` | 502 | The HTML could not be rendered. |
| `VALIDATION_ERROR` | 400 | Neither `html` nor `base64HtmlString` was given, or `name` is empty. |

**Example**

```ts
const pdf = await totalum.pdf.fromHtml({
  html: '<style>body{font-family:sans-serif}</style><h1>Invoice 12</h1><p>Total: 49.00 €</p>',
  name: 'invoice-12',
  options: { format: 'A4', printBackground: true, margin: { top: '20mm', bottom: '20mm' } },
});
```

Result:

```json
{
  "url": "https://files.totalum-project.com/p/acme-crm/01M4P0S5Q26K57B47C6E9A65Y5.pdf",
  "fileName": "invoice-12.pdf",
  "key": "p/acme-crm/01M4P0S5Q26K57B47C6E9A65Y5.pdf",
  "usage": { "credits": 0.0004 }
}
```

## `pdf.fromUrl`

```ts
pdf.fromUrl(input: PdfFromUrlInput): Promise<{ key, fileName, url, usage }>
```

Prints a web page to a PDF and stores it as a project file — archive a page, or render one of your own pages
(a printable report route) as a PDF.

**Parameters**

| Name | Type | Required | Description |
|---|---|---|---|
| `input` | [`PdfFromUrlInput`](#pdffromurlinput) | yes | The page URL, the file name, page options, and cookies or headers to send. |
| `input.url` | `string` | yes | The page to print. Allowed: a URL. |
| `input.name` | `string` | yes | The file name; `.pdf` is added when missing. Allowed: at least 1 character. |
| `input.options` | `{ format?, landscape?, margin?, printBackground?, headerTemplate?, footerTemplate?, displayHeaderFooter? }` | no | Paper and layout options (as in `pdf.fromHtml`). |
| `input.options.format` | `string` | no | The paper size: `A4` (default), `Letter`, `Legal`, `A3`, `A5`, `Tabloid`. |
| `input.options.landscape` | `boolean` | no | `true` for landscape pages. |
| `input.options.margin` | `Record<string, string>` | no | Page margins as CSS lengths: `{ top, right, bottom, left }` (`"20mm"`). |
| `input.options.printBackground` | `boolean` | no | `true` prints background colours and images. |
| `input.options.headerTemplate` | `string` | no | HTML of the header on every page (with `displayHeaderFooter`); `<span class="pageNumber"></span>` and `<span class="totalPages"></span>` are filled in. |
| `input.options.footerTemplate` | `string` | no | HTML of the footer on every page (with `displayHeaderFooter`). |
| `input.options.displayHeaderFooter` | `boolean` | no | `true` prints the header and footer templates. |
| `input.cookies` | `(Record<string, unknown>)[]` | no | Cookies to send with the page request, `{ name, value, domain }` each — for a page behind a login. |
| `input.headers` | `Record<string, string>` | no | Extra HTTP headers to send with the page request. |

**Returns** `Promise<{ key, fileName, url, usage }>` — The stored PDF's `key`, `url` and `fileName`, and the credits it cost.

| Field | Type | Always present | Description |
|---|---|---|---|
| `key` | `string` | yes | The stored file's key (see `totalum.files`). |
| `fileName` | `string` | yes | The file name (`invoice-12.pdf`). |
| `url` | `string` | yes | The PDF's public URL. |
| `usage` | `{ credits }` | yes | What the call cost. |
| `usage.credits` | `number` | yes | Credits charged for this call. |

**Errors** — besides the [errors every call can throw](https://www.totalum.app/docs/sdk#errors):

| Code | HTTP | When |
|---|---|---|
| `URL_NOT_ALLOWED` | 400 | Not a public `http(s)` URL. |
| `GENERATE_PDF_BY_URL_ERROR` | 502 | The page could not be rendered. |

**Example**

```ts
const pdf = await totalum.pdf.fromUrl({
  url: 'https://acme.totalum.app/reports/2026-09/print',
  name: 'report-2026-09',
  options: { format: 'A4', landscape: true },
});
```

Result:

```json
{
  "url": "https://files.totalum-project.com/p/acme-crm/01M4P1D2E3F4G5H6J7K8M9N0PQ.pdf",
  "fileName": "report-2026-09.pdf",
  "key": "p/acme-crm/01M4P1D2E3F4G5H6J7K8M9N0PQ.pdf",
  "usage": { "credits": 0.0006 }
}
```

## `PdfFromHtmlInput`

The input of `pdf.fromHtml()`.

| Name | Type | Required | Description |
|---|---|---|---|
| `name` | `string` | yes | The file name; `.pdf` is added when missing. Allowed: at least 1 character. |
| `html` | `string` | no | The HTML to render (or pass `base64HtmlString`). At most 10 MB. |
| `base64HtmlString` | `string` | no | The same HTML, base64-encoded, instead of `html`. |
| `options` | `{ format?, landscape?, margin?, printBackground?, headerTemplate?, footerTemplate?, displayHeaderFooter? }` | no | Paper and layout options. |
| `options.format` | `string` | no | The paper size: `A4` (default), `Letter`, `Legal`, `A3`, `A5`, `Tabloid`. |
| `options.landscape` | `boolean` | no | `true` for landscape pages. |
| `options.margin` | `Record<string, string>` | no | Page margins as CSS lengths: `{ top, right, bottom, left }` (`"20mm"`). |
| `options.printBackground` | `boolean` | no | `true` prints background colours and images. |
| `options.headerTemplate` | `string` | no | HTML of the header on every page (with `displayHeaderFooter`); `<span class="pageNumber"></span>` and `<span class="totalPages"></span>` are filled in. |
| `options.footerTemplate` | `string` | no | HTML of the footer on every page (with `displayHeaderFooter`). |
| `options.displayHeaderFooter` | `boolean` | no | `true` prints the header and footer templates. |
| `waitForSelector` | `string` | no | Wait until this CSS selector is on the page before printing. |

## `PdfFromUrlInput`

The input of `pdf.fromUrl()`.

| Name | Type | Required | Description |
|---|---|---|---|
| `url` | `string` | yes | The page to print. Allowed: a URL. |
| `name` | `string` | yes | The file name; `.pdf` is added when missing. Allowed: at least 1 character. |
| `options` | `{ format?, landscape?, margin?, printBackground?, headerTemplate?, footerTemplate?, displayHeaderFooter? }` | no | Paper and layout options (as in `pdf.fromHtml`). |
| `options.format` | `string` | no | The paper size: `A4` (default), `Letter`, `Legal`, `A3`, `A5`, `Tabloid`. |
| `options.landscape` | `boolean` | no | `true` for landscape pages. |
| `options.margin` | `Record<string, string>` | no | Page margins as CSS lengths: `{ top, right, bottom, left }` (`"20mm"`). |
| `options.printBackground` | `boolean` | no | `true` prints background colours and images. |
| `options.headerTemplate` | `string` | no | HTML of the header on every page (with `displayHeaderFooter`); `<span class="pageNumber"></span>` and `<span class="totalPages"></span>` are filled in. |
| `options.footerTemplate` | `string` | no | HTML of the footer on every page (with `displayHeaderFooter`). |
| `options.displayHeaderFooter` | `boolean` | no | `true` prints the header and footer templates. |
| `cookies` | `(Record<string, unknown>)[]` | no | Cookies to send with the page request, `{ name, value, domain }` each — for a page behind a login. |
| `headers` | `Record<string, string>` | no | Extra HTTP headers to send with the page request. |

## `PdfOutput`

A PDF stored as a public project file, from `totalum.pdf`.

| Field | Type | Always present | Description |
|---|---|---|---|
| `url` | `string` | yes | The PDF's public URL. |
| `fileName` | `string` | yes | The file name (`invoice-12.pdf`). |
| `key` | `string` | yes | The stored file's key (see `totalum.files`). |
