mozilla/pdf.js · error · Error

doc.bookmarkRoot is read-only

Error message

doc.bookmarkRoot is read-only

What it means

PDF.js's scripting sandbox (src/scripting_api) implements the Acrobat JavaScript `doc` object inside a QuickJS sandbox. Per the Acrobat API specification this property is read-only, so the class defines a getter that returns the current value and a setter that unconditionally throws `Error("doc.<prop> is read-only")`. The throw aborts the currently executing sandboxed script event and is reported back to the host. `bookmarkRoot` is the root of the bookmark (outline) tree; pdf.js returns `undefined` (not implemented) and forbids replacement.

Source

Thrown at src/scripting_api/doc.js:279

  set author(_) {
    throw new Error("doc.author is read-only");
  }

  get baseURL() {
    return this._baseURL;
  }

  set baseURL(baseURL) {
    this._baseURL = baseURL;
  }

  get bookmarkRoot() {
    return undefined;
  }

  set bookmarkRoot(_) {
    throw new Error("doc.bookmarkRoot is read-only");
  }

  get calculate() {
    return this._calculate;
  }

  set calculate(calculate) {
    this._calculate = calculate;
  }

  get creator() {
    return this._creator;
  }

  set creator(_) {
    throw new Error("doc.creator is read-only");
  }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Remove the assignment to `doc.bookmarkRoot`; read it through the getter instead.
  2. Build/edit the outline at PDF authoring time; pdf.js does not expose a writable bookmark root.
  3. If you cannot edit the PDF's embedded script, wrap the statement in try/catch so the rest of the form logic still runs.

Example fix

// before
doc.bookmarkRoot = new Bookmark();
// after
// bookmarkRoot is read-only — drop the assignment; read via doc.bookmarkRoot
Defensive patterns

Strategy: validation

Validate before calling

// Known read-only doc properties (PDF.js scripting_api/doc.js)
const READONLY_DOC_PROPS = new Set([
  'author','bookmarkRoot','creator','dataObjects','docID',
  'documentFileName','dynamicXFAForm','external','filesize','hidden',
  'hostContainer','icons','info','innerAppWindowRect','innerDocWindowRect',
  'isModal','keywords','modDate'
]);
if (READONLY_DOC_PROPS.has('bookmarkRoot')) {
  // skip the write; bookmarkRoot is read-only in pdf.js
  console.warn('doc.bookmarkRoot is read-only in pdf.js');
} else {
  doc.bookmarkRoot = value;
}

Type guard

// Known read-only doc properties (PDF.js scripting_api/doc.js)
const READONLY_DOC_PROPS = new Set([
  'author','bookmarkRoot','creator','dataObjects','docID',
  'documentFileName','dynamicXFAForm','external','filesize','hidden',
  'hostContainer','icons','info','innerAppWindowRect','innerDocWindowRect',
  'isModal','keywords','modDate'
]);
const isReadOnlyDocProp = (name) => READONLY_DOC_PROPS.has(name);
// usage: if (!isReadOnlyDocProp('keywords')) doc.keywords = v;

Try / catch

try {
  doc.bookmarkRoot = value;
} catch (e) {
  // pdf.js throws Error('doc.bookmarkRoot is read-only')
  if (/is read-only/.test(e.message)) { /* swallow, expected */ }
  else { throw e; }
}

Prevention

When it happens

Trigger: A sandboxed PDF form/script executes an assignment to the property, e.g. `doc.bookmarkRoot = value;` or `doc.bookmarkRoot++`. Because the setter always throws (there is no condition), any write attempt triggers it.

Common situations: Scripts ported from desktop Acrobat where bookmarkRoot was assignable; forms that try to stamp runtime state into document metadata on save/open; and PDFs generated by tools that emit non-spec-compliant JavaScript.

Related errors


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