{"record":{"id":"4d02a01171673aae","repo":"mozilla/pdf.js","slug":"doc-title-is-read-only","errorCode":null,"errorMessage":"doc.title is read-only","messagePattern":"doc\\.title is read-only","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/scripting_api/doc.js","lineNumber":679,"sourceCode":"\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\n  get viewState() {\n    return undefined;\n  }\n\n  set viewState(_) {\n    throw new Error(\"doc.viewState is read-only\");\n  }\n","sourceCodeStart":661,"sourceCodeEnd":697,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/scripting_api/doc.js#L661-L697","documentation":"`Doc.title` returns the document's Title metadata string (`this._title`), set at parse time from the info dictionary / XMP. It is read-only per the Acrobat spec because PDF.js scripting does not persist metadata changes back to the file. The throwing setter enforces that contract so a script cannot silently alter the stored title.","triggerScenarios":"A script writes `doc.title = 'My Title';`, typically as branding or on save. The setter throws on assignment.","commonSituations":"Metadata-edit scripts from desktop Acrobat; branding workflows; code assuming in-script title setters are persisted.","solutions":["Remove the assignment; read the value (`var t = doc.title;`).","Set the title at PDF authoring/generation time so it is embedded in the file.","Guard legacy scripts with try/catch to tolerate the write attempt."],"exampleFix":"// before\ndoc.title = 'Invoice 2026';\n\n// after\nvar t = doc.title; // read-only metadata","handlingStrategy":"validation","validationCode":"const READ_ONLY_DOC_PROPS = new Set(['title','subject','author','creator','keywords','producer' /* ... */]);\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, 'title'); // true","tryCatchPattern":"try {\n  doc.title = 'My Title';\n} catch (e) {\n  if (/title is read-only/.test(e.message)) console.warn(e.message);\n  else throw e;\n}","preventionTips":["Title metadata is read-only in scripting; set it when authoring the PDF.","Lint scripts against the read-only metadata set before dispatch.","Wrap branding/metadata scripts in try/catch for legacy tolerance."],"tags":["scripting","acrobat-js-api","doc-object","read-only","metadata"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}