parallax/jsPDF · error · Error

Invalid arguments passed to jsPDF.context2d.measureText

Error message

Invalid arguments passed to jsPDF.context2d.measureText

What it means

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.

Source

Thrown at src/modules/context2d.js:1404

    });
  };

  /**
   * Returns an object that contains the width of the specified text
   *
   * @name measureText
   * @function
   * @param text {String} The text to be measured
   * @description The measureText() method returns an object that contains the width of the specified text, in pixels.
   * @returns {Number}
   */
  Context2D.prototype.measureText = function(text) {
    if (typeof text !== "string") {
      console.error(
        "jsPDF.context2d.measureText: Invalid arguments",
        arguments
      );
      throw new Error(
        "Invalid arguments passed to jsPDF.context2d.measureText"
      );
    }
    var pdf = this.pdf;
    var k = this.pdf.internal.scaleFactor;

    var fontSize = pdf.internal.getFontSize();
    var txtWidth =
      (pdf.getStringUnitWidth(text) * fontSize) / pdf.internal.scaleFactor;
    txtWidth *= Math.round(((k * 96) / 72) * 10000) / 10000;

    var TextMetrics = function(options) {
      options = options || {};
      var _width = options.width || 0;
      Object.defineProperty(this, "width", {
        get: function() {
          return _width;
        }

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Always pass a string: ctx.measureText(String(text)).
  2. Guard: if (typeof text === 'string') ctx.measureText(text); else return a zero-width TextMetrics.
  3. Wrap in a helper that returns { width: 0 } for non-string input.

Example fix

// before
const m = ctx.measureText(count); // count is a number -> throws [108]

// after
const m = ctx.measureText(String(count));
Defensive patterns

Strategy: type-guard

Validate before calling

function safeMeasure(ctx, text) {
  if (typeof text !== 'string') text = String(text);
  return ctx.measureText(text);
}

Type guard

function isMeasurableText(text) {
  return typeof text === 'string';
}

Try / catch

try { return ctx.measureText(text); }
catch (e) {
  if (/context2d.measureText/.test(e.message)) return { width: 0 };
  throw e;
}

Prevention

When it happens

Trigger: ctx.measureText(value) where value is not a string (number, object, undefined); forgetting to stringify before measuring; calling measureText on a null variable.

Common situations: Measuring numeric IDs or counters; receiving typed values from JSON/TS code without String() coercion; null guards missing on optional fields.

Related errors


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