parallax/jsPDF · error · Error

Invalid argument passed to jsPDF.setCharSpace

Error message

Invalid argument passed to jsPDF.setCharSpace

What it means

Thrown by jsPDF.setCharSpace when charSpace is NaN. The single guard is `isNaN(charSpace)`, so non-numbers (coerced to NaN), undefined, and unparseable strings are rejected. charSpace must be a finite numeric value (it is stored as activeCharSpace and used in text spacing calculations).

Source

Thrown at src/jspdf.js:5411

   * @name getCharSpace
   */
  var getCharSpace = (API.__private__.getCharSpace = API.getCharSpace = function() {
    return parseFloat(activeCharSpace || 0);
  });

  /**
   * Set global value of CharSpace.
   *
   * @param {number} charSpace
   * @function
   * @instance
   * @returns {jsPDF} jsPDF-instance
   * @memberof jsPDF#
   * @name setCharSpace
   */
  API.__private__.setCharSpace = API.setCharSpace = function(charSpace) {
    if (isNaN(charSpace)) {
      throw new Error("Invalid argument passed to jsPDF.setCharSpace");
    }
    activeCharSpace = charSpace;
    return this;
  };

  var lineCapID = 0;
  /**
   * Is an Object providing a mapping from human-readable to
   * integer flag values designating the varieties of line cap
   * and join styles.
   *
   * @memberof jsPDF#
   * @name CapJoinStyles
   */
  API.CapJoinStyles = {
    0: 0,
    butt: 0,
    but: 0,

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Pass a finite number: setCharSpace(1.5).
  2. Coerce with Number() and guard: `Number.isFinite(Number(v)) ? Number(v) : 0`.
  3. Avoid null (it silently becomes 0); prefer an explicit numeric default.

Example fix

// before
pdf.setCharSpace(opts.spacing); // opts.spacing may be undefined
// after
pdf.setCharSpace(Number.isFinite(Number(opts.spacing)) ? Number(opts.spacing) : 0);
Defensive patterns

Strategy: validation

Validate before calling

function safeSetCharSpace(doc, charSpace) {
  const v = Number(charSpace);
  if (!Number.isFinite(v)) throw new Error('charSpace must be a finite number');
  return doc.setCharSpace(v);
}

Type guard

function isFiniteNumber(v) { return Number.isFinite(Number(v)); }

Prevention

When it happens

Trigger: Calling setCharSpace(undefined), setCharSpace('abc'), setCharSpace({}), or a value derived from a missing property. setCharSpace(null) does NOT throw (null coerces to 0) but is not meaningful.

Common situations: Reading a spacing value from user input or JSON without parsing; forgetting to pass the argument; passing an object or string from a config layer.

Related errors


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