parallax/jsPDF · error · Error

Invalid argument passed to jsPDF.hpf

Error message

Invalid argument passed to jsPDF.hpf

What it means

`hpf` (high-precision float) is jsPDF's smart number formatter. When the constructor's `floatPrecision` option is a number (src/jspdf.js:501), hpf routes through this branch and throws at src/jspdf.js:504 if `isNaN(number)`. This is the formatter used for most coordinate/geometry output. The error means a NaN was passed where a real float was expected; it is almost never a configuration problem in this branch (the precision is fixed and valid) — it is a data problem upstream.

Source

Thrown at src/jspdf.js:504

  };

  var roundToPrecision = (API.roundToPrecision = API.__private__.roundToPrecision = function(
    number,
    parmPrecision
  ) {
    var tmpPrecision = precision || parmPrecision;
    if (isNaN(number) || isNaN(tmpPrecision)) {
      throw new Error("Invalid argument passed to jsPDF.roundToPrecision");
    }
    return number.toFixed(tmpPrecision).replace(/0+$/, "");
  });

  // high precision float
  var hpf;
  if (typeof floatPrecision === "number") {
    hpf = API.hpf = API.__private__.hpf = function(number) {
      if (isNaN(number)) {
        throw new Error("Invalid argument passed to jsPDF.hpf");
      }
      return roundToPrecision(number, floatPrecision);
    };
  } else if (floatPrecision === "smart") {
    hpf = API.hpf = API.__private__.hpf = function(number) {
      if (isNaN(number)) {
        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");

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Identify which numeric argument became NaN by logging the args to the failing draw/text call.
  2. Coerce all external numeric inputs: `Number.isFinite(v) ? v : 0` (or a sensible default).
  3. Add a central assertion in your rendering layer that rejects non-finite numbers before reaching jsPDF.
  4. If you genuinely want to emit nothing for a missing value, branch around the jsPDF call rather than passing NaN.

Example fix

// before
 var w = parseFloat(el.dataset.width);  // NaN when missing
 doc.rect(x, y, w, h);

// after
 var w = parseFloat(el.dataset.width);
 if (!Number.isFinite(w)) w = 0;
 doc.rect(x, y, w, h);
Defensive patterns

Strategy: validation

Validate before calling

function finiteNum(x) { return Number.isFinite(x) ? x : 0; }
// sanitize all coordinates before any draw call:
[x, y, w, h] = [x, y, w, h].map(finiteNum);
doc.rect(x, y, w, h);

Type guard

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

Try / catch

try {
  doc.rect(x, y, w, h);
} catch (e) {
  if (/jsPDF\.hpf/.test(e.message)) {
    console.warn('Non-finite value in rect, skipping');
  } else throw e;
}

Prevention

When it happens

Trigger: Any drawing/text call that internally formats a NaN value through hpf while floatPrecision is a number (the common case, since the default is 16): e.g. `doc.line(undefined/0, ...)`; passing a NaN computed from invalid arithmetic into `rect`, `line`, `text`, `triangle`, etc.

Common situations: Dynamic layouts where a width/height is derived from a container measurement that returned NaN (e.g. reading an SVG attribute that doesn't exist); aggregating coordinates from JSON that has missing/null numbers; math libraries returning NaN for edge cases.

Related errors


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