zztaeha

Errors

13 error codes in daeepdf 1.2.0. Every error daeepdf throws is a PdfError carrying one of these as its code, with a message saying what to change.

Each entry says when it's thrown and what to change, with an example that was run in Chrome against 1.2.0.

Your input

Values a call can't use. Every error daeepdf throws is a PdfError whose PdfError.code is one of these codes, with a message for people that starts [daeepdf] . 'invalid-option' comes at once, before anything loads; the others come when the work reaches the value: a page size when the template is read, a password when the file is encrypted, a container when the preview adds its pages.

'invalid-option'

An argument or option a call can't use, caught before any work starts.

Every call checks what it is given first, and throws this for an option PdfOptions doesn't have, naming the closest one when it is a near miss and listing them all otherwise. It throws it too for a value of the wrong type or outside its list: a size name that isn't one of the six exactly as written, so 'a4' fails, a negative length or a length string without mm, cm, in, pt or px, an entry of security.allow that isn't a Permission, a loadTimeout under 0 or over 2,147,483,647, a Bookmark.target that is neither an id nor a page from 1, or a signal that isn't an AbortSignal. An option set to undefined counts as left out.

The calls' own arguments are checked the same way: HTML that isn't a string, options that aren't an object, a pdf.download filename that is empty or only spaces, and a pdf.preview container that isn't an element.

The message names the option and what it got: "size "a4" isn't a size: use "A3", "A4", …", or "The options object has no "margn" – did you mean "margin"?". Fix the call as it says. In TypeScript, PdfOptions catches most of these before the code runs; they reach daeepdf from options built at runtime, such as settings read from a file.

import pdf, { PdfError } from 'daeepdf'

// Settings read from a file, with a typo in them
const options = JSON.parse('{ "size": "Letter", "margn": "1in" }')

try {
  await pdf.render('<p>Invoice 42</p>', options)
} catch (e) {
  if (!(e instanceof PdfError)) throw e
  e.code // 'invalid-option'
  e.message // '[daeepdf] The options object has no "margn" – did you mean "margin"?'
}
'invalid-page-size'

A page side is under 3 pt or over 14,400 pt.

Each side of the paper must be from 3 to 14,400 pt, about 1 mm to 200 in, the range PDF itself allows. It is checked for a size of { width, height } and for the lengths of the template's @page { size }, once the template is read, and pdf.preview checks it too. The named sizes all fit, and landscape only swaps the sides. An @page size of zero or less isn't read at all, and the page is A4, the default.

A size that isn't a length at all, or is negative, is 'invalid-option' instead, thrown before anything loads. The message gives both sides in pt.

To fix it, keep both sides in range. A page 14,400 pt tall is the longest a PDF holds, so a longer receipt or banner has to run onto more pages.

import pdf, { PdfError } from 'daeepdf'

const receipt = (height: string) => pdf.render('<p>Receipt</p>', { size: { width: '80mm', height } })

const error = await receipt('201in').catch((e: unknown) => e)
error instanceof PdfError && error.code // 'invalid-page-size'

// 200 in is 14,400 pt, the longest side a page can have
const file = await receipt('200in')
file.pages // 1
'invalid-password'

A password has a character PDF encryption can't take.

PDF 2.0's AES-256 encryption prepares each password with SASLprep (RFC 4013) before hashing it, so every reader turns the same password into the same key. SASLprep forbids control characters, a tab or a newline among them, private-use characters, noncharacters and a few others, and a password with right-to-left letters must start and end with one and hold no left-to-right letters. Either rule broken in security.password or security.ownerPassword throws this.

It is thrown as the file is encrypted, after the layout, so only by pdf.render and pdf.download: the preview doesn't encrypt anything.

What SASLprep changes without failing: a no-break space or another wide space becomes a plain space, soft hyphens and other invisible characters go, and the password is NFKC-normalized, so full-width letters become plain ones. Only its first 127 bytes of UTF-8 count. A plain ASCII password is used as it is.

To fix it, refuse those characters where the user chooses the password, or strip them before you pass it.

import pdf, { PdfError } from 'daeepdf'

const password = 'summer\t2026' // a tab, pasted in with the password
const error = await pdf.render('<p>Payslip</p>', { security: { password } }).catch((e: unknown) => e)
error instanceof PdfError && error.code // 'invalid-password'

// Without control characters it encrypts
const file = await pdf.render('<p>Payslip</p>', { security: { password: password.replace(/\p{Cc}/gu, '') } })
'container-detached'

The preview's container isn't in the document.

pdf.preview shows each page in an iframe inside container, and an iframe outside the document never loads, so the container must be in the page when the preview adds its pages. That is checked after the template has been laid out, not when the call starts, so a container attached in the meantime works. A container that isn't an element at all is 'invalid-option'.

To fix it, call pdf.preview once the container is in the page; in a framework, after the component has mounted.

import pdf, { PdfError } from 'daeepdf'

