mozilla/pdf.js · error · Error
doc.viewState is read-only
Error message
doc.viewState is read-only
What it means
`Doc.viewState` returns the document's view state (used to capture/restore the viewing position); PDF.js returns `undefined` because it does not expose a serializable view-state object to the scripting sandbox. It is read-only per the Acrobat spec — view state is observed, not assigned through this property (restoration uses dedicated APIs). The throwing setter enforces that contract.
Source
Thrown at src/scripting_api/doc.js:695
set title(_) {
throw new Error("doc.title is read-only");
}
get URL() {
return this._URL;
}
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");
}
View on GitHub (pinned to 5903d58d58)
Solutions
- Remove the assignment; read the value (`var v = doc.viewState;`, `undefined` in PDF.js).
- Implement view capture/restore at the host viewer layer (scroll/zoom/page state) instead of via this property.
- Run legacy scripts through try/catch.
Example fix
// before
doc.viewState = { page: 1, zoom: 1.5 };
// after
var v = doc.viewState; // read-only; undefined in PDF.js Defensive patterns
Strategy: try-catch
Validate before calling
const READ_ONLY_DOC_PROPS = new Set(['viewState','pageWindowRect' /* ... */]);
function assertsNoReadOnlyWrite(scriptSrc) {
for (const p of READ_ONLY_DOC_PROPS) {
if (new RegExp(`\\bdoc\\.${p}\\s*=[^=]`).test(scriptSrc)) {
throw new Error(`Script writes read-only property doc.${p}`);
}
}
}
assertsNoReadOnlyWrite(myScript); Type guard
function isReadOnlyDocProp(doc, prop) {
const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(doc), prop)
|| Object.getOwnPropertyDescriptor(doc, prop);
return !!desc && !!desc.get && !desc.set;
}
isReadOnlyDocProp(doc, 'viewState'); // true Try / catch
try {
doc.viewState = { page: 1, zoom: 1.5 };
} catch (e) {
if (/viewState is read-only/.test(e.message)) console.warn(e.message);
else throw e;
} Prevention
- View state is not serialized to the scripting sandbox; capture/restore it at the viewer layer.
- Lint scripts for assignments to read-only view/window properties.
- Wrap layout-restore scripts in try/catch.
When it happens
Trigger: A script writes `doc.viewState = {...};`, attempting to capture or force a view state. The setter throws on assignment.
Common situations: Layout-restore scripts ported from Acrobat; code that snapshots view state on close; tutorials that demonstrate unsupported writes.
Related errors
- doc.numTemplates is read-only
- doc.outerAppWindowRect is read-only
- doc.outerDocWindowRect is read-only
- doc.pageWindowRect is read-only
- doc.requiresFullSave is read-only
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/a412ebe2f67d6c90.
Report an issue: GitHub.