zztaeha

Options

23 options, in the one object render, download and preview take, in daeepdf 1.2.0. Each says what it does, what it is when you leave it out and what it fails with, with an example that was type-checked and run in Chrome against 1.2.0.

Lengths are pt as numbers, or strings in mm, cm, in, pt or px. An option that's misspelled, of the wrong type or outside its list throws 'invalid-option' at once.

The page

The paper, its margins, what happens to content wider than the page, and the header and footer on every page. A template's own @page rule sets the size, orientation and margins wherever size, landscape and margin are left out, and an option you pass wins over it. All six apply to the preview as they do to the file.

size
size?: PageSize

The paper: one of six named sizes, or a width and a height.

The names are 'A3', 'A4', 'A5', 'Letter', 'Legal' and 'Tabloid', each portrait and case-sensitive; PageSize has their sizes in pt. { width, height } takes each side as a Length, from 3 to 14,400 pt. landscape turns either kind.

Left out, the size comes from the template's @page { size }: a CSS keyword such as A5, letter or jis-b4, one or two lengths, or an orientation alone, which turns A4. Only a plain @page is read, the last to set a size winning, and @page { size: auto } counts as none; one under :first, :left, a page name or @media screen is ignored. A size you pass replaces the template's size but keeps its orientation, so 'Letter' on a template with @page { size: A4 landscape } gives Letter turned sideways.

The PDF's pages are exactly this size. Chrome's print lays a page out in whole CSS px, the paper rounded up to whole points and then to whole px, so A4 lays out 795 × 1123 px, and daeepdf lays out on the same.

A name outside the six, or a side that isn't a Length, fails with 'invalid-option'. A side outside 3 to 14,400 pt fails with 'invalid-page-size', the template's @page size too.

import pdf from 'daeepdf'

const html = `<!doctype html><html><head><style>
  @font-face { font-family: Inter; src: url('/fonts/inter-var.ttf'); }
  @page { size: A4 landscape; }
  body { font: 12pt Inter; }
</style></head><body><p>Quarterly figures</p></body></html>`

const asIs = await pdf.render(html) // 841.89 × 595.28 pt: the template's A4, landscape
const letter = await pdf.render(html, { size: 'Letter' }) // 792 × 612 pt: Letter, still landscape
const label = await pdf.render(html, { size: { width: '150mm', height: '100mm' } }) // 425.2 × 283.46 pt
landscape
landscape?: boolean

Turns the paper so its long side runs across the page.

true puts the longer side across, whatever size gives: 'A4' becomes 841.89 × 595.28 pt, and a size that is already wide stays as it is. false overrides a template's landscape keyword and keeps a named size portrait. It doesn't turn a wide { width, height } or wide @page lengths tall, though: those print as given.

Left out, the orientation keyword of the template's @page { size } decides, as in @page { size: A4 landscape }, and without one the page is portrait. The preview's cards turn with it. Anything but true or false fails with 'invalid-option'.

import pdf from 'daeepdf'

const wide = await pdf.render(html, { size: 'A4', landscape: true }) // 841.89 × 595.28 pt
const given = await pdf.render(html, { size: { width: 842, height: 595 }, landscape: false }) // 842 × 595 pt
  • Default: the template's @page orientation, else portrait
  • Fails with: 'invalid-option'.
margin
margin?: Length | { top?: Length; right?: Length; bottom?: Length; left?: Length }

Space kept clear around the content on every page, as one length or one per side.

One Length sets all four sides, and an object sets each on its own, a side left out being 0. The content lays out in what the margins leave of the page, placed at the margins rounded to whole CSS px, as Chrome's print places it: '20mm' is 56.69 pt, and the content starts 57 pt in.

The margins stay blank. Content reaching into them is clipped, as in Chrome's print, and what lies wholly in them is left out of the file. A header and footer sit inside them: the top margin, the header, the content, the footer, then the bottom margin.

