parallax/jsPDF · error · Error

getTextDimensions expects text-parameter to be of type Strin

Error message

getTextDimensions expects text-parameter to be of type String or type Number or an Array of Strings.

What it means

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.

Source

Thrown at src/modules/cell.js:195

   * @returns {Object} dimensions
   */
  jsPDFAPI.getTextDimensions = function(text, options) {
    _initialize.call(this);
    options = options || {};
    var fontSize = options.fontSize || this.getFontSize();
    var font = options.font || this.getFont();
    var scaleFactor = options.scaleFactor || this.internal.scaleFactor;
    var width = 0;
    var amountOfLines = 0;
    var height = 0;
    var tempWidth = 0;
    var scope = this;

    if (!Array.isArray(text) && typeof text !== "string") {
      if (typeof text === "number") {
        text = String(text);
      } else {
        throw new Error(
          "getTextDimensions expects text-parameter to be of type String or type Number or an Array of Strings."
        );
      }
    }

    const maxWidth = options.maxWidth;
    if (maxWidth > 0) {
      if (typeof text === "string") {
        text = this.splitTextToSize(text, maxWidth);
      } else if (Object.prototype.toString.call(text) === "[object Array]") {
        text = text.reduce(function(acc, textLine) {
          return acc.concat(scope.splitTextToSize(textLine, maxWidth));
        }, []);
      }
    } else {
      // Without the else clause, it will not work if you do not pass along maxWidth
      text = Array.isArray(text) ? text : [text];
    }

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Coerce input to string: doc.getTextDimensions(String(text || ''))
  2. Type-check before calling: if (typeof text === 'string' || typeof text === 'number') doc.getTextDimensions(text)
  3. Handle null/undefined explicitly: doc.getTextDimensions(text ?? '')
  4. If passing an array, ensure every element is a string: arr.map(String)

Example fix

// before
var label = data?.label; // could be undefined
doc.getTextDimensions(label); // throws if undefined

// after
var dims = doc.getTextDimensions(label ?? '');
// or
doc.getTextDimensions(String(label));
Defensive patterns

Strategy: type-guard

Validate before calling

// Ensure text parameter is valid before calling getTextDimensions
function safeGetTextDimensions(doc, text, options) {
  if (text === null || text === undefined) {
    text = '';
  } else if (typeof text === 'number') {
    text = String(text);
  } else if (Array.isArray(text)) {
    text = text.map(function(t) { return String(t); });
  } else if (typeof text !== 'string') {
    text = String(text);
  }
  return doc.getTextDimensions(text, options);
}

Type guard

/**
 * @param {*} text
 * @returns {boolean}
 */
function isValidTextForDimensions(text) {
  return typeof text === 'string' ||
    typeof text === 'number' ||
    (Array.isArray(text) && text.every(function(t) {
      return typeof t === 'string' || typeof t === 'number';
    }));
}

Try / catch

try {
  var dims = doc.getTextDimensions(text);
} catch (e) {
  if (e.message.includes('getTextDimensions expects')) {
    var dims = doc.getTextDimensions(String(text || ''));
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/c9c5063c1fb1c22e. Report an issue: GitHub.