{"record":{"id":"1a84f82134629017","repo":"parallax/jsPDF","slug":"invalid-arguments-passed-to-jspdf-context2d-measur","errorCode":null,"errorMessage":"Invalid arguments passed to jsPDF.context2d.measureText","messagePattern":"Invalid arguments passed to jsPDF\\.context2d\\.measureText","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/context2d.js","lineNumber":1404,"sourceCode":"    });\n  };\n\n  /**\n   * Returns an object that contains the width of the specified text\n   *\n   * @name measureText\n   * @function\n   * @param text {String} The text to be measured\n   * @description The measureText() method returns an object that contains the width of the specified text, in pixels.\n   * @returns {Number}\n   */\n  Context2D.prototype.measureText = function(text) {\n    if (typeof text !== \"string\") {\n      console.error(\n        \"jsPDF.context2d.measureText: Invalid arguments\",\n        arguments\n      );\n      throw new Error(\n        \"Invalid arguments passed to jsPDF.context2d.measureText\"\n      );\n    }\n    var pdf = this.pdf;\n    var k = this.pdf.internal.scaleFactor;\n\n    var fontSize = pdf.internal.getFontSize();\n    var txtWidth =\n      (pdf.getStringUnitWidth(text) * fontSize) / pdf.internal.scaleFactor;\n    txtWidth *= Math.round(((k * 96) / 72) * 10000) / 10000;\n\n    var TextMetrics = function(options) {\n      options = options || {};\n      var _width = options.width || 0;\n      Object.defineProperty(this, \"width\", {\n        get: function() {\n          return _width;\n        }","sourceCodeStart":1386,"sourceCodeEnd":1422,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/context2d.js#L1386-L1422","documentation":"measureText only checks that its argument is a string (typeof text !== 'string'); no NaN checks exist because there are no numeric args. It then computes width from the current font size and the PDF string-unit width. Passing a number, object, null, or undefined throws immediately.","triggerScenarios":"ctx.measureText(value) where value is not a string (number, object, undefined); forgetting to stringify before measuring; calling measureText on a null variable.","commonSituations":"Measuring numeric IDs or counters; receiving typed values from JSON/TS code without String() coercion; null guards missing on optional fields.","solutions":["Always pass a string: ctx.measureText(String(text)).","Guard: if (typeof text === 'string') ctx.measureText(text); else return a zero-width TextMetrics.","Wrap in a helper that returns { width: 0 } for non-string input."],"exampleFix":"// before\nconst m = ctx.measureText(count); // count is a number -> throws [108]\n\n// after\nconst m = ctx.measureText(String(count));","handlingStrategy":"type-guard","validationCode":"function safeMeasure(ctx, text) {\n  if (typeof text !== 'string') text = String(text);\n  return ctx.measureText(text);\n}","typeGuard":"function isMeasurableText(text) {\n  return typeof text === 'string';\n}","tryCatchPattern":"try { return ctx.measureText(text); }\ncatch (e) {\n  if (/context2d.measureText/.test(e.message)) return { width: 0 };\n  throw e;\n}","preventionTips":["Coerce all measured values with String().","Null-check optional fields before measuring.","Return a zero-width TextMetrics fallback when text is absent."],"tags":["canvas2d","context2d","validation","text","pdf"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}