Left out, the margins come from the template's plain @page rule: margin and margin-top and the rest, in absolute lengths, em (12 pt, the page context's 16 px) or percentages, of the paper's width for left and right and of its height for top and bottom. A margin you pass replaces all four of the template's, so { top: 10 } leaves the other sides at 0, not at what @page says. Chrome's print dialog adds margins of its own where @page sets none, and daeepdf adds none.

A side that isn't a Length, such as '10%' or a negative number, fails with 'invalid-option'. Margins that leave less than 3 pt of the page across or down fail with 'margins-too-large', the template's @page margins too.

import pdf from 'daeepdf'

// html holds a chart 700 pt tall, then a line of text
const half = await pdf.render(html, { margin: '0.5in' }) // 1 page
const inch = await pdf.render(html, { margin: { top: '1in', bottom: '1in', left: '20mm', right: '20mm' } })
inch.pages // 2: an inch top and bottom leaves too little room for both

const code = await pdf.render(html, { size: 'A5', margin: '10cm' }).catch((e: { code: string }) => e.code)
code // 'margins-too-large': A5 is 14.8 cm across
top?: Length
Above the content, and above the header. 0 when left out.
right?: Length
To the right of the content, the header and the footer. 0 when left out.
bottom?: Length
Below the content, and below the footer. 0 when left out.
left?: Length
To the left of the content, the header and the footer. 0 when left out.
shrinkToFit
shrinkToFit?: boolean

Scales content wider than the page down onto it, as Chrome's print does.

When the first layout reaches past the page's right edge, the page is laid out again as wide as the content reaches, up to 1.5 times its width, and every page is scaled down by the same factor onto the paper, about its top left corner. A box 1000 px wide on A4, whose page lays out 795 px wide, prints at 0.795 of its size, and as each page then holds more, the document can have fewer pages. Whatever reaches past 1.5 times the width is cut at the right edge.

Only what reaches right counts: content past the left edge, content an overflow box clips, and position: fixed content never set it off. A word too long for its line overflows rather than breaking, as in Chrome, so a long URL can shrink every page.

false keeps the content at its size, cut at the page's right edge, where Chrome's print would shrink it. The preview shrinks its cards' content the same way.

import pdf from 'daeepdf'

// html holds a chart 1000 px wide and 1100 px tall, its label Q4 at the right
const fitted = await pdf.render(html) // 1 page, all of it at 0.795 of its size
const cut = await pdf.render(html, { shrinkToFit: false }) // 2 pages, Q4 cut off past the edge
header
header?: Band

HTML at the top of every page, inside the margin.

A Band: a string, the same on every page, or a function of { page, pages } returning each page's HTML, or '' for a page without one. It lays out as wide as the page area and as tall as its content, and the content's area shrinks to make room, so the template needs no padding to clear it.

Every page reserves the height of the tallest page's header, whether it has one or not. daeepdf lays page 1's header out to measure it, lays the document out, then lays out each page's header; if one came out taller, the document is laid out once more with that height, and a header taller still is clipped to it.

A header is a small document of its own. It can use any font the template declares with @font-face, but none of the template's other CSS, not even its body font, so set its font in the header itself: text in no declared font is left out, as in the template. Its links work, and screen readers skip it, as it is marked as an artifact.

Chrome's print has nothing like it. Its own header line holds the date and the title, and the @page margin boxes it draws, such as @top-center, aren't read by daeepdf. The preview shows each card's header as it prints.

A header and footer that leave less than 3 pt of the page between them fail with 'bands-too-tall', and a value that is neither a string nor a function with 'invalid-option'. Whatever your function throws reaches you as it is.

import pdf from 'daeepdf'

const file = await pdf.render(html, {
  margin: '15mm',
  header: '<p style="font: 8pt Inter; color: #666; border-bottom: 0.5pt solid">Acme Inc. – Confidential</p>',
})
// Acme Inc. – Confidential at the top of both pages, 15 mm from the edge

About the document

What the file says about itself: the document information a viewer shows in its properties, the language a screen reader reads it in, and the outline. None of it changes the layout, and the preview ignores it.

