Documentation
Creating PDFs
PDFs in Vue
PDFs in Nuxt
PDFs in React
PDFs in Angular
Output options
renderToBytes(doc, options) turns a document into bytes. Everything on this page is optional -
renderToBytes(doc) on its own already produces a valid PDF.
import { renderToBytes } from "@jasy/pdf";
const bytes = await renderToBytes(doc, {
title: "Q3 report",
accessible: true,
});
Files inside the PDF
A PDF can carry other files. That is how a ZUGFeRD invoice hides its XML inside the page a human reads, and you can use it for anything else: the CSV a table was built from, the contract a quote refers to, the source of a chart.
const bytes = await renderToBytes(doc, {
attachments: [
{
name: "figures.csv",
data: new TextEncoder().encode("month,revenue\n2026-06,42000\n"),
mimeType: "text/csv",
relationship: "Source",
description: "The numbers behind the chart on page 2",
},
],
});
| Field | Meaning |
|---|---|
name | the file name a reader shows, e.g. "figures.csv" |
data | the bytes (Uint8Array) |
mimeType | e.g. "text/csv"; default "application/octet-stream" |
relationship | /AFRelationship: "Data", "Source", "Alternative", "Supplement" |
description | free text shown next to the file |
relationship says what the file IS to the document rather than how it is stored, and a validator
checks it: ZUGFeRD requires "Data" for its factur-x.xml, because the XML is the invoice, not a
supplement to it. Attachments are written as associated files, which is what PDF/A-3 and PDF 2.0
require - a plain /EmbeddedFiles entry would not satisfy either.
Reading them back since beta.4
readAttachments lists every file a PDF carries - from any producer, not only jasy:
import { PdfDocument, readAttachments } from "@jasy/pdf/edit";
const doc = await PdfDocument.open(bytes, { password }); // password only if it is encrypted
for (const file of readAttachments(doc)) {
console.log(file.name, file.mimeType, file.relationship, file.locations);
const data = file.bytes(); // or file.text() for XML, CSV, ...
}
It finds a file wherever a PDF may keep one - the document's attachment list, the PDF/A-3 associated
files, and file annotations on a page - and locations says where each one hangs. One file listed in
two places is one entry; two files with the same name are two, so deciding what to do with a duplicate
name stays your call. bytes() hands back exactly what was embedded: decrypted, every standard filter
decoded, and checked against the file's own checksum. Data that does not match it, or a filter that
cannot be decoded, throws an AttachmentError instead of returning something close.
Building a PDF/A yourself
@jasy/e-invoice does all of this for you, and for an invoice you should let it.
These options exist because the same machinery is useful for any archival PDF - a contract, a report
that has to survive a decade:
const bytes = await renderToBytes(doc, {
pdfVersion: "1.7", // PDF/A-3 needs it; default "1.4"
documentId: true, // a trailer /ID, which PDF/A requires
outputIntent: sRGBProfileBytes, // an ICC profile, so color is defined
xmp: xmpPacket, // the XMP that DECLARES the conformance level
standardFonts: false, // no Helvetica: PDF/A wants every font embedded
fonts: { Helvetica: myEmbeddedFace },
});
Two of those are easy to get wrong:
standardFonts: falseis the point, not a detail. PDF/A forbids relying on the reader's fonts, and the standard 14 are exactly that. Turning them off means every font name you use must arrive throughfonts- which is why@jasy/e-invoiceships the Liberation faces under the namesHelvetica,TimesandCourier.xmpis what a validator reads. The conformance level lives in the XMP packet, not in the structure. Getting the structure right and the packet wrong produces a file that claims nothing.
documentId writes a deterministic /ID: it is a hash of the objects, not a random number and not
the clock. That is what lets the same document render byte-identically twice, so an archived file stays
re-derivable years later.
Typography and size
| Option | Default | What it does |
|---|---|---|
fonts | - | embedded faces, keyed by the name used in Text({ font }) |
kerning | true | space glyph pairs from the font's own data; false is the older output |
compress | true | FlateDecode the streams; false gives a greppable, readable PDF |
compress: false is a debugging tool worth knowing about: the content stream becomes plain text, so
you can open the PDF in an editor and read the operators jasy wrote.
Loading ahead since beta.7
The metrics of the standard fonts are not in the bundle you start with. They load with the first render (158 KB minified, 12 KB gzipped), and every later render finds them loaded. To take that wait off the first PDF, load them earlier - once the app is idle, or when the pointer reaches a download button:
import { preloadStandardFonts } from "@jasy/pdf";
requestIdleCallback(() => void preloadStandardFonts());
It is safe to call any number of times, and a load that failed is tried again on the next call.
Telling you what happened
const bytes = await renderToBytes(doc, {
onOverflow: "warn",
onMissingGlyphs: (chars) => report(`not drawable: ${chars.join(" ")}`),
onDiagnostic: (d) => report(`${d.code} in ${d.element} "${d.text}" on page ${d.page}`),
});
onOverflow- what to do when a single block is taller than a whole page and cannot be broken."error"(default) throws,"warn"logs,"ignore"is silent. It is clipped either way: the alternative is a pagination loop that never ends.onMissingGlyphs- called once with any characters no font could draw. They are removed rather than written as the font's.notdefbox, which PDF/A forbids. Deliberately a callback and not a policy: a missing tick mark must never stop an invoice from being produced, but your application should be able to say which character went missing.onDiagnosticsince beta.4 - one channel for everything jasy has to say about a document it rendered anyway. Each finding has a stablecode, the element as you wrote it ("Box","Text"), its first text and the page:codeMeaning page-break-ignoreda PageBreakinside aRowor aPageBuilder, which never paginatecolumn-break-ignoredsince beta.5a ColumnBreakoutside the columns of aFlowbreak-ignoredbreakBefore/breakAfteron something inside aRowor a table rowkeep-together-splita keepTogethergroup holds a forced break; the break winsoverflow-clippeda block taller than a page that cannot break (with onOverflownot"error")overflow-widesince beta.5a word that cannot break, or a Rowwhose children need more room, drawn past the edgemissing-glyphscharacters no font could draw, per Textimage-unsizedsince beta.5an image whose own size was unknown at layout - a URL first built in a PageBuilder
Without a handler nothing changes: what warned before still goes to the console.
Metadata and access
| Option | What it does |
|---|---|
title | the document title - used by readers, and required for a conformant tagged PDF |
lang | document language, e.g. "de-DE" (default "en-US") |
accessible | emit a tagged structure tree - see Accessibility |
encrypt | AES-256 password protection - see Encryption |
encrypt and PDF/A are mutually exclusive, and jasy says so rather than producing a file that fails
validation later: PDF/A forbids encryption, so encrypting a ZUGFeRD invoice throws.