mozilla/pdf.js · error · Error
doc.xfa is read-only
Error message
doc.xfa is read-only
What it means
Thrown by the setter of the `xfa` property on the Acrobat JavaScript `Doc` object in PDF.js's scripting sandbox (src/scripting_api/doc.js:702). In the Acrobat API, `doc.xfa` exposes the underlying XFA (XML Forms Architecture) DOM node; PDF.js treats it as an intrinsic, non-writable attribute and throws on any assignment. The getter returns `this._xfa` (the XFA object parsed by the core layer, or undefined for non-XFA documents).
Source
Thrown at src/scripting_api/doc.js:703
set URL(_) {
throw new Error("doc.URL is read-only");
}
get viewState() {
return undefined;
}
set viewState(_) {
throw new Error("doc.viewState is read-only");
}
get xfa() {
return this._xfa;
}
set xfa(_) {
throw new Error("doc.xfa is read-only");
}
get XFAForeground() {
return false;
}
set XFAForeground(_) {
throw new Error("doc.XFAForeground is read-only");
}
get zoomType() {
return this._zoomType;
}
set zoomType(type) {
if (!this._userActivation) {
return;
}View on GitHub (pinned to 5903d58d58)
Solutions
- Remove the assignment to `doc.xfa` — it is read-only by design; mutate the XFA tree through its own methods instead of replacing the reference.
- If a generic routine is assigning, skip read-only members by name before writing (guard with a known-readonly list).
- Wrap the assignment in try/catch if the write is best-effort, so a single read-only field does not abort the whole script.
Example fix
// before this.doc.xfa = newXfa; // after // do not reassign; xfa is read-only. Read it only: const xfa = this.doc.xfa;
Defensive patterns
Strategy: try-catch
Validate before calling
// These Doc properties are intrinsic and never writable in PDF.js.
const READ_ONLY_DOC_PROPS = new Set(['URL','viewState','xfa','XFAForeground']);
function safeAssignDoc(doc, key, value) {
if (READ_ONLY_DOC_PROPS.has(key)) return false; // skip
doc[key] = value;
return true;
} Try / catch
try {
doc.xfa = candidate;
} catch (e) {
// xfa is read-only in PDF.js; proceed without the assignment
} Prevention
- Never assign to xfa/XFAForeground/URL/viewState; read them only.
- In generic copy/restore code, maintain a read-only property blocklist and skip those keys.
- When porting Acrobat scripts, replace XFA-tree swaps with mutation of the existing XFA nodes.
When it happens
Trigger: A script embedded in a PDF assigns to `doc.xfa` (or `this.doc.xfa`), e.g. `this.doc.xfa = newXfa;`, or generic form-filler code that copies whole objects via `Object.assign(doc, {...})` which hits the setter. Also triggered by libraries that try to 'reset' a form by reassigning every enumerable-looking property.
Common situations: XFA form migration where an author assumed the XFA tree could be swapped at runtime; porting an Acrobat script from a reader that silently ignored the assignment; an aggressive form-reset routine that blindly writes known property names.
Related errors
- doc.XFAForeground is read-only
- app.activeDocs is read-only
- app.constants is read-only
- app.formsVersion is read-only
- app.fromPDFConverters is read-only
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/a8bbdcb8177bc7b8.
Report an issue: GitHub.