mozilla/pdf.js · error · Error
doc.path is read-only
Error message
doc.path is read-only
What it means
`Doc.path` returns the device-independent path of the open document; PDF.js returns `""` because documents loaded in a browser have no filesystem path. It is read-only per the Acrobat spec — the path is a factual attribute of how the doc was opened, not something a script can change. The throwing setter enforces that contract.
Source
Thrown at src/scripting_api/doc.js:591
}
this._send({ command: "page-num", value });
this._pageNum = value;
}
get pageWindowRect() {
return [0, 0, 0, 0];
}
set pageWindowRect(_) {
throw new Error("doc.pageWindowRect is read-only");
}
get path() {
return "";
}
set path(_) {
throw new Error("doc.path is read-only");
}
get permStatusReady() {
return true;
}
set permStatusReady(_) {
throw new Error("doc.permStatusReady is read-only");
}
get producer() {
return this._producer;
}
set producer(_) {
throw new Error("doc.producer is read-only");
}
View on GitHub (pinned to 5903d58d58)
Solutions
- Remove the assignment; read `doc.path` (returns `""` in PDF.js) or `doc.URL` for the source location.
- Manage save paths at the host integration layer rather than inside the document script.
- Wrap script execution in try/catch to tolerate legacy save code.
Example fix
// before doc.path = '/tmp/saved.pdf'; // after var src = doc.URL; // read-only source; doc.path returns '' in browser
Defensive patterns
Strategy: validation
Validate before calling
const READ_ONLY_DOC_PROPS = new Set(['path','URL' /* ... */]);
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, 'path'); // true Try / catch
try {
doc.path = '/tmp/saved.pdf';
} catch (e) {
if (/\\bpath is read-only/.test(e.message)) console.warn(e.message);
else throw e;
} Prevention
- `doc.path` is a factual load attribute; manage save paths at the host layer.
- Read `doc.path`/`doc.URL` instead of writing them.
- Lint scripts for assignments to read-only path properties before dispatch.
When it happens
Trigger: A script writes `doc.path = '/some/file.pdf';`, typically to record or fake a save location. The setter throws immediately.
Common situations: Save/export scripts ported from desktop Acrobat; scripts trying to derive a working directory from `doc.path`; code that assumes a writable path field.
Related errors
- doc.mouseX is read-only
- doc.mouseY is read-only
- doc.numFields is read-only
- doc.numPages is read-only
- doc.numTemplates is read-only
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/eb451639875a7754.
Report an issue: GitHub.