title
title?: string

The document's title, for a viewer to show in place of the file name.

It is written to the document information, and with pdfA or pdfUA to the XMP metadata, which under pdfA holds it alone. A viewer chooses whether to show it or the file name, and with pdfUA the file asks it to show the title.

daeepdf doesn't read the template's <title>, which Chrome's print takes as the title, so pass it here. An empty string counts as none. pdfUA needs one, and fails with 'missing-title' without it, an empty one included. Anything but a string fails with 'invalid-option'.

import pdf from 'daeepdf'

const file = await pdf.render(html, { title: 'Q4 Report – Acme Inc.' })
// Q4 Report – Acme Inc. in the viewer's title bar or document properties
author
author?: string

Who wrote the document, for its properties.

It is written to the document information, and with pdfA or pdfUA to the XMP metadata as dc:creator. An empty string counts as none, and the template's <meta name="author"> isn't read. Anything but a string fails with 'invalid-option'.

import pdf from 'daeepdf'

const file = await pdf.render(html, { author: 'Ada Lovelace' })
subject
subject?: string

What the document is about, for its properties.

It is written to the document information, and with pdfA or pdfUA to the XMP metadata as dc:description. An empty string counts as none, and anything but a string fails with 'invalid-option'.

import pdf from 'daeepdf'

const file = await pdf.render(html, { title: 'Q4 Report', subject: 'Quarterly sales summary' })
creator
creator?: string

The app that made the document, for its properties.

It is written to the document information as its Creator, and with pdfA or pdfUA to the XMP metadata as xmp:CreatorTool. The producer beside it is always daeepdf 1.2.0, and can't be set. An empty string counts as none, and anything but a string fails with 'invalid-option'.

import pdf from 'daeepdf'

const file = await pdf.render(html, { creator: 'Acme Billing 4.2' })
keywords
keywords?: string[]

Words to find the document by, for its properties.

A list of strings, written as one string joined with , , which is how PDF holds them, so a keyword with a comma in it reads as two. With pdfA or pdfUA the string goes in the XMP metadata too. Blank keywords are left out, and an empty list writes no entry. Anything but a list of strings fails with 'invalid-option'.

import pdf from 'daeepdf'

const file = await pdf.render(html, { keywords: ['invoice', 'acme', '2026'] })
// written as invoice, acme, 2026
language
language?: string

The text's language, like 'en-US', for screen readers and read-aloud.

It is written as the document's language, and with pdfA or pdfUA in the XMP metadata too. It isn't checked as a language tag, so any string is written as given.

Left out, or '', the template's <html lang> is used; a lang on any other element, or on your app page's own <html>, isn't. With neither, the file names no language, except under pdfA or pdfUA, which write 'en-US'.

It changes only what the file says. The layout follows the template's own lang, which picks the quotation marks, hyphenation and the like, so set the language there and leave this out.

import pdf from 'daeepdf'

const html = `<!doctype html><html lang="de"><head><style>
  @font-face { font-family: Inter; src: url('/fonts/inter-var.ttf'); }
  body { font: 12pt Inter; }
</style></head><body><p><q>Guten Tag</q></p></body></html>`

const file = await pdf.render(html) // de, from <html lang>, and German quotation marks
const swiss = await pdf.render(html, { language: 'de-CH' }) // de-CH, the layout unchanged
bookmarks
bookmarks?: boolean | Bookmark[]

The PDF's outline, built from the headings or listed yourself.

true builds it from the h1 to h6 headings in document order, each a level under the nearest heading of a higher rank before it, so an h3 straight after an h1 sits one level down, as Chrome's print nests them. An entry's title is the heading's rendered text, its whitespace collapsed. An empty or hidden heading makes none, and nor do headings in a header or footer.

A list of Bookmarks gives the outline yourself, each pointing at an element's id or at a page. One whose id no element has is left out with a 'bookmark-target-missing' warning, and its children move up into its place.

