parallax/jsPDF · error · Error

Invalid argument passed to jsPDF.f3

Error message

Invalid argument passed to jsPDF.f3

What it means

`f3` (src/jspdf.js:534) formats to 3 decimal places via `roundToPrecision(number, 3)` and throws at src/jspdf.js:536 for NaN. Like f2 it is an internal formatter used by encodeColorString (precision 3 default for grayscale/RGB) and various geometry outputs. The root cause is always a NaN reaching a 3-decimal formatting path.

Source

Thrown at src/jspdf.js:536

    };
  } else {
    hpf = API.hpf = API.__private__.hpf = function(number) {
      if (isNaN(number)) {
        throw new Error("Invalid argument passed to jsPDF.hpf");
      }
      return roundToPrecision(number, 16);
    };
  }
  var f2 = (API.f2 = API.__private__.f2 = function(number) {
    if (isNaN(number)) {
      throw new Error("Invalid argument passed to jsPDF.f2");
    }
    return roundToPrecision(number, 2);
  });

  var f3 = (API.__private__.f3 = function(number) {
    if (isNaN(number)) {
      throw new Error("Invalid argument passed to jsPDF.f3");
    }
    return roundToPrecision(number, 3);
  });

  var scale = (API.scale = API.__private__.scale = function(number) {
    if (isNaN(number)) {
      throw new Error("Invalid argument passed to jsPDF.scale");
    }
    if (apiMode === ApiMode.COMPAT) {
      return number * scaleFactor;
    } else if (apiMode === ApiMode.ADVANCED) {
      return number;
    }
  });

  var transformY = function(y) {
    if (apiMode === ApiMode.COMPAT) {
      return getPageHeight() - y;

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Trace the NaN to the offending argument in the failing call.
  2. Coerce all numeric inputs with `Number.isFinite(v) ? v : 0`.
  3. For colors, pass hex strings or validated numbers in [0,255] (or [0,1]).
  4. Centralize numeric sanitization at the boundary of your rendering layer.

Example fix

// before
 doc.setTextColor(ch.r, ch.g, ch.b);  // ch.b is NaN

// after
 var b = Number.isFinite(ch.b) ? ch.b : 0;
 doc.setTextColor(ch.r, ch.g, b);
Defensive patterns

Strategy: validation

Validate before calling

function finiteNum(x) { return Number.isFinite(x) ? x : 0; }
doc.setTextColor(finiteNum(r), finiteNum(g), finiteNum(b));

Type guard

function isFiniteNumber(x) { return typeof x === 'number' && Number.isFinite(x); }

Try / catch

try {
  doc.setTextColor(r, g, b);
} catch (e) {
  if (/jsPDF\.f3/.test(e.message)) {
    doc.setTextColor(finiteNum(r), finiteNum(g), finiteNum(b));
  } else throw e;
}

Prevention

When it happens

Trigger: A NaN color channel flowing into encodeColorString's default (precision 3) branch; a NaN in a coordinate that is formatted at 3-decimal precision; passing undefined into a numeric position argument.

Common situations: Same family as f2: unvalidated numeric data from user input, DOM measurements, or deserialized JSON producing NaN; color values from a picker that occasionally emits null.

Related errors


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