parallax/jsPDF · error · Error
Invalid unit: {unit}
Error message
Invalid unit: {unit} What it means
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.
Source
Thrown at src/jspdf.js:3292
scaleFactor = 72 / 96;
} else {
scaleFactor = 96 / 72;
}
break;
case "pc":
scaleFactor = 12;
break;
case "em":
scaleFactor = 12;
break;
case "ex":
scaleFactor = 6;
break;
default:
if (typeof unit === "number") {
scaleFactor = unit;
} else {
throw new Error("Invalid unit: " + unit);
}
}
var encryption = null;
setCreationDate();
setFileId();
var getEncryptor = function(objectId) {
if (encryptionOptions !== null) {
return encryption.encryptor(objectId, 0);
}
return function(data) {
return data;
};
};
//---------------------------------------
// Public APIView on GitHub (pinned to a3930ce03a)
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.
Example fix
// before
const doc = new jsPDF({ unit: 'inch' }); // throws
// after
const doc = new jsPDF({ unit: 'in' }); Defensive patterns
Strategy: validation
Validate before calling
const VALID_UNITS = ['pt','mm','cm','in','px','pc','em','ex'];
function makeDoc(unit) {
if (typeof unit === 'number' || VALID_UNITS.includes(unit)) {
return new jsPDF({ unit });
}
throw new Error('Unsupported unit: ' + unit);
} Type guard
function isValidUnit(u) { return typeof u === 'number' || ['pt','mm','cm','in','px','pc','em','ex'].includes(u); } Prevention
- Whitelist unit strings at the config boundary.
- Prefer 'pt' or 'mm' for portability.
- Pass a number only when you need a custom scale factor.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- zoom must be Integer (e.g. 2), a percentage Value (e.g. 300%
- Page mode must be one of UseNone, UseOutlines, UseThumbs, or
- Layout mode must be one of continuous, single, twoleft, twor
- Invalid format: ${format}
- Invalid arguments passed to PubSub.subscribe (jsPDF-module)
AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13).
Data as JSON: /api/errors/f936755b56730be8.
Report an issue: GitHub.