A viewer that shows outlines opens the file with it showing, every entry closed. false, an empty list or leaving it out writes none. Anything else fails with 'invalid-option', naming the entry: a missing title or target, a page that isn't a whole number from 1, or a key a Bookmark doesn't have.

import pdf from 'daeepdf'

// html holds h1 Report, h2 Sales, h2 Costs on page 2, h3 Travel
const file = await pdf.render(html, { bookmarks: true })
// Report, with Sales and Costs under it, and Travel under Costs

Standards and security

Tagging, the PDF/A and PDF/UA standards, and encryption. Every export is tagged unless accessible is false. pdfA and pdfUA add what their standards ask for, and fail at once where the other options can't meet them: PDF/A with security, PDF/UA without a title. The preview ignores all four.

accessible
accessible?: boolean

Tags the document's structure, so screen readers can follow it.

A tagged file carries a structure tree built from the template's HTML: headings, paragraphs, lists, tables, figures with their alternative text, links and form fields, in reading order. What isn't content, such as backgrounds, borders, headers and footers, is marked as an artifact that screen readers skip, and role="presentation", role="none" or aria-hidden="true" marks an element and all it holds the same way. The tree is only as good as the HTML, so use real headings, lists and tables.

false writes an untagged file, which is smaller. pdfA and pdfUA tag the file whatever this says, as both standards need it. A document with nothing to tag, such as an empty page, isn't marked as tagged either way.

Anything but true or false fails with 'invalid-option'.

import pdf from 'daeepdf'

const tagged = await pdf.render(html) // tagged
const plain = await pdf.render(html, { accessible: false }) // untagged
const archived = await pdf.render(html, { accessible: false, pdfA: true }) // tagged: PDF/A needs it
pdfA
pdfA?: boolean

Writes the file as PDF/A-4, the PDF 2.0 standard for archiving.

The file declares PDF/A-4 in its XMP metadata, embeds an sRGB output intent, and is tagged. Its document information lives in the XMP metadata alone, with no Info dictionary, so a tool that reads only that dictionary shows no title. There is no way to get PDF/A-1, 2 or 3.

PDF/A forbids drawing a missing glyph, so a character no declared font covers is left out of the page, with a 'glyph-missing' warning that counts them, though copying the text still gives it. A CMYK JPEG without its own color profile goes in as the browser decodes it, in sRGB. Without language or an <html lang>, the language is written as 'en-US'.

PDF/A forbids encryption, so security, even {}, fails with 'pdfa-encryption' before any work. It combines with pdfUA, and the file then declares both.

import pdf from 'daeepdf'

const file = await pdf.render(html, { pdfA: true, title: 'Annual Report 2026' }) // PDF/A-4, tagged

const code = await pdf.render(html, { pdfA: true, security: {} }).catch((e: { code: string }) => e.code)
code // 'pdfa-encryption'
pdfUA
pdfUA?: boolean

Writes the file as PDF/UA-2, the PDF 2.0 standard for accessibility.

The file is tagged, declares PDF/UA-2 in its XMP metadata, and asks viewers to show its title in place of its file name. It combines with pdfA and with security.

PDF/UA needs a title: without one, or with one of only spaces, the export fails with 'missing-title' before any work. Every figure needs alternative text, its accessible name: aria-labelledby, aria-label, alt, an SVG's <title> or title. alt="" marks an image as decoration; an 'alt-missing' warning counts the images with neither. As under pdfA, a character no declared font covers is left out with a 'glyph-missing' warning, and the language is 'en-US' when nothing names one.

The structure comes from the template's HTML, so the file is only as accessible as its headings, lists, tables and reading order.

import pdf from 'daeepdf'

const file = await pdf.render(html, { pdfUA: true, title: 'Quarterly report' })
file.warnings.map((w) => w.code) // ['alt-missing']: the logo has no alt

const code = await pdf.render(html, { pdfUA: true }).catch((e: { code: string }) => e.code)
code // 'missing-title'
security
security?: { password?: string; ownerPassword?: string; allow?: Permission[] }

Encrypts the file with AES-256, with a password to open it and limits on what a viewer may do.

