{"record":{"id":"12e39f3dbb5d1847","repo":"parallax/jsPDF","slug":"invalid-argument-passed-to-jspdf-f2","errorCode":null,"errorMessage":"Invalid argument passed to jsPDF.f2","messagePattern":"Invalid argument passed to jsPDF\\.f2","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jspdf.js","lineNumber":529,"sourceCode":"        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\");\n      }\n      return roundToPrecision(number, 16);\n    };\n  }\n  var f2 = (API.f2 = API.__private__.f2 = function(number) {\n    if (isNaN(number)) {\n      throw new Error(\"Invalid argument passed to jsPDF.f2\");\n    }\n    return roundToPrecision(number, 2);\n  });\n\n  var f3 = (API.__private__.f3 = function(number) {\n    if (isNaN(number)) {\n      throw new Error(\"Invalid argument passed to jsPDF.f3\");\n    }\n    return roundToPrecision(number, 3);\n  });\n\n  var scale = (API.scale = API.__private__.scale = function(number) {\n    if (isNaN(number)) {\n      throw new Error(\"Invalid argument passed to jsPDF.scale\");\n    }\n    if (apiMode === ApiMode.COMPAT) {\n      return number * scaleFactor;\n    } else if (apiMode === ApiMode.ADVANCED) {","sourceCodeStart":511,"sourceCodeEnd":547,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L511-L547","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the arguments to the failing color/geometry call and find the NaN.","For colors, prefer hex strings (`'#ff0000'`) or validated 0-255 numbers; coerce with `Number.isFinite`.","For geometry, sanitize all coordinate inputs before passing to jsPDF.","Add a render-time invariant that no argument is non-finite."],"exampleFix":"// before\n doc.setFillColor(red, green, NaN);   // bad channel\n\n// after\n function clamp255(v){ return Number.isFinite(v) ? Math.max(0, Math.min(255, v)) : 0; }\n doc.setFillColor(clamp255(red), clamp255(green), clamp255(blue));","handlingStrategy":"validation","validationCode":"function clamp255(v){ return Number.isFinite(v) ? Math.max(0, Math.min(255, v)) : 0; }\ndoc.setFillColor(clamp255(r), clamp255(g), clamp255(b));","typeGuard":"function isFiniteChannel(v) { return typeof v === 'number' && Number.isFinite(v); }","tryCatchPattern":"try {\n  doc.setFillColor(r, g, b);\n} catch (e) {\n  if (/jsPDF\\.f2/.test(e.message)) {\n    doc.setFillColor(clamp255(r), clamp255(g), clamp255(b));\n  } else throw e;\n}","preventionTips":["Pass colors as hex strings or validated 0-255 numbers.","Coerce color channels with a clamp helper before setFillColor/setDrawColor/setTextColor.","Avoid feeding parsed CSS values that may be NaN."],"tags":["numeric","f2","nan","formatting","color","geometry"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}