const panel = document.createElement('div') // not in the page yet
const error = await pdf.preview('<p>Draft</p>', panel).catch((e: unknown) => e)
error instanceof PdfError && error.code // 'container-detached'

document.body.append(panel)
await pdf.preview('<p>Draft</p>', panel) // the page card is in the panel now

The document

Templates whose pages leave no room for content, or make too many pages. Each is thrown once the template is read or laid out, before the PDF is written, and pdf.preview throws them as an export would.

'margins-too-large'

The margins leave less than 3 pt of the page for content.

Thrown when the paper less its margins is under 3 pt wide or tall. The margins are margin when you pass it, else the template's @page margins, where a percentage is of the paper's width for the left and right and of its height for the top and bottom. It is checked once the template is read, by exports and pdf.preview alike.

The message gives the four margins and the paper in pt: "The margins (300, 300, 300, 300pt) leave no room for content on a 595.28 x 841.89pt page."

To fix it, use smaller margins or a larger size. A bare number is pt, so margin: 300 is over 4 in a side.

import pdf, { PdfError } from 'daeepdf'

// Meant as px, read as pt: 300 pt a side is more than half of A4's width
const error = await pdf.render('<p>Letter</p>', { margin: 300 }).catch((e: unknown) => e)
error instanceof PdfError && error.code // 'margins-too-large'

const file = await pdf.render('<p>Letter</p>', { margin: '300px' })
file.pages // 1
'bands-too-tall'

The header and footer leave less than 3 pt of the page for content.

Thrown when the page area inside the margins, less the tallest header and the tallest footer, is under 3 pt. The bands are measured on the first page, then on every page, and grow to the tallest, so a band too tall on one page alone throws too. Exports and pdf.preview both check it.

Just above 3 pt it isn't thrown, and each page holds a sliver of content, so a short document makes many pages. The message gives the bands' height and the paper's.

To fix it, make the bands shorter, or the margins smaller. A band is laid out at the page area's width and is as tall as its content, so an image or a margin in it adds to its height.

import pdf, { PdfError } from 'daeepdf'

// A full-width banner at the top and the bottom of a landscape A5 page
const banner = '<img src="/images/photo.jpg" style="width: 100%">'
const flyer = { size: 'A5', landscape: true } as const
const both = { ...flyer, header: banner, footer: banner }
const error = await pdf.render('<p>Flyer</p>', both).catch((e: unknown) => e)
error instanceof PdfError && error.code // 'bands-too-tall': each banner is over 300 pt of the page's 419.5

// The banner as the header alone leaves room
const file = await pdf.render('<p>Flyer</p>', { ...flyer, header: banner })
file.pages // 1
'too-many-pages'

The document would have more than 10,000 pages.

Thrown as soon as the layout shows the page count is over 10,000, before anything is drawn, by exports and pdf.preview alike. Content an overflow box clips adds no pages.

A runaway template is the usual cause: a tiny size, a huge height, or a section repeated in a loop. The limit keeps one from making an export without end.

To fix it, find what multiplies the pages. A real document over 10,000 pages has to be split into several exports.

import pdf, { PdfError } from 'daeepdf'

// An inch-square page and content 10,001 in tall
const html = '<div style="height: 10001in"></div>'
const error = await pdf.render(html, { size: { width: '1in', height: '1in' } }).catch((e: unknown) => e)
error instanceof PdfError && error.code // 'too-many-pages'
// with the message '[daeepdf] The document would have 10001 pages – more than the 10000 allowed.'

Standards

Options that conflict with pdfA or pdfUA. Both are checked before any work starts, so they fail at once. The preview ignores both options and never throws these.

'missing-title'

pdfUA was set without a title.

PDF/UA requires the document to have a title, which a viewer shows in its title bar. Thrown when pdfUA is true and title is missing, empty or only spaces, before any work starts. The template's own <title> doesn't count: the title comes from the option alone.

The preview ignores pdfUA, so pdf.preview never throws this.

To fix it, pass title with pdfUA.

import pdf, { PdfError } from 'daeepdf'

const html = '<html lang="en"><title>Annual report</title><p>Annual report</p></html>'
const error = await pdf.render(html, { pdfUA: true }).catch((e: unknown) => e)
error instanceof PdfError && error.code // 'missing-title': the <title> element doesn't count

const file = await pdf.render(html, { pdfUA: true, title: 'Annual report' })
'pdfa-encryption'

pdfA was set together with security.

PDF/A forbids encryption. Thrown when pdfA is true and security is given, before any work starts. Any security object counts, an empty one or one with only security.allow included, since each encrypts the file. The preview ignores both options, so pdf.preview never throws this.

To fix it, choose one. For a copy to archive and a protected one to hand out, make two files: one with pdfA, one with security.

import pdf, { PdfError } from 'daeepdf'

