mozilla/pdf.js · error · Error
doc.templates is read-only
Error message
doc.templates is read-only
What it means
`Doc.templates` returns the array of page-template objects; PDF.js returns `[]` because page templates are not implemented. It is read-only per the Acrobat spec — templates are created/removed via `createTemplate`/`removeTemplate`, never by array assignment. The throwing setter enforces that and rejects attempts to inject templates the engine cannot honor.
Source
Thrown at src/scripting_api/doc.js:671
set spellLanguageOrder(spellLanguageOrder) {
this._spellLanguageOrder = spellLanguageOrder;
}
get subject() {
return this._subject;
}
set subject(_) {
throw new Error("doc.subject is read-only");
}
get templates() {
return [];
}
set templates(_) {
throw new Error("doc.templates is read-only");
}
get title() {
return this._title;
}
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");
}
View on GitHub (pinned to 5903d58d58)
Solutions
- Remove the assignment; read the value (`var t = doc.templates;`, `[]` in PDF.js).
- Implement page-spawning logic at the host/integration layer.
- Run legacy scripts through try/catch.
Example fix
// before doc.templates = [tpl]; // after var t = doc.templates; // read-only; [] in PDF.js (unsupported)
Defensive patterns
Strategy: try-catch
Validate before calling
const READ_ONLY_DOC_PROPS = new Set(['templates','numTemplates' /* ... */]);
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, 'templates'); // true Try / catch
try {
doc.templates = [tpl];
} catch (e) {
if (/templates is read-only/.test(e.message)) console.warn(e.message);
else throw e;
} Prevention
- PDF.js does not implement page templates; never write template properties.
- Implement page-spawning at the host layer.
- Lint and try/catch legacy dynamic-form scripts.
When it happens
Trigger: A script writes `doc.templates = [...];` or `doc.templates.push(t);`. The setter throws on the assignment.
Common situations: Dynamic-form scripts ported from Acrobat that spawn template pages; legacy enterprise forms; code unaware PDF.js lacks template support.
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/07f858890ab33c27.
Report an issue: GitHub.