{"record":{"id":"f936755b56730be8","repo":"parallax/jsPDF","slug":"invalid-unit-unit","errorCode":null,"errorMessage":"Invalid unit: {unit}","messagePattern":"Invalid unit: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jspdf.js","lineNumber":3292,"sourceCode":"        scaleFactor = 72 / 96;\n      } else {\n        scaleFactor = 96 / 72;\n      }\n      break;\n    case \"pc\":\n      scaleFactor = 12;\n      break;\n    case \"em\":\n      scaleFactor = 12;\n      break;\n    case \"ex\":\n      scaleFactor = 6;\n      break;\n    default:\n      if (typeof unit === \"number\") {\n        scaleFactor = unit;\n      } else {\n        throw new Error(\"Invalid unit: \" + unit);\n      }\n  }\n\n  var encryption = null;\n  setCreationDate();\n  setFileId();\n\n  var getEncryptor = function(objectId) {\n    if (encryptionOptions !== null) {\n      return encryption.encryptor(objectId, 0);\n    }\n    return function(data) {\n      return data;\n    };\n  };\n\n  //---------------------------------------\n  // Public API","sourceCodeStart":3274,"sourceCodeEnd":3310,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L3274-L3310","documentation":"Thrown by the jsPDF constructor when the `unit` option does not match a known unit string and is not a number. Valid string units are 'pt', 'mm', 'cm', 'in', 'px', 'pc', 'em', 'ex'; a numeric value is also accepted as a custom scale factor. Anything else falls through to the default branch and throws.","triggerScenarios":"Passing `new jsPDF({ unit: 'inch' })`, `{ unit: 'pixels' }`, a misspelled unit, wrong case ('MM'), an empty string, or a non-numeric non-string value (e.g. an object) for unit.","commonSituations":"Typos ('inch' vs 'in', 'pixel' vs 'px'); copy-pasting a unit from a tutorial that uses a different library; locale/casing mistakes; passing unit as part of a nested config object instead of the top-level options.","solutions":["Use one of the supported unit strings: 'pt', 'mm', 'cm', 'in', 'px', 'pc', 'em', or 'ex'.","If you need a custom scale, pass a number: `new jsPDF({ unit: 72 })` treats it as points-per-user-unit.","Check for trailing whitespace or wrong case in the unit string."],"exampleFix":"// before\nconst doc = new jsPDF({ unit: 'inch' }); // throws\n// after\nconst doc = new jsPDF({ unit: 'in' });","handlingStrategy":"validation","validationCode":"const VALID_UNITS = ['pt','mm','cm','in','px','pc','em','ex'];\nfunction makeDoc(unit) {\n  if (typeof unit === 'number' || VALID_UNITS.includes(unit)) {\n    return new jsPDF({ unit });\n  }\n  throw new Error('Unsupported unit: ' + unit);\n}","typeGuard":"function isValidUnit(u) { return typeof u === 'number' || ['pt','mm','cm','in','px','pc','em','ex'].includes(u); }","tryCatchPattern":null,"preventionTips":["Whitelist unit strings at the config boundary.","Prefer 'pt' or 'mm' for portability.","Pass a number only when you need a custom scale factor."],"tags":["config","constructor","unit","validation"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}