One annotation format for Vue, React, Svelte and the PDF
Inkbar is an open-source PDF editor for web apps. It ships components for Vue, React and Svelte, plus a plain TypeScript API, and all four run on one core. Supporting four ways to build a UI meant deciding early what an annotation is, because every one of them has to hold the same thing.
What the other viewers hand you
The PDF SDKs I looked at give every framework the same imperative object. You load it into a ref, call methods on it and listen for events. The annotations live inside that object, and you ask it for them when you want to save. In React that fights the way you write everything else, and in Vue and Svelte it skips the reactivity you picked them for.
I wanted an annotation to be data your app already knows how to handle. In React you hold the list in useState and pass it with onAnnotationsChange. In Vue you bind it with v-model, and in Svelte with bind:annotations. Leave the binding off and Inkbar keeps the list itself, with undo and redo. Either way the items in the list are plain objects you can log, diff or send to a server.
{
type: 'highlight',
id: 'h1',
page: 0,
rect: { x: 72, y: 700, width: 120, height: 14 },
quads: [{ p1: { x: 72, y: 714 }, p2: { x: 192, y: 714 }, p3: { x: 72, y: 700 }, p4: { x: 192, y: 700 } }],
color: '#ffd400',
}That object is the same whichever framework drew it, because all four packages hand you the core's list as it is. Moving an app from Vue to React leaves its saved annotations alone.
The schema that drifted
Inkbar's core defines those objects with zod, and the export came with a JSON Schema generated from it. The docs site didn't. It had a hand-written reference page and a hand-copied schema, and the copy soon lacked three annotation types and a round of field changes.
Fixing the docs raised a bigger question. If your backend accepts annotations from Inkbar, you shouldn't need Inkbar to read the format. So I took it out of Inkbar and gave it a name: Gloss, after the notes scribes wrote in the margins of manuscripts.
What Gloss is
Gloss is a JSON format for PDF annotations with a JSON Schema at a stable URL. It follows XFDF, the ISO standard XML format for annotations, so it has the same types and the same coordinates. Where XFDF has a name for a field, Gloss uses it, and the reference lists the XFDF name for every field that has one.
A few rules keep it useful between systems:
- Coordinates are PDF points, from the bottom-left of the page, before rotation. These are the numbers in the PDF file, so a mark keeps its values at any zoom.
- Readers keep what they don't know. No definition rejects an unknown field, and an item with an unknown
typestill counts as an annotation, so a file from a newer writer survives an older reader. - Your data goes in
custom, under your product's name. Gloss holds only what a PDF annotation can store. - Every definition is linkable. You can
$refhttps://gloss-format.org/schema/v1.json#/$defs/Redactfrom your own JSON Schema or OpenAPI document and tighten it there.
The gloss-format package on npm ships the schema, with a zod schema and a TypeScript type for every definition. The reference pages on gloss-format.org are generated from the same source on every release, so they can't fall behind.
Inkbar as one reader among many
Inkbar now depends on gloss-format like any other consumer. Its in-memory model extends the Gloss types with viewer state, such as whether a mark came from the file and is locked, and exportAnnotations('json') drops that state again on the way out.
The third rule earned its place when Inkbar's own features didn't fit. Gloss has no loupe or mask, so Inkbar writes a loupe as a circle with custom.inkbar.type set to loupe. Another Gloss reader draws a plain circle in the right place, and Inkbar reads its loupe back.
The same objects go into the file. @inkbar/engine is a WebAssembly build of PDFium, and its write API takes Gloss annotations and turns each one into a PDF annotation dictionary. Applying redactions takes Gloss redact marks too. So one shape of data runs from a component in your framework, through your own state and backend, into the saved PDF, and back out when the file is reopened.
Try it
The Inkbar sandbox runs the viewer in Vue, React, Svelte and plain TypeScript, and its form scenarios show the field values as Gloss while you type. To use Gloss without Inkbar:
npm install gloss-formatThe format, the schema and the reference are at gloss-format.org. Inkbar's docs are at inkbar.dev.