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 API

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Use one of the supported unit strings: 'pt', 'mm', 'cm', 'in', 'px', 'pc', 'em', or 'ex'.
  2. If you need a custom scale, pass a number: `new jsPDF({ unit: 72 })` treats it as points-per-user-unit.
  3. 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

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


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/f936755b56730be8. Report an issue: GitHub.