{"record":{"id":"3d28a4fc29d2e343","repo":"parallax/jsPDF","slug":"invalid-argument-passed-to-jspdf-hpf","errorCode":null,"errorMessage":"Invalid argument passed to jsPDF.hpf","messagePattern":"Invalid argument passed to jsPDF\\.hpf","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jspdf.js","lineNumber":504,"sourceCode":"  };\n\n  var roundToPrecision = (API.roundToPrecision = API.__private__.roundToPrecision = function(\n    number,\n    parmPrecision\n  ) {\n    var tmpPrecision = precision || parmPrecision;\n    if (isNaN(number) || isNaN(tmpPrecision)) {\n      throw new Error(\"Invalid argument passed to jsPDF.roundToPrecision\");\n    }\n    return number.toFixed(tmpPrecision).replace(/0+$/, \"\");\n  });\n\n  // high precision float\n  var hpf;\n  if (typeof floatPrecision === \"number\") {\n    hpf = API.hpf = API.__private__.hpf = function(number) {\n      if (isNaN(number)) {\n        throw new Error(\"Invalid argument passed to jsPDF.hpf\");\n      }\n      return roundToPrecision(number, floatPrecision);\n    };\n  } else if (floatPrecision === \"smart\") {\n    hpf = API.hpf = API.__private__.hpf = function(number) {\n      if (isNaN(number)) {\n        throw new Error(\"Invalid argument passed to jsPDF.hpf\");\n      }\n      if (number > -1 && number < 1) {\n        return roundToPrecision(number, 16);\n      } else {\n        return roundToPrecision(number, 5);\n      }\n    };\n  } else {\n    hpf = API.hpf = API.__private__.hpf = function(number) {\n      if (isNaN(number)) {\n        throw new Error(\"Invalid argument passed to jsPDF.hpf\");","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L486-L522","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Identify which numeric argument became NaN by logging the args to the failing draw/text call.","Coerce all external numeric inputs: `Number.isFinite(v) ? v : 0` (or a sensible default).","Add a central assertion in your rendering layer that rejects non-finite numbers before reaching jsPDF.","If you genuinely want to emit nothing for a missing value, branch around the jsPDF call rather than passing NaN."],"exampleFix":"// before\n var w = parseFloat(el.dataset.width);  // NaN when missing\n doc.rect(x, y, w, h);\n\n// after\n var w = parseFloat(el.dataset.width);\n if (!Number.isFinite(w)) w = 0;\n doc.rect(x, y, w, h);","handlingStrategy":"validation","validationCode":"function finiteNum(x) { return Number.isFinite(x) ? x : 0; }\n// sanitize all coordinates before any draw call:\n[x, y, w, h] = [x, y, w, h].map(finiteNum);\ndoc.rect(x, y, w, h);","typeGuard":"function isFiniteNumber(x) { return typeof x === 'number' && Number.isFinite(x); }","tryCatchPattern":"try {\n  doc.rect(x, y, w, h);\n} catch (e) {\n  if (/jsPDF\\.hpf/.test(e.message)) {\n    console.warn('Non-finite value in rect, skipping');\n  } else throw e;\n}","preventionTips":["Validate numeric data at the source (DOM reads, JSON parsing).","Never pass the result of an unchecked parseFloat/division directly to jsPDF.","Centralize a finite-coercion helper for all rendering inputs."],"tags":["numeric","hpf","nan","formatting","floatprecision"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}