Document
An open PDF: read it, edit it with ops (atomic transactions with undo), save it.
Example
Section titled “Example”await init();const doc = Document.open(bytes);doc.transact((tx) => tx.setInfo({ title: "Final" }));const updated = doc.save({ mode: "incremental" });doc.free();Accessors
Section titled “Accessors”canExtractText
Section titled “canExtractText”Get Signature
Section titled “Get Signature”get canExtractText():
boolean
Whether page(i).text() works on this document: it was opened or built while the extract or
the text module was loaded. A document opened before the module loaded stays in the base
module; save and reopen it to move it.
Returns
Section titled “Returns”boolean
hasUnsavedChanges
Section titled “hasUnsavedChanges”Get Signature
Section titled “Get Signature”get hasUnsavedChanges():
boolean
Whether there are changes no save has written yet.
Returns
Section titled “Returns”boolean
Get Signature
Section titled “Get Signature”get info():
DocumentInfoJs
The Info dictionary’s text entries (document text).
Throws
Section titled “Throws”LaminaError reading errors.
Returns
Section titled “Returns”DocumentInfoJs
pageCount
Section titled “pageCount”Get Signature
Section titled “Get Signature”get pageCount():
number
The number of pages.
Throws
Section titled “Throws”LaminaError reading errors.
Returns
Section titled “Returns”number
Get Signature
Section titled “Get Signature”get pages(): readonly
PageInfoJs[]
Every page’s facts, in order (one page-tree walk).
Throws
Section titled “Throws”LaminaError reading errors.
Returns
Section titled “Returns”readonly PageInfoJs[]
warnings
Section titled “warnings”Get Signature
Section titled “Get Signature”get warnings(): readonly
LaminaWarning[]
Every warning so far (build warnings first), each the ADR 0026 §1 object.
Returns
Section titled “Returns”readonly LaminaWarning[]
Methods
Section titled “Methods”[dispose]()
Section titled “[dispose]()”[dispose]():
void
Frees the document at the end of a using block.
Returns
Section titled “Returns”void
apply()
Section titled “apply()”apply(
ops):EditedJs
Applies ops in order as one transaction: all of them, or none.
Parameters
Section titled “Parameters”readonly OpJs[]
Returns
Section titled “Returns”EditedJs
the pages the edit touched.
Throws
Section titled “Throws”LaminaError unknown-option / invalid-argument for a value that isn’t an op;
the first failing op’s error (unknown-page, …), with nothing applied.
applyLog()
Section titled “applyLog()”applyLog(
log):EditedJs
Applies an op log ({ v: 1, ops }, as JSON text or an object) as one transaction.
Parameters
Section titled “Parameters”string | OpLog
Returns
Section titled “Returns”EditedJs
Throws
Section titled “Throws”LaminaError invalid-op-log, unsupported-version; as Document.apply.
ensure()
Section titled “ensure()”ensure<
T>(f,options?):Promise<T>
Runs a synchronous call on this document, fetching what it needs first: for a document
from Document.openRange, a data-pending makes it fetch the ranges asked for and
call f again. For other documents it just returns f().
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”() => T | Promise<T>
the call (it may run several times; keep it free of side effects). An async
function works too: its data-pending rejections are caught the same way.
options?
Section titled “options?”signal aborts this call’s fetches (the open’s signal applies to the open
only).
signal?
Section titled “signal?”AbortSignal
Returns
Section titled “Returns”Promise<T>
what f returns.
Example
Section titled “Example”const doc = await Document.openRange(httpRangeSource(url));const pages = await doc.ensure(() => doc.pages);Throws
Section titled “Throws”what f throws, and as Document.openRange.
free()
Section titled “free()”free():
void
Frees the document’s WASM memory now; without it, it is freed when garbage-collected.
Calling it twice is harmless; using the document afterwards is not (L2b adds disposed).
Returns
Section titled “Returns”void
page()
Section titled “page()”page(
index):PageRef
A handle on page index that follows the page by its ID.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”Throws
Section titled “Throws”LaminaError page-index-out-of-range.
pageById()
Section titled “pageById()”pageById(
id):PageRef
A handle on the page with id (p12.0).
Parameters
Section titled “Parameters”string
Returns
Section titled “Returns”Throws
Section titled “Throws”LaminaError invalid-argument, unknown-page.
pageInfo()
Section titled “pageInfo()”pageInfo(
index):PageInfoJs
Page index’s facts.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”PageInfoJs
Throws
Section titled “Throws”LaminaError page-index-out-of-range.
rebase()
Section titled “rebase()”rebase(
bytes):void
Rebases onto bytes, the output of a save whose rebase failed (not-rebased).
Parameters
Section titled “Parameters”Uint8Array
Returns
Section titled “Returns”void
Throws
Section titled “Throws”LaminaError no-pending-save; reading errors in bytes.
redo()
Section titled “redo()”redo():
any
Redoes the newest undone transaction.
Returns
Section titled “Returns”any
how it went, or undefined when there is nothing to redo.
Throws
Section titled “Throws”save()
Section titled “save()”save(
options?):Uint8Array
Saves and rebases onto the output (later edits and saves build on it).
Parameters
Section titled “Parameters”options?
Section titled “options?”any
mode (auto, incremental, fullRewrite), overrides, output form.
Returns
Section titled “Returns”Uint8Array
the file’s bytes.
Throws
Section titled “Throws”LaminaError would-invalidate-signatures, requires-rewrite, encrypted-base, …
saveJob()
Section titled “saveJob()”saveJob(
options?):SaveJob
A save as a job stepped by the caller (to keep a UI responsive).
Parameters
Section titled “Parameters”options?
Section titled “options?”any
Returns
Section titled “Returns”Throws
Section titled “Throws”LaminaError unknown-option / invalid-argument for bad options.
saveWithReport()
Section titled “saveWithReport()”saveWithReport(
options?):object
As Document.save, with the save’s report (mode used, bytes written, renumbering, …).
Parameters
Section titled “Parameters”options?
Section titled “options?”any
Returns
Section titled “Returns”object
readonlybytes:Uint8Array
report
Section titled “report”
readonlyreport:SaveReportJs
Throws
Section titled “Throws”transact()
Section titled “transact()”transact<
T>(edit):T
Runs edit with a transaction handle and applies what it collected as one transaction
(ADR 0006 §2). When edit throws, nothing is applied and the error propagates. The ops are
applied when edit returns: reads inside edit see the document before the transaction.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”(tx) => T
collects ops on tx; synchronous only (a callback returning a promise is
refused with invalid-argument and nothing is applied).
Returns
Section titled “Returns”T
what edit returned.
Throws
Section titled “Throws”LaminaError nested-transaction when called (or when apply, undo, redo,
save… are called) inside a transact of the same document; invalid-argument for an
async callback; as Document.apply.
Example
Section titled “Example”doc.transact((tx) => { tx.setInfo({ title: "Report" }); tx.rotatePage(doc.page(0).id, 90);});undo()
Section titled “undo()”undo():
any
Undoes the newest transaction.
Returns
Section titled “Returns”any
how it went, or undefined when there is nothing to undo.
Throws
Section titled “Throws”LaminaError saved-not-rebased; reading errors.
open()
Section titled “open()”
staticopen(bytes,options?):Document
Opens a document from its bytes (the bytes are copied into the WASM memory).
The password is tried as given. When that fails with wrong-password and its NFKC form
differs, the NFKC form is tried too: R5/R6 passwords are prepared with SASLprep, whose
normalisation the WASM modules leave out (ADR 0016 §7), while R2–R4 passwords must reach
the core unchanged.
Parameters
Section titled “Parameters”ArrayBuffer | Uint8Array<ArrayBufferLike>
the PDF file.
options?
Section titled “options?”password, strict mode, limits.
Returns
Section titled “Returns”Document
the document.
Throws
Section titled “Throws”LaminaError not-pdf, password-required, wrong-password, limit-exceeded,
strict, …; unknown-option / invalid-argument for bad options; not-initialized.
openRange()
Section titled “openRange()”
staticopenRange(source,options?):Promise<Document>
Opens a document whose bytes are fetched range by range (httpRangeSource,
callbackSource): only what the open needs is fetched (the end of the file, the xref,
the catalog). Calls on the document stay synchronous; one that needs bytes not fetched yet
throws data-pending with ranges, so wrap later reads in Document.ensure.
Parameters
Section titled “Parameters”source
Section titled “source”RangeSource | Promise<RangeSource>
the file (or a promise of it, as httpRangeSource returns).
options?
Section titled “options?”OpenOptions & RangeOptions = {}
open options (password, strict, limits) and fetch options.
Returns
Section titled “Returns”Promise<Document>
the document, once open has everything it needs.
Throws
Section titled “Throws”LaminaError as Document.open; data-pending when fetching made no
progress or maxRounds ran out; source-budget-exceeded; the source’s own errors
(fetch-failed, source-changed); the signal’s reason when aborted.