parallax/jsPDF · error · Error

Invalid argument passed to jsPDF.f2

Error message

Invalid argument passed to jsPDF.f2

What it means

`f2` (src/jspdf.js:527) formats a number to 2 decimal places via `roundToPrecision(number, 2)` and throws at src/jspdf.js:529 when the input is NaN. f2 is used internally for many coordinate outputs. A throw here means a NaN reached a code path that formats 2-decimal values — typically a color component (encodeColorString uses precision 2), a stroke width, or a control-point coordinate.

Source

Thrown at src/jspdf.js:529

        throw new Error("Invalid argument passed to jsPDF.hpf");
      }
      if (number > -1 && number < 1) {
        return roundToPrecision(number, 16);
      } else {
        return roundToPrecision(number, 5);
      }
    };
  } 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) {

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Inspect the arguments to the failing color/geometry call and find the NaN.
  2. For colors, prefer hex strings (`'#ff0000'`) or validated 0-255 numbers; coerce with `Number.isFinite`.
  3. For geometry, sanitize all coordinate inputs before passing to jsPDF.
  4. Add a render-time invariant that no argument is non-finite.

Example fix

// before
 doc.setFillColor(red, green, NaN);   // bad channel

// after
 function clamp255(v){ return Number.isFinite(v) ? Math.max(0, Math.min(255, v)) : 0; }
 doc.setFillColor(clamp255(red), clamp255(green), clamp255(blue));
Defensive patterns

Strategy: validation

Validate before calling

function clamp255(v){ return Number.isFinite(v) ? Math.max(0, Math.min(255, v)) : 0; }
doc.setFillColor(clamp255(r), clamp255(g), clamp255(b));

Type guard

function isFiniteChannel(v) { return typeof v === 'number' && Number.isFinite(v); }

Try / catch

try {
  doc.setFillColor(r, g, b);
} catch (e) {
  if (/jsPDF\.f2/.test(e.message)) {
    doc.setFillColor(clamp255(r), clamp255(g), clamp255(b));
  } else throw e;
}

Prevention

When it happens

Trigger: Passing NaN into setDrawColor/setFillColor/setTextColor as a numeric channel; a NaN in a bezier curve control point; calling line/rect/triangle/ellipse with a NaN coordinate; lineWidth computed as NaN.

Common situations: Color values parsed from CSS strings that failed to convert (producing NaN) and were passed as numbers; geometry computed from DOM measurements of detached/hidden elements returning NaN; data-binding numeric fields that are null.

Related errors


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