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

  1. Remove the assignment; read the value (`var t = doc.templates;`, `[]` in PDF.js).
  2. Implement page-spawning logic at the host/integration layer.
  3. 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

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


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/07f858890ab33c27. Report an issue: GitHub.