mozilla/pdf.js · error · Error
doc.numTemplates is read-only
Error message
doc.numTemplates is read-only
What it means
`Doc.numTemplates` returns the number of page templates in the document; PDF.js always returns 0 because the page-template feature is not implemented. It is read-only per the Acrobat spec — templates are managed via `createTemplate`/`removeTemplate`, not by setting a count. The throwing setter keeps PDF.js behavior aligned with Acrobat's contract.
Source
Thrown at src/scripting_api/doc.js:542
set numFields(_) {
throw new Error("doc.numFields is read-only");
}
get numPages() {
return this._numPages;
}
set numPages(_) {
throw new Error("doc.numPages is read-only");
}
get numTemplates() {
return 0;
}
set numTemplates(_) {
throw new Error("doc.numTemplates is read-only");
}
get outerAppWindowRect() {
return [0, 0, 0, 0];
}
set outerAppWindowRect(_) {
throw new Error("doc.outerAppWindowRect is read-only");
}
get outerDocWindowRect() {
return [0, 0, 0, 0];
}
set outerDocWindowRect(_) {
throw new Error("doc.outerDocWindowRect is read-only");
}
View on GitHub (pinned to 5903d58d58)
Solutions
- Remove the assignment; `numTemplates` is observable only (`var n = doc.numTemplates;`, always 0 in PDF.js).
- If template behavior is required, implement it at the host/integration layer rather than via this property.
- Guard execution of legacy scripts with try/catch to tolerate the unsupported write.
Example fix
// before doc.numTemplates = 3; // after var n = doc.numTemplates; // 0 in PDF.js; templates unsupported
Defensive patterns
Strategy: try-catch
Validate before calling
const READ_ONLY_DOC_PROPS = new Set(['numTemplates','templates' /* ... */]);
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, 'numTemplates'); // true Try / catch
try {
doc.numTemplates = 3;
} catch (e) {
if (/numTemplates is read-only/.test(e.message)) console.warn(e.message);
else throw e;
} Prevention
- PDF.js does not implement page templates; never write template-count properties.
- Lint scripts for read-only template properties before execution.
- Wrap legacy dynamic-form scripts in try/catch.
When it happens
Trigger: A script writes `doc.numTemplates = n;` or increments it. Because templates are unsupported, the value would be meaningless to set even if the setter accepted it; instead it throws.
Common situations: Acrobat SDK sample code that demonstrates template creation; legacy dynamic-form scripts; scripts unaware that PDF.js does not implement page templates.
Related errors
- doc.templates 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/59091bbed8089949.
Report an issue: GitHub.