{"record":{"id":"514108610e4fe246","repo":"parallax/jsPDF","slug":"invalid-arguments-passed-to-jspdf-context2d-fillte","errorCode":null,"errorMessage":"Invalid arguments passed to jsPDF.context2d.fillText","messagePattern":"Invalid arguments passed to jsPDF\\.context2d\\.fillText","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/context2d.js","lineNumber":1329,"sourceCode":"  var isStrokeTransparent = function() {\n    return Boolean(this.ctx.isStrokeTransparent || this.globalAlpha == 0);\n  };\n\n  /**\n   * Draws \"filled\" text on the canvas\n   *\n   * @name fillText\n   * @function\n   * @param text {String} Specifies the text that will be written on the canvas\n   * @param x {Number} The x coordinate where to start painting the text (relative to the canvas)\n   * @param y {Number} The y coordinate where to start painting the text (relative to the canvas)\n   * @param maxWidth {Number} Optional. The maximum allowed width of the text, in pixels\n   * @description The fillText() method draws filled text on the canvas. The default color of the text is black.\n   */\n  Context2D.prototype.fillText = function(text, x, y, maxWidth) {\n    if (isNaN(x) || isNaN(y) || typeof text !== \"string\") {\n      console.error(\"jsPDF.context2d.fillText: Invalid arguments\", arguments);\n      throw new Error(\"Invalid arguments passed to jsPDF.context2d.fillText\");\n    }\n    maxWidth = isNaN(maxWidth) ? undefined : maxWidth;\n    if (isFillTransparent.call(this)) {\n      return;\n    }\n\n    var degs = rad2deg(this.ctx.transform.rotation);\n\n    // We only use X axis as scale hint\n    var scale = this.ctx.transform.scaleX;\n\n    putText.call(this, {\n      text: text,\n      x: x,\n      y: y,\n      scale: scale,\n      angle: degs,\n      align: this.textAlign,","sourceCodeStart":1311,"sourceCodeEnd":1347,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/context2d.js#L1311-L1347","documentation":"fillText requires the text argument to be a string and x/y to be finite numbers; the guard is isNaN(x) || isNaN(y) || typeof text !== 'string'. maxWidth is optional — an NaN maxWidth is silently coerced to undefined, so it never triggers this error. Validation runs before the fill-transparency check.","triggerScenarios":"ctx.fillText(text, x, y) where text is a number, object, undefined, or null; x or y undefined/NaN; passing a numeric value where text was expected (e.g. argument order swapped).","commonSituations":"Rendering metrics or counters without String() coercion; calling fillText(value, x, y) instead of fillText(String(value), x, y); reading x/y from NaN-producing transforms.","solutions":["Coerce text with String(...) and validate x/y with Number.isFinite before calling.","Guard the whole call: if (typeof text === 'string' && Number.isFinite(+x) && Number.isFinite(+y)) ctx.fillText(text, +x, +y);","Check argument order — text comes first, then x, then y, then optional maxWidth."],"exampleFix":"// before\nctx.fillText(score, x, y); // score is a number -> throws [106]\n\n// after\nif (Number.isFinite(x) && Number.isFinite(y)) ctx.fillText(String(score), x, y);","handlingStrategy":"validation","validationCode":"function safeFillText(ctx, text, x, y, maxWidth) {\n  if (typeof text !== 'string') text = String(text);\n  if (!Number.isFinite(+x) || !Number.isFinite(+y)) return;\n  ctx.fillText(text, +x, +y, Number.isFinite(+maxWidth) ? +maxWidth : undefined);\n}","typeGuard":"function isFillTextArgs(text, x, y) {\n  return typeof text === 'string' && Number.isFinite(+x) && Number.isFinite(+y);\n}","tryCatchPattern":"try { ctx.fillText(text, x, y); }\ncatch (e) { if (!/context2d.fillText/.test(e.message)) throw e; /* else stringify+retry */ }","preventionTips":["Always stringify dynamic values before passing as text.","Keep argument order: text, x, y, [maxWidth].","Validate coordinates at the layout boundary."],"tags":["canvas2d","context2d","validation","text","nan","pdf"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}