These docs describe changes that aren’t released yet. For the newest release, see the released docs.
Document
An open PDF: read it, edit it with ops (atomic transactions with undo), save it. It holds
WebAssembly memory: free it with using or Document.free (an alias).
Example
Section titled “Example”await init();using doc = Document.open(bytes);doc.transact((tx) => tx.setInfo({ title: "Final" }));const updated = doc.save({ mode: "incremental" });Extends
Section titled “Extends”Handle<BaseGlue>
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”LaminaPdfError 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”LaminaPdfError 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”LaminaPdfError reading errors.
Returns
Section titled “Returns”readonly PageInfoJs[]
warnings
Section titled “warnings”Get Signature
Section titled “Get Signature”get warnings(): readonly
LaminaPdfWarning[]
Every warning so far (build warnings first), each the ADR 0026 §1 object.
Returns
Section titled “Returns”readonly LaminaPdfWarning[]
Methods
Section titled “Methods”[dispose]()
Section titled “[dispose]()”[dispose]():
void
Frees the handle at the end of a using block (an alias of free()).
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”Handle.[dispose]
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”LaminaPdfError 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”LaminaPdfError 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.
extract()
Section titled “extract()”extract<
F>(options):ExtractResult[F]
The document’s text for retrieval and LLMs: Markdown or plain text in reading order, the
laminapdf.extract/1 JSON with a box for every block, or chunks sized for retrieval. Tables
drawn with lines come out as tables, and text a reader can’t see is left out. Needs a
document opened while the extract or text module was loaded.
Type Parameters
Section titled “Type Parameters”F extends ExtractFormat
Parameters
Section titled “Parameters”options
Section titled “options”Returns
Section titled “Returns”a string for "markdown" and "text", an Extraction for "json",
Chunks for "chunks".
Example
Section titled “Example”await init({ extract: true });const doc = Document.open(bytes);const markdown = doc.extract({ format: "markdown" });const chunks = doc.extract({ format: "chunks", chunk: { maxTokens: 400 } });Throws
Section titled “Throws”LaminaPdfError extract-module-not-loaded for a document opened in the base
module; page-index-out-of-range; unknown-option (with didYouMean) / invalid-argument
for bad options; reading errors (data-pending for a range document: use ensure).
free()
Section titled “free()”free():
void
Frees the handle’s memory now; without it, the garbage collector frees it later. A second
call does nothing, and any other call afterwards throws disposed. Prefer using, which
calls this at the end of the block.
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”Handle.free
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”LaminaPdfError 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”LaminaPdfError 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”LaminaPdfError 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”LaminaPdfError 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”LaminaPdfError as Document.undo.
save()
Section titled “save()”save(
options?):Uint8Array<ArrayBuffer>
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<ArrayBuffer>
the file’s bytes.
Throws
Section titled “Throws”LaminaPdfError 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”LaminaPdfError 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<ArrayBuffer>
report
Section titled “report”
readonlyreport:SaveReportJs
Throws
Section titled “Throws”LaminaPdfError as Document.save.
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”LaminaPdfError 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”LaminaPdfError 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”LaminaPdfError 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”LaminaPdfError 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.