mozilla/pdf.js · error · Error

doc.subject is read-only

Error message

doc.subject is read-only

What it means

`Doc.subject` returns the document's Subject metadata string (`this._subject`), populated at parse time from the document info dictionary / XMP. It is read-only per the Acrobat spec — PDF.js scripting does not persist metadata changes back to the file. The throwing setter enforces that contract so a script cannot silently misrepresent stored metadata.

Source

Thrown at src/scripting_api/doc.js:663

  set spellDictionaryOrder(spellDictionaryOrder) {
    this._spellDictionaryOrder = spellDictionaryOrder;
  }

  get spellLanguageOrder() {
    return this._spellLanguageOrder;
  }

  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");
  }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Remove the assignment; read the value (`var s = doc.subject;`).
  2. Edit metadata at PDF generation/processing time (it is set by the authoring tool and embedded in the file).
  3. Wrap legacy scripts in try/catch to tolerate the write attempt.

Example fix

// before
doc.subject = 'Annual Report';

// after
var s = doc.subject; // read-only metadata
Defensive patterns

Strategy: validation

Validate before calling

const READ_ONLY_DOC_PROPS = new Set(['subject','title','author','creator','keywords','producer' /* ... */]);
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, 'subject'); // true

Try / catch

try {
  doc.subject = 'New Subject';
} catch (e) {
  if (/subject is read-only/.test(e.message)) console.warn(e.message);
  else throw e;
}

Prevention

When it happens

Trigger: A script writes `doc.subject = 'New Subject';`, typically in a save/branding workflow. The setter throws on assignment.

Common situations: Metadata-edit scripts from desktop Acrobat; branding templates; code that assumes in-script metadata setters persist to disk.

Related errors


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