Calls
6 calls on pdf, daeepdf's default export, in daeepdf 1.2.0, as its declarations give them. Each says what it does, what comes back and what it fails with, with an example that was type-checked and run in Chrome against 1.2.0.
Every failure is a PdfError with a code to branch on, and comes back through the call's promise.
Making a PDF
pdf.render and pdf.download lay a template out and write it as a PDF, and both resolve to a PdfFile. The template is HTML in a string, a whole document or a fragment, with its CSS in <style> blocks and an @font-face for each font, since text prints only in a font the template declares. Every option is checked before any work starts, and every failure is a PdfError with a code.
pdf.renderpdf.render(html: string, options?: PdfOptions): Promise<PdfFile>Lays the HTML out and writes it as a PDF, resolving to the file instead of saving it.
It resolves to a frozen
PdfFile: the bytes, the page count, and a warning for anything the export left out or drew another way. Nothing is saved or shown, so handPdfFile.blobto an upload or an<iframe>, or callPdfFile.downloadlater. With no options and no@pagerule the page is A4 portrait without margins, and the file is tagged for screen readers and unencrypted.The HTML is a string, a whole document or a fragment. It is parsed and sanitized, never run: scripts, event handlers,
<link>stylesheets and@importare removed, so its CSS goes in<style>blocks. It lays out in a hidden frame the size of the page, so@media,vwandvhanswer to the page, and your app's stylesheets apply unlessuseAppStylesisfalse. Text prints only in a font the template declares with@font-face. Text in any other font, the browser's default included, is left out with a'font-not-declared'warning unless you setfallbackFont.The engine,
daegun.wasm, loads on the first export and stays loaded;pdf.warmuploads it sooner.Every option is checked before any work starts: HTML that isn't a string, or an option misspelled or of the wrong type, fails with
'invalid-option', and a misspelling's message names the closest option.'invalid-page-size','margins-too-large','bands-too-tall'and'too-many-pages'come from the page and what fits on it;'missing-title','pdfa-encryption'and'invalid-password'from the standards andsecurity;'strict'and'aborted'fromstrictandsignal; and'engine-not-loaded'from adaegun.wasmthat can't be loaded. Anything else is'internal', a fault in daeepdf, with the original error as itscause. An error object your ownheaderorfooterfunction throws reaches you as it is, while a thrown string or number comes back as'internal'.import pdf from 'daeepdf' const html = `<!doctype html> <html lang="en"> <head> <style> @font-face { font-family: Inter; src: url('/fonts/inter-var.ttf'); } body { font: 11pt Inter; } </style> </head> <body><h1>Invoice 42</h1><p>Total: $120.00</p></body> </html>` const file = await pdf.render(html, { size: 'Letter', margin: '0.75in', title: 'Invoice 42' }) file.pages // 1 file.warnings // [], so everything printed // Show it in the page, or send it to your server iframe.src = URL.createObjectURL(file.blob())pdf.downloadpdf.download(html: string, filename: string, options?: PdfOptions): Promise<PdfFile>Renders the HTML as
pdf.renderdoes, then saves the file under the name you give.It resolves to the same
PdfFileonce it has handed the file to the browser to save, so you can still readPdfFile.warningsor uploadPdfFile.bytes. Saving is the browser's: daeepdf clicks a link to the file, and where it goes, or whether the user is asked, is up to the browser. On iOS and iPadOS the file is offered asapplication/octet-streaminstead ofapplication/pdf.The name is trimmed, and
.pdfis added unless it already ends in.pdf, in any case:'Invoice 42'saves asInvoice 42.pdf, and'report.PDF'as it is. Nothing else about it changes, so run a name built from user input throughpdf.safeNamefirst.It fails as
pdf.renderdoes, and with'invalid-option'for a name that isn't a string, is empty or is only spaces. The name is checked first, before the options and before anything is rendered.import pdf, { PdfError } from 'daeepdf' const customer = 'Acme Studio / Berlin' const file = await pdf.download(html, `Invoice ${pdf.safeName(customer)}`, { size: 'A4' }) // saved as Invoice Acme_Studio__Berlin.pdf file.pages // 1 const blank = await pdf.download(html, ' ').catch((e: PdfError) => e.code) blank // 'invalid-option', and nothing was rendered or saved
Previewing
pdf.preview shows the pages a template makes as cards in an element of your page, laid out as the export lays them out. It takes the same PdfOptions, so one object drives both the preview and the file, and it never loads the engine.
pdf.previewpdf.preview(html: string, container: HTMLElement, options?: PdfOptions): Promise<void>Shows the pages a template makes as page cards inside an element of your page.
Each page is a white card the paper's size in CSS px, 793.7 × 1122.5 px for A4, stacked down
containerwith 24 px between them. The cards are laid out as the export lays out its pages, with the same page breaks, margins, header and footer, and content wider than the page shrunk onto it, so the preview shows page for page whatpdf.renderwrites. Scale it down with CSSzoomon the container or one of its ancestors. Give it an element of its own: anything else incontainerbut a<style>is removed.It takes the same
PdfOptionsas an export, so one object can drive both.size,landscape,margin,shrinkToFit,header,footer,fallbackFont,useAppStyles,loadTimeoutandsignalapply: an abortedsignalstops the preview with'aborted'before its pages are swapped in, so the container keeps the pages it had. The rest concern only the file, and are checked but do nothing here: a preview withpdfUAand notitle, or withstrictand a warning, still renders.The promise resolves once the new pages are on screen, all swapped in at once, so a preview updated as someone types doesn't flicker. A container renders one preview at a time: a call made while one runs waits, and of the calls waiting only the newest renders. A call a newer one replaces resolves without rendering once the render before it is done, so calling it on every change renders only as often as it can keep up.
Past 10 pages, only the cards in or near the window hold their page, and the rest fill in as they come near it, since each card holds a copy of the whole layout.
It fails with
'invalid-option'for an option or argument of the wrong kind, with'container-detached'whencontainerisn't in the document, with'aborted'whensignalaborts, and with the page errors an export would meet:'invalid-page-size','margins-too-large','bands-too-tall'and'too-many-pages'. It never loads the engine, so it doesn't fail with'engine-not-loaded'.import pdf from 'daeepdf' import type { PdfOptions } from 'daeepdf' const options: PdfOptions = { size: 'A4', margin: '20mm', footer: ({ page, pages }) => `<p style="font: 9pt Inter; text-align: center">${page} / ${pages}</p>`, } await pdf.preview(invoice('$120.00'), container, options) container.children.length // 1, a card for each page // The same template and options make the file input.addEventListener('input', () => pdf.preview(invoice(input.value), container, options)) exportButton.addEventListener('click', () => pdf.download(invoice(input.value), 'invoice-42', options))
Helpers
pdf.warmup loads the engine before the first export needs it. pdf.escape is for every value you put into a template, and pdf.safeName for a file name built from one.
pdf.warmuppdf.warmup(): Promise<void>Loads the engine ahead of the first export, so that export doesn't wait for it.
daeepdf's engine is
daegun.wasm, about 3 MB, or 1.2 MB compressed, served beside daeepdf's module.pdf.renderandpdf.downloadload it themselves on first use, which puts the fetch and compile on the first export. Callpdf.warmupwhen the page loads, or when the screen with the export button opens, and that export starts at once.pdf.previewdoesn't need the engine.It resolves to nothing once the engine is ready. Calls made while it loads, from here or from an export, share that one load, and once it is loaded a call resolves at once.
It fails with
'engine-not-loaded'whendaegun.wasmcan't be fetched or compiled, usually because it isn't served beside the module, or not asapplication/wasm; the original error is thecause. A failed load isn't kept, so the next call tries again.import pdf from 'daeepdf' exportButton.disabled = true await pdf.warmup() // resolves once daegun.wasm is loaded exportButton.disabled = false- Fails with:
'engine-not-loaded'.
- Fails with:
pdf.escapepdf.escape(text: string): stringEscapes text for HTML, so a value from a user prints as the text it is.
It replaces
&,<,>,"and'with their entities,&first, so the result is safe as an element's text and inside an attribute value quoted either way. Nothing else changes, and text already escaped is escaped again:&becomes&amp;. A value that isn't a string is turned into one withStringfirst.Run every value from a user, a database or an API through it before it goes into a template. daeepdf sanitizes the template, removing scripts and script URLs, but only escaping keeps a name like
<b>Bob</b>from being read as markup. It isn't enough inside a<style>block, a URL or an unquoted attribute: build those only from values you have checked.import pdf from 'daeepdf' const name = `O'Brien & Sons <b>"Ltd"</b>` pdf.escape(name) // 'O'Brien & Sons <b>"Ltd"</b>' const html = `<!doctype html><html><head><style> @font-face { font-family: Inter; src: url('/fonts/inter-var.ttf'); } body { font: 12pt Inter; } </style></head><body><p title="${pdf.escape(name)}">Bill to ${pdf.escape(name)}</p></body></html>` const file = await pdf.render(html) // prints Bill to O'Brien & Sons <b>"Ltd"</b>, the tags as textpdf.safeNamepdf.safeName(text: string): stringTurns any text into a name that is safe to save a file under.
It trims the text, turns each run of whitespace into one
_, and removes every character that isn't a letter, a digit, a combining mark,_or-. Letters and digits of every script stay, so'田中太郎'and'José'come back as they are. Everything else goes: path separators, quotes,#, emoji, and dots too, so pass the name without its extension and letpdf.downloadadd.pdf.The result can be empty, for text that is all punctuation, and
pdf.downloadfails with'invalid-option'for an empty name, so give it a fallback. There is no length limit. Anything that isn't a string is read as one first, aspdf.escapereads it, so42gives'42'.import pdf from 'daeepdf' pdf.safeName('Acme Studio / 2026') // 'Acme_Studio__2026' pdf.safeName('Rechnung #0042') // 'Rechnung_0042' pdf.safeName(' 田中 太郎 ') // '田中_太郎' pdf.safeName('report.pdf') // 'reportpdf': the dot goes too pdf.safeName('???') || 'document' // 'document'