Any security object encrypts the file, with AES-256 as PDF 2.0 defines it, even {}, which asks for no password and allows everything. Leave it out for a file anyone can open, print, copy and change.

The limits are honored by viewers, not enforced by the file: a viewer that ignores them can print or copy anyway, and even with nothing allowed, assistive tools may still extract the text. Only security.password keeps someone without it from reading the file.

Passwords are prepared with SASLprep before they are hashed, as PDF 2.0 asks, so a no-break space counts as a space and a full-width letter as its plain one, as a viewer reads them. A password with a control or private-use character, or with right-to-left letters among left-to-right ones, fails with 'invalid-password'. Only the first 127 bytes of a password's UTF-8 count.

PDF/A forbids encryption, so security with pdfA fails with 'pdfa-encryption'. A misspelled field or permission fails with 'invalid-option', naming the closest one.

import pdf from 'daeepdf'

const file = await pdf.render(html, {
  security: { password: 'open sesame', allow: ['print'] },
})
// asks for the password; once open, it may be printed but not copied or changed
password?: string
The password a viewer asks for before it shows anything. Left out or empty, the file opens without one, and only the security.allow limits apply.
ownerPassword?: string
The password that lifts the limits, in a viewer that asks for one. Left out or empty, each export makes a random one, so nobody can lift them.
allow?: Permission[]
The Permissions a viewer has, from 'print', 'copy', 'edit', 'annotate' and 'fill'. What isn't listed is withheld: left out, all five are allowed, and [] allows none.

Loading and output

What becomes of the template's forms, a font for text in no declared font, whether your app's stylesheets apply, and how long to wait for what is still loading. All but fillable apply to the preview too.

fillable
fillable?: boolean

Keeps form fields fillable, or with false prints them flat, as they show.

The template's <input>, <textarea> and <select> elements, checkboxes and radio buttons among them, become form fields a viewer can fill in, each holding the value the template gave it. Text and choice fields need a declared font, as other text does, and are left out without one.

false draws each field into the page as it shows, with no form behind it: the file has no fields, and the values print as text, as they do in Chrome's print. The file stays tagged, and conforms to PDF/UA-2 under pdfUA.

Anything but true or false fails with 'invalid-option'.

import pdf from 'daeepdf'

const form = await pdf.render(html) // 1 field, filled in with Ada Lovelace
const flat = await pdf.render(html, { fillable: false }) // no fields: Ada Lovelace printed as text
fallbackFont
fallbackFont?: string

A font file for text in any font the template doesn't declare, which is otherwise left out.

daeepdf prints text only in fonts the template declares with @font-face, since only those can be embedded, and leaves out text in any other font, the browser's default included, with a 'font-not-declared' warning. With fallbackFont, every family the template names without declaring it gives way to this font, so that text lays out and prints in it, while families the template declares keep their own fonts.

It is the URL of a TTF, OTF or TTC file, relative to your page; a WOFF or WOFF2 file can't be embedded. One that fails to load, or doesn't arrive within loadTimeout, gives a 'font-not-loaded' warning, and the text it would have printed is left out as before. An empty string counts as none.

It covers headers, footers and form fields too, and the preview lays out in it. Choose one that covers every script your data may hold.

import pdf from 'daeepdf'

const html = '<!doctype html><html><body><p style="font-family: Arial">Hello</p></body></html>'
const bare = await pdf.render(html)
bare.warnings.map((w) => w.code) // ['font-not-declared'], and nothing printed
const covered = await pdf.render(html, { fallbackFont: '/fonts/inter-var.ttf' }) // Hello, in Inter
  • Default: none: text in an undeclared font is left out
  • Fails with: 'invalid-option'.
useAppStyles
useAppStyles?: boolean

Lets your app page's own stylesheets reach the template.

The template lays out in a frame of its own, and by default the frame mirrors your app's page: its <style> blocks, <link> stylesheets and adopted stylesheets, its color-scheme meta, and of its <html> and <body> attributes class, style, lang, dir and the data-*-theme, -scheme and -mode ones. A template can then use your framework's classes and theme variables, and a dark-mode class on your <html> reaches your CSS. Other attributes, such as a user id in data-user, stay out.

The template's own CSS applies to the template alone, and where it and your app's CSS set the same property with selectors as specific, the template's wins.

false lays the template out on its own, as on a blank page, so it prints the same in any app, and an app stylesheet that is slow to load can't hold the export up. Headers, footers and the preview follow it. Anything but true or false fails with 'invalid-option'.

import pdf from 'daeepdf'

// the app's own stylesheet has .page-break { break-before: page }, and html uses it
const withApp = await pdf.render(html) // 2 pages
const alone = await pdf.render(html, { useAppStyles: false }) // 1 page: the class means nothing here
loadTimeout
loadTimeout?: number

How long an export waits for fonts, images and stylesheets to load, in ms.

An export loads the template's fonts and images and your app's stylesheets, and waits no longer than this for any of them. What hasn't arrived by then is canceled and left out: a font with a 'font-not-loaded' warning, the text set in it then left out too, an image with an 'image-skipped' warning, and an app stylesheet without one. A file it gave up on isn't waited for again in the same export's headers, footers and later layouts. The preview waits as long for its own.

It is a number of ms from 0 to 2,147,483,647, about 24.8 days, the longest a browser timer holds, and 0 gives up at once on every font and image still to be fetched. Anything else, a negative number, Infinity or a string, fails with 'invalid-option'.

import pdf from 'daeepdf'

const patient = await pdf.render(html, { loadTimeout: 30_000 }) // Inter arrives in time: Hello prints
const hasty = await pdf.render(html, { loadTimeout: 0 })
hasty.warnings.map((w) => w.code) // ['font-not-loaded', 'font-not-declared']: gave up on Inter, so no text

Control

Turning warnings into a failure, and stopping an export or a preview partway. The preview ignores strict.

strict
strict?: boolean

Fails the export with 'strict' when it has any warning, instead of finishing.

An export finishes even when part of the template can't print, and says what in PdfFile.warnings. With strict, any warning fails it instead, with a PdfError whose PdfError.warnings lists them and whose message repeats each one. No file comes back, and pdf.download saves nothing.

The whole export runs first, so a failure costs as much time as a success. Use it in tests, to catch a template that has stopped printing something, or where a PDF with something missing is worse than none. The preview ignores it. Anything but true or false fails with 'invalid-option'.

import pdf, { PdfError } from 'daeepdf'

try {
  await pdf.render(html, { strict: true })
} catch (e) {
  if (!(e instanceof PdfError) || e.code !== 'strict') throw e
  e.warnings.map((w) => w.code) // ['font-not-declared']: Helvetica isn't declared
}
signal
signal?: AbortSignal

Stops an export partway, failing it with 'aborted'.

Pass an AbortSignal, an AbortController's or one from AbortSignal.timeout. The export checks it between its steps: loading, each layout, each page's header and footer, and drawing. Once it is aborted, the waits for loads end at once and the export stops at its next check, failing with 'aborted'. The signal's reason is the error's cause, and PdfError.warnings holds the warnings so far.

A signal aborted before the call fails the export before it lays anything out, but after the options are checked: an invalid option still fails with 'invalid-option', and pdfUA without a title with 'missing-title'. Aborting after the export resolved changes nothing. A preview stops at it too, before its new pages are swapped in, keeping the pages it showed. Anything but an AbortSignal fails with 'invalid-option'.

import pdf, { PdfError } from 'daeepdf'

const controller = new AbortController()
const job = pdf.render(html, { signal: controller.signal })
controller.abort('Canceled by the user') // from a Cancel button, say
const e = await job.catch((e: unknown) => e)
e instanceof PdfError && [e.code, e.cause] // ['aborted', 'Canceled by the user']

const bounded = await pdf.render(html, { signal: AbortSignal.timeout(60_000) }) // gives up after a minute

Type to search.