parallax/jsPDF · error · Error

Invalid argument passed to jsPDF.scale

Error message

Invalid argument passed to jsPDF.scale

What it means

`scale` (src/jspdf.js:541) multiplies a number by the unit scaleFactor in compat mode, or returns it unchanged in advanced mode. It throws at src/jspdf.js:543 if the value is NaN. scale is exposed as `API.scale` and is the bridge between user units (mm, pt, in) and PDF units, so a NaN here means a non-finite measurement was passed into a coordinate/size that the library then scaled.

Source

Thrown at src/jspdf.js:543

    };
  }
  var f2 = (API.f2 = API.__private__.f2 = function(number) {
    if (isNaN(number)) {
      throw new Error("Invalid argument passed to jsPDF.f2");
    }
    return roundToPrecision(number, 2);
  });

  var f3 = (API.__private__.f3 = function(number) {
    if (isNaN(number)) {
      throw new Error("Invalid argument passed to jsPDF.f3");
    }
    return roundToPrecision(number, 3);
  });

  var scale = (API.scale = API.__private__.scale = function(number) {
    if (isNaN(number)) {
      throw new Error("Invalid argument passed to jsPDF.scale");
    }
    if (apiMode === ApiMode.COMPAT) {
      return number * scaleFactor;
    } else if (apiMode === ApiMode.ADVANCED) {
      return number;
    }
  });

  var transformY = function(y) {
    if (apiMode === ApiMode.COMPAT) {
      return getPageHeight() - y;
    } else if (apiMode === ApiMode.ADVANCED) {
      return y;
    }
  };

  var transformScaleY = function(y) {
    return scale(transformY(y));

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Find the NaN among the numeric arguments to the failing call.
  2. Validate measurements at the source (DOM reads, data parsing) with `Number.isFinite`.
  3. Default non-finite values to 0 or skip the drawing operation.
  4. When doing manual unit math, guard divisors and parseFloat results.

Example fix

// before
 var widthInPt = px / 0;          // NaN
 doc.rect(0, 0, widthInPt, 100);

// after
 var widthInPt = Number.isFinite(px / dpi) ? px / dpi : 0;
 doc.rect(0, 0, widthInPt, 100);
Defensive patterns

Strategy: validation

Validate before calling

function finiteNum(x) { return Number.isFinite(x) ? x : 0; }
// guard unit conversions and DOM-derived measurements:
var widthPx = Number.isFinite(parseFloat(style.width)) ? parseFloat(style.width) : 0;
var widthPt = finiteNum(widthPx / dpi);
doc.rect(0, 0, widthPt, 100);

Type guard

function isFiniteNumber(x) { return typeof x === 'number' && Number.isFinite(x); }

Try / catch

try {
  doc.rect(0, 0, w, h);
} catch (e) {
  if (/jsPDF\.scale/.test(e.message)) {
    doc.rect(0, 0, finiteNum(w), finiteNum(h));
  } else throw e;
}

Prevention

When it happens

Trigger: Calling any positional drawing API with a NaN coordinate/size while in compat mode (scaleFactor is applied); directly invoking `doc.scale(NaN)`; passing a NaN that survives into a coordinate transformation.

Common situations: Unit conversion helpers that divide by zero; reading a numeric CSS value (e.g. via getComputedStyle on a hidden element) returning NaN; computing page-relative offsets from a measurement that failed.

Related errors


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