const html = '<p>Contract</p>'
const error = await pdf.render(html, { pdfA: true, security: { allow: ['print'] } }).catch((e: unknown) => e)
error instanceof PdfError && error.code // 'pdfa-encryption'

const archived = await pdf.render(html, { pdfA: true })
const handedOut = await pdf.render(html, { security: { allow: ['print'] } })

Control

Errors you ask for: strict turns warnings into an error, and signal cancels an export. pdf.preview accepts both options and ignores them.

'strict'

With strict on, the export finished with warnings, and throws instead of returning the file.

The export runs to the end first, so PdfError.warnings holds every warning it had, the list PdfFile.warnings would have held, and the message joins their messages. The file is dropped. pdf.preview ignores strict.

Use it where a document must print exactly as written: in tests, or before a contract goes out.

To fix it, act on each warning by its code. Or leave strict off and read PdfFile.warnings yourself, to decide which ones matter.

import pdf, { PdfError } from 'daeepdf'

// Text with no font declared, so it would be left out
const html = '<p>Contract</p>'
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']
}
'aborted'

The signal aborted, and the export or preview stopped.

An export checks its signal between its steps, from loading fonts through each layout, header, footer and painting pass, and stops at the first check after it aborts; a wait for files ends at once. A signal already aborted stops it before it starts. PdfError.cause is the signal's reason: an AbortError DOMException unless abort() was given one, a TimeoutError from AbortSignal.timeout(). PdfError.warnings holds the warnings up to that point.

Once the file is made, an abort changes nothing. An error thrown for another reason while the signal is aborted comes out as this too. pdf.preview stops at its signal as well, before its new pages are swapped in, so the container keeps the pages it showed; its error holds no warnings.

Nothing needs fixing: it is the user's choice. Catch it and return without showing an error.

import pdf, { PdfError } from 'daeepdf'

const controller = new AbortController()
cancel.onclick = () => controller.abort()

const making = pdf.render('<p>Report</p>', { signal: controller.signal })
try {
  const file = await making
  file.download('report')
} catch (e) {
  if (!(e instanceof PdfError) || e.code !== 'aborted') throw e
  e.cause // the signal's reason, a DOMException named 'AbortError'
}

daeepdf itself

Failures that aren't the template's: the engine file not loading, and a fault in daeepdf. No template causes them, so the handling is the same for every document: tell the user, log the error, and try again later or report it.

'engine-not-loaded'

The engine, daegun.wasm, couldn't be loaded.

daeepdf's engine is a WebAssembly file, daegun.wasm, fetched from beside daeepdf's own module the first time it is needed. pdf.render, pdf.download and pdf.warmup throw this when that fails: the file isn't served there, as when a bundler didn't copy it, the server sends it without the application/wasm type, a Content Security Policy forbids WebAssembly, or the network fails. PdfError.cause is the browser's own error. pdf.preview doesn't use the engine, so it never throws this.

A failed load isn't remembered: the next call fetches the file again, so a retry after a network failure can work.

No template causes it, so the example makes the fetch fail in a hidden line, as a server that doesn't serve the file would.

To fix it, serve daegun.wasm from the same folder as daeepdf's index.js, as application/wasm (see Installation). Call pdf.warmup early to find out before anyone exports.

import pdf, { PdfError } from 'daeepdf'

// Load the engine when the app starts, so a broken install shows at once
try {
  await pdf.warmup()
} catch (e) {
  if (!(e instanceof PdfError) || e.code !== 'engine-not-loaded') throw e
  e.cause // the browser's error, here TypeError: Failed to fetch
}

await pdf.warmup() // a later call tries again
'internal'

A fault in daeepdf itself, with the original error as its cause.

Every error daeepdf raises on purpose has one of the other codes. Anything else thrown inside a call, a bug in daeepdf or a browser API failing under it, comes out as this: its message is the original one after [daeepdf] , and PdfError.cause is the original error, so a catch only ever sees a PdfError. An engine that failed is dropped, and the next export starts a fresh one.

One thing passes through unwrapped: whatever your own header or footer function throws, an error object, a string or a number, reaches you as it is.

A template shouldn't be able to cause it, so the example makes one the only way a caller can: with an option whose getter throws, which daeepdf doesn't expect. If a template does cause one, report it to daeepdf with the template and PdfError.cause.

import pdf, { PdfError } from 'daeepdf'

// A getter that throws stands in for a fault inside daeepdf
const options = { get title(): string { throw new TypeError('no title today') } }
try {
  await pdf.render('<p>Report</p>', options)
} catch (e) {
  if (!(e instanceof PdfError) || e.code !== 'internal') throw e
  e.message // '[daeepdf] no title today'
  e.cause instanceof TypeError // true: the original error
}

// A header's own error isn't wrapped
const header = () => { throw new RangeError('no page numbers') }
const error = await pdf.render('<p>Report</p>', { header }).catch((e: unknown) => e)
error instanceof RangeError // true

Type to search.