Docs
BlogHomeStart building

PDF (totalum-sdk/pdf)


import … from 'totalum-sdk/pdf' — or the same namespace on totalum from 'totalum-sdk'. Install, configuration and errors: Totalum 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 typeWhat it does
totalumPdfCreates 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.fromHtmlRenders HTML (with its CSS) to a PDF in a real browser and stores it as a project file: invoices, tickets, certificates, reports.
pdf.fromUrlPrints 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.
PdfFromHtmlInputThe input of pdf.fromHtml().
PdfFromUrlInputThe input of pdf.fromUrl().
PdfOutputA 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

NameTypeRequiredDescription
optionsTotalumClientOptionsnoThe project key, API URL and environment; each falls back to its environment variable.
options.keystringnoProject key tlm_pk_…; default TOTALUM_PROJECT_KEY. Server-side only.
options.apiUrlstringnoThe Totalum API's URL: default TOTALUM_SDK_API_URL, else https://sdk-api.totalum.app.
options.env'dev' | 'live'noSent as x-totalum-env on every call (the database it targets, and dev vs live usage); default TOTALUM_ENV, else live.

Returns 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

NameTypeRequiredDescription
inputPdfFromHtmlInputyesThe HTML, the file name and the page options.
input.namestringyesThe file name; .pdf is added when missing. Allowed: at least 1 character.
input.htmlstringnoThe HTML to render (or pass base64HtmlString). At most 10 MB.
input.base64HtmlStringstringnoThe same HTML, base64-encoded, instead of html.
input.options{ format?, landscape?, margin?, printBackground?, headerTemplate?, footerTemplate?, displayHeaderFooter? }noPaper and layout options.
input.options.formatstringnoThe paper size: A4 (default), Letter, Legal, A3, A5, Tabloid.
input.options.landscapebooleannotrue for landscape pages.
input.options.marginRecord<string, string>noPage margins as CSS lengths: { top, right, bottom, left } ("20mm").
input.options.printBackgroundbooleannotrue prints background colours and images.
input.options.headerTemplatestringnoHTML of the header on every page (with displayHeaderFooter); <span class="pageNumber"></span> and <span class="totalPages"></span> are filled in.
input.options.footerTemplatestringnoHTML of the footer on every page (with displayHeaderFooter).
input.options.displayHeaderFooterbooleannotrue prints the header and footer templates.
input.waitForSelectorstringnoWait 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.

FieldTypeAlways presentDescription
keystringyesThe stored file's key (see totalum.files).
fileNamestringyesThe file name (invoice-12.pdf).
urlstringyesThe PDF's public URL.
usage{ credits }yesWhat the call cost.
usage.creditsnumberyesCredits charged for this call.

Errors — besides the errors every call can throw:

CodeHTTPWhen
PDF_TOO_LARGE413The HTML is over 10 MB.
CREATE_PDF_FROM_HTML_ERROR502The HTML could not be rendered.
VALIDATION_ERROR400Neither 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

NameTypeRequiredDescription
inputPdfFromUrlInputyesThe page URL, the file name, page options, and cookies or headers to send.
input.urlstringyesThe page to print. Allowed: a URL.
input.namestringyesThe file name; .pdf is added when missing. Allowed: at least 1 character.
input.options{ format?, landscape?, margin?, printBackground?, headerTemplate?, footerTemplate?, displayHeaderFooter? }noPaper and layout options (as in pdf.fromHtml).
input.options.formatstringnoThe paper size: A4 (default), Letter, Legal, A3, A5, Tabloid.
input.options.landscapebooleannotrue for landscape pages.
input.options.marginRecord<string, string>noPage margins as CSS lengths: { top, right, bottom, left } ("20mm").
input.options.printBackgroundbooleannotrue prints background colours and images.
input.options.headerTemplatestringnoHTML of the header on every page (with displayHeaderFooter); <span class="pageNumber"></span> and <span class="totalPages"></span> are filled in.
input.options.footerTemplatestringnoHTML of the footer on every page (with displayHeaderFooter).
input.options.displayHeaderFooterbooleannotrue prints the header and footer templates.
input.cookies(Record<string, unknown>)[]noCookies to send with the page request, { name, value, domain } each — for a page behind a login.
input.headersRecord<string, string>noExtra 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.

FieldTypeAlways presentDescription
keystringyesThe stored file's key (see totalum.files).
fileNamestringyesThe file name (invoice-12.pdf).
urlstringyesThe PDF's public URL.
usage{ credits }yesWhat the call cost.
usage.creditsnumberyesCredits charged for this call.

Errors — besides the errors every call can throw:

CodeHTTPWhen
URL_NOT_ALLOWED400Not a public http(s) URL.
GENERATE_PDF_BY_URL_ERROR502The 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().

NameTypeRequiredDescription
namestringyesThe file name; .pdf is added when missing. Allowed: at least 1 character.
htmlstringnoThe HTML to render (or pass base64HtmlString). At most 10 MB.
base64HtmlStringstringnoThe same HTML, base64-encoded, instead of html.
options{ format?, landscape?, margin?, printBackground?, headerTemplate?, footerTemplate?, displayHeaderFooter? }noPaper and layout options.
options.formatstringnoThe paper size: A4 (default), Letter, Legal, A3, A5, Tabloid.
options.landscapebooleannotrue for landscape pages.
options.marginRecord<string, string>noPage margins as CSS lengths: { top, right, bottom, left } ("20mm").
options.printBackgroundbooleannotrue prints background colours and images.
options.headerTemplatestringnoHTML of the header on every page (with displayHeaderFooter); <span class="pageNumber"></span> and <span class="totalPages"></span> are filled in.
options.footerTemplatestringnoHTML of the footer on every page (with displayHeaderFooter).
options.displayHeaderFooterbooleannotrue prints the header and footer templates.
waitForSelectorstringnoWait until this CSS selector is on the page before printing.

#PdfFromUrlInput

The input of pdf.fromUrl().

NameTypeRequiredDescription
urlstringyesThe page to print. Allowed: a URL.
namestringyesThe file name; .pdf is added when missing. Allowed: at least 1 character.
options{ format?, landscape?, margin?, printBackground?, headerTemplate?, footerTemplate?, displayHeaderFooter? }noPaper and layout options (as in pdf.fromHtml).
options.formatstringnoThe paper size: A4 (default), Letter, Legal, A3, A5, Tabloid.
options.landscapebooleannotrue for landscape pages.
options.marginRecord<string, string>noPage margins as CSS lengths: { top, right, bottom, left } ("20mm").
options.printBackgroundbooleannotrue prints background colours and images.
options.headerTemplatestringnoHTML of the header on every page (with displayHeaderFooter); <span class="pageNumber"></span> and <span class="totalPages"></span> are filled in.
options.footerTemplatestringnoHTML of the footer on every page (with displayHeaderFooter).
options.displayHeaderFooterbooleannotrue prints the header and footer templates.
cookies(Record<string, unknown>)[]noCookies to send with the page request, { name, value, domain } each — for a page behind a login.
headersRecord<string, string>noExtra HTTP headers to send with the page request.

#PdfOutput

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

FieldTypeAlways presentDescription
urlstringyesThe PDF's public URL.
fileNamestringyesThe file name (invoice-12.pdf).
keystringyesThe stored file's key (see totalum.files).