TypeScript and WASM API Reference¶
The browser-facing API lives in @fabifont/open-redact-pdf.
Initialization¶
initWasm(): Promise<void>¶
Loads the generated wasm module. Call this once before opening PDFs.
Document handle¶
openPdf(input: Uint8Array): PdfHandle¶
Parses an input PDF and returns an opaque handle used by the rest of the API.
freePdf(handle: PdfHandle): void¶
Releases the memory held by a document handle. Call this before replacing a handle (e.g., when reopening a different file) to avoid leaking wasm heap memory.
getPageCount(handle: PdfHandle): number¶
Returns the page count for the parsed PDF.
getPageSize(handle: PdfHandle, pageIndex: number): PageSize¶
Returns the normalized page-space size.
Text inspection¶
extractText(handle: PdfHandle, pageIndex: number): PageText¶
Returns extracted text and geometry for the requested page.
searchText(handle: PdfHandle, pageIndex: number, query: string): TextMatch[]¶
Searches text in visual glyph order and returns match geometry as quad arrays.
Redaction¶
applyRedactions(handle: PdfHandle, plan: RedactionPlan): ApplyReport¶
Applies redactions in place to the opened handle.
savePdf(handle: PdfHandle): Uint8Array¶
Serializes the sanitized PDF as a new byte array.
Canonical types¶
RectTarget¶
type RectTarget = {
kind: "rect"
pageIndex: number
x: number
y: number
width: number
height: number
}
QuadTarget¶
QuadGroupTarget¶
type QuadGroupTarget = {
kind: "quadGroup"
pageIndex: number
quads: Array<[Point, Point, Point, Point]>
}
RedactionMode¶
Controls the visual and structural output of text redaction:
"strip"— physically removes the targeted bytes; surrounding text shifts to fill the gap. No overlay is painted."redact"— replaces targeted text with blank space and paints a colored overlay over the region. (default)"erase"— replaces targeted text with blank space but paints no overlay, leaving a visible gap.
RedactionPlan¶
type RedactionPlan = {
targets: RedactionTarget[]
mode?: RedactionMode
fillColor?: { r: number; g: number; b: number }
overlayText?: string | null
removeIntersectingAnnotations?: boolean
stripMetadata?: boolean
stripAttachments?: boolean
}
TextMatch¶
Example¶
import {
initWasm,
openPdf,
freePdf,
searchText,
applyRedactions,
savePdf,
} from "@fabifont/open-redact-pdf"
await initWasm()
const handle = openPdf(bytes)
const matches = searchText(handle, 0, "account")
const targets = matches.map((match) => ({
kind: "quadGroup" as const,
pageIndex: match.pageIndex,
quads: match.quads,
}))
applyRedactions(handle, {
targets,
mode: "redact",
stripMetadata: true,
stripAttachments: true,
})
const sanitized = savePdf(handle)