{"record":{"id":"c9c5063c1fb1c22e","repo":"parallax/jsPDF","slug":"gettextdimensions-expects-text-parameter-to-be-of","errorCode":null,"errorMessage":"getTextDimensions expects text-parameter to be of type String or type Number or an Array of Strings.","messagePattern":"getTextDimensions expects text-parameter to be of type String or type Number or an Array of Strings\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/cell.js","lineNumber":195,"sourceCode":"   * @returns {Object} dimensions\n   */\n  jsPDFAPI.getTextDimensions = function(text, options) {\n    _initialize.call(this);\n    options = options || {};\n    var fontSize = options.fontSize || this.getFontSize();\n    var font = options.font || this.getFont();\n    var scaleFactor = options.scaleFactor || this.internal.scaleFactor;\n    var width = 0;\n    var amountOfLines = 0;\n    var height = 0;\n    var tempWidth = 0;\n    var scope = this;\n\n    if (!Array.isArray(text) && typeof text !== \"string\") {\n      if (typeof text === \"number\") {\n        text = String(text);\n      } else {\n        throw new Error(\n          \"getTextDimensions expects text-parameter to be of type String or type Number or an Array of Strings.\"\n        );\n      }\n    }\n\n    const maxWidth = options.maxWidth;\n    if (maxWidth > 0) {\n      if (typeof text === \"string\") {\n        text = this.splitTextToSize(text, maxWidth);\n      } else if (Object.prototype.toString.call(text) === \"[object Array]\") {\n        text = text.reduce(function(acc, textLine) {\n          return acc.concat(scope.splitTextToSize(textLine, maxWidth));\n        }, []);\n      }\n    } else {\n      // Without the else clause, it will not work if you do not pass along maxWidth\n      text = Array.isArray(text) ? text : [text];\n    }","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/cell.js#L177-L213","documentation":"getTextDimensions() calculates the rendered width and height of text. It accepts a string, a number (which is coerced to a string), or an array of strings (each element treated as a line). Any other type — objects, booleans, undefined, null, functions — is rejected because the text measurement logic (getStringUnitWidth) requires string inputs to look up character widths in the font metrics table.","triggerScenarios":"Calling doc.getTextDimensions(undefined) when a variable is not initialized. Passing an object like { text: 'hello' } instead of the string itself. Passing null from an optional parameter. Passing a number inside an array of non-strings: getTextDimensions([123, 'abc']) where 123 is not pre-stringified. Passing a boolean or Date object.","commonSituations":"Optional chaining producing undefined: getTextDimensions(data?.label) where data is null. Receiving values from JSON parsing where types are unknown. Passing DOM element text content that could be null. Working with form field values that may be empty/undefined.","solutions":["Coerce input to string: doc.getTextDimensions(String(text || ''))","Type-check before calling: if (typeof text === 'string' || typeof text === 'number') doc.getTextDimensions(text)","Handle null/undefined explicitly: doc.getTextDimensions(text ?? '')","If passing an array, ensure every element is a string: arr.map(String)"],"exampleFix":"// before\nvar label = data?.label; // could be undefined\ndoc.getTextDimensions(label); // throws if undefined\n\n// after\nvar dims = doc.getTextDimensions(label ?? '');\n// or\ndoc.getTextDimensions(String(label));","handlingStrategy":"type-guard","validationCode":"// Ensure text parameter is valid before calling getTextDimensions\nfunction safeGetTextDimensions(doc, text, options) {\n  if (text === null || text === undefined) {\n    text = '';\n  } else if (typeof text === 'number') {\n    text = String(text);\n  } else if (Array.isArray(text)) {\n    text = text.map(function(t) { return String(t); });\n  } else if (typeof text !== 'string') {\n    text = String(text);\n  }\n  return doc.getTextDimensions(text, options);\n}","typeGuard":"/**\n * @param {*} text\n * @returns {boolean}\n */\nfunction isValidTextForDimensions(text) {\n  return typeof text === 'string' ||\n    typeof text === 'number' ||\n    (Array.isArray(text) && text.every(function(t) {\n      return typeof t === 'string' || typeof t === 'number';\n    }));\n}","tryCatchPattern":"try {\n  var dims = doc.getTextDimensions(text);\n} catch (e) {\n  if (e.message.includes('getTextDimensions expects')) {\n    var dims = doc.getTextDimensions(String(text || ''));\n  } else throw e;\n}","preventionTips":["Always coerce text input with String() before calling getTextDimensions","Handle null/undefined with nullish coalescing: text ?? ''","If passing arrays, ensure every element is a string or number"],"tags":["cell","text","type-check","validation"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}