{"record":{"id":"07f858890ab33c27","repo":"mozilla/pdf.js","slug":"doc-templates-is-read-only","errorCode":null,"errorMessage":"doc.templates is read-only","messagePattern":"doc\\.templates is read-only","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/scripting_api/doc.js","lineNumber":671,"sourceCode":"\n  set spellLanguageOrder(spellLanguageOrder) {\n    this._spellLanguageOrder = spellLanguageOrder;\n  }\n\n  get subject() {\n    return this._subject;\n  }\n\n  set subject(_) {\n    throw new Error(\"doc.subject is read-only\");\n  }\n\n  get templates() {\n    return [];\n  }\n\n  set templates(_) {\n    throw new Error(\"doc.templates is read-only\");\n  }\n\n  get title() {\n    return this._title;\n  }\n\n  set title(_) {\n    throw new Error(\"doc.title is read-only\");\n  }\n\n  get URL() {\n    return this._URL;\n  }\n\n  set URL(_) {\n    throw new Error(\"doc.URL is read-only\");\n  }\n","sourceCodeStart":653,"sourceCodeEnd":689,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/scripting_api/doc.js#L653-L689","documentation":"`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.","triggerScenarios":"A script writes `doc.templates = [...];` or `doc.templates.push(t);`. The setter throws on the assignment.","commonSituations":"Dynamic-form scripts ported from Acrobat that spawn template pages; legacy enterprise forms; code unaware PDF.js lacks template support.","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."],"exampleFix":"// before\ndoc.templates = [tpl];\n\n// after\nvar t = doc.templates; // read-only; [] in PDF.js (unsupported)","handlingStrategy":"try-catch","validationCode":"const READ_ONLY_DOC_PROPS = new Set(['templates','numTemplates' /* ... */]);\nfunction assertsNoReadOnlyWrite(scriptSrc) {\n  for (const p of READ_ONLY_DOC_PROPS) {\n    if (new RegExp(`\\\\bdoc\\\\.${p}\\\\s*=[^=]`).test(scriptSrc)) {\n      throw new Error(`Script writes read-only property doc.${p}`);\n    }\n  }\n}\nassertsNoReadOnlyWrite(myScript);","typeGuard":"function isReadOnlyDocProp(doc, prop) {\n  const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(doc), prop)\n    || Object.getOwnPropertyDescriptor(doc, prop);\n  return !!desc && !!desc.get && !desc.set;\n}\nisReadOnlyDocProp(doc, 'templates'); // true","tryCatchPattern":"try {\n  doc.templates = [tpl];\n} catch (e) {\n  if (/templates is read-only/.test(e.message)) console.warn(e.message);\n  else throw e;\n}","preventionTips":["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."],"tags":["scripting","acrobat-js-api","doc-object","read-only","templates","unsupported"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}