Rasura

Rust, compiled to WebAssembly

Edit the text in a PDF.Without regenerating the file.

A PDF holds no paragraphs and no words. It holds instructions to draw glyph 36 here and glyph 82 four units to the right. Rasura rebuilds the sentence from those positions, changes it, and patches the file in place. Every byte it did not need to touch is returned exactly as it arrived.

npm install rasura·cargo add rasura

board-report.pdfpage 1
BT
/F1 11 Tf
72 709.8 Td
[(Prepar)18(ed f)-4(or the)] TJ
13.2 TL T*
[(boar)11(d, and f)-4(or any-)] TJ
T*
[(one curious about)] TJ
ET

Paragraph 1

Prepared for the board, and for anyone curious about what a PDF editor can actually see.

font
Helvetica
size
11 pt
lines
3
coverage
complete

replaceText(0..8, 'Written')

fidelity: exact· +1,204 bytes

the same page, before and after it is understood

1,030

real PDFs checked on every build

1,196

Rust tests, plus 41 in JavaScript

8

invariants held across the corpus

0

lines of unsafe code

Three rules

Everything in the library follows from these, in this order.

01

Non-locality is forbidden

An edit on page 40 changes no other page by a pixel, and alters the bytes of no object it did not need to touch. An unedited save returns the input byte for byte.

02

Fidelity is reported, never assumed

Operations return the rung they reached instead of throwing. Set a floor and anything below it is refused rather than quietly degraded.

03

The file stays a valid PDF

Output passes qpdf --check and opens in Acrobat, Preview, Chrome and Firefox with no repair prompt.

RungWhat it means
exactThe glyphs were already in the embedded font. Nothing was approximated.
reembeddedA glyph was injected into the document's own font from a typeface you supplied.
substitutedA different face was used. The text is right, the letterforms are not.
overlaidOld content covered, new content drawn on top. A last resort.

Two things it does

Change a document, or make one.

Editing patches the file that exists. Composition builds one that does not, with a layout engine that decides the measure, the leading, where lines break and where pages break, and keeps a heading with the section under it.

Both embed and subset the typeface to the characters actually drawn. 515 KB of Roboto becomes 14.5 KB for the two dozen glyphs a short document uses.

Full API reference
edit.js
import { Pdf } from 'rasura'
const doc = await Pdf.open(await file.arrayBuffer())
const page = await doc.page(0)
const [first] = page.paragraphs()
first.text
// 'Prepared for the board, and for anyone curious'
// Edits are staged, so a set of them commits or rolls back together.
const session = doc.edit()
const outcome = await session.replaceText(
first.id,
{ start: 0, end: 8 },
'Written',
)
outcome.fidelity // 'exact'
const { bytes, bytesAppended } = await session.commit()
// the original file, plus 1,204 bytes

What it refuses

Each of these is a decision with a reason, not a gap in a roadmap.

Rendering

pdf.js does it well and is Apache-2.0. Pair with it for display.

Scanned documents

Editing them needs OCR and raster inpainting, which is a different product. Reported as documentKind: scanned.

XFA forms

Deprecated by ISO and Adobe-proprietary. Detected, exposed, and form edits refused.

Creating signatures

Needs key custody and carries regulatory weight. Existing ones are preserved and their invalidation reported.

Redacting under an image

Image data is not searched, so a scan of the same words would survive. It fails rather than half-succeeds.

Bold and italic when composing

One embedded face per composed document, for now. Named here rather than left to be discovered.

Open a file in the editor. Nothing you load is uploaded anywhere.

It runs the real library in your tab, so there is nowhere to upload it to.