parallax/jsPDF · error · Error

Invalid measurements (width and/or height) passed to jsPDF.a

Error message

Invalid measurements (width and/or height) passed to jsPDF.addSvgAsImage

What it means

addSvgAsImage does NOT auto-size from the SVG's intrinsic width/height/viewBox; it allocates a canvas of exactly canvas.width=w and canvas.height=h before rendering (ignoreDimensions is set in options). The guard isNaN(w)||isNaN(h) therefore throws if either dimension is not a number, because the canvas could not be created with a numeric size. The caller must always supply concrete pixel/unit dimensions.

Source

Thrown at src/modules/svg.js:114

   */
  jsPDFAPI.addSvgAsImage = function(
    svg,
    x,
    y,
    w,
    h,
    alias,
    compression,
    rotation
  ) {
    if (isNaN(x) || isNaN(y)) {
      console.error("jsPDF.addSvgAsImage: Invalid coordinates", arguments);
      throw new Error("Invalid coordinates passed to jsPDF.addSvgAsImage");
    }

    if (isNaN(w) || isNaN(h)) {
      console.error("jsPDF.addSvgAsImage: Invalid measurements", arguments);
      throw new Error(
        "Invalid measurements (width and/or height) passed to jsPDF.addSvgAsImage"
      );
    }

    var canvas = document.createElement("canvas");
    canvas.width = w;
    canvas.height = h;
    var ctx = canvas.getContext("2d");
    ctx.fillStyle = "#fff"; /// set white fill style
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    var options = {
      ignoreMouse: true,
      ignoreAnimation: true,
      ignoreDimensions: true
    };
    var doc = this;
    return loadCanvg()

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Pass explicit numeric width and height in the document's units, e.g. doc.addSvgAsImage(svg, 10, 10, 200, 150).
  2. If you want the SVG's natural size, parse it yourself (svg width/height attributes, or viewBox) and pass those numbers explicitly.
  3. Coerce and validate with Number()/Number.isFinite() before the call when dimensions come from external input.
  4. Confirm both w and h are defined — omitting either one defaults to undefined -> NaN.

Example fix

// before
doc.addSvgAsImage(svgMarkup, 10, 10);     // w, h omitted -> undefined -> NaN

// after
doc.addSvgAsImage(svgMarkup, 10, 10, 200, 150);
Defensive patterns

Strategy: validation

Validate before calling

function svgNaturalSize(svgMarkup) {
  var m = svgMarkup.match(/<svg[^>]*(?:\swidth=["']([\d.]+)["'][^>]*\sheight=["']([\d.]+)["']|\sheight=["']([\d.]+)["'][^>]*\swidth=["']([\d.]+)["'])/i);
  if (!m) return null;
  return { w: parseFloat(m[1] || m[4]), h: parseFloat(m[2] || m[3]) };
}
// before calling:
var size = svgNaturalSize(svg) || { w: 200, h: 150 };
if (!Number.isFinite(size.w) || !Number.isFinite(size.h)) throw new TypeError('SVG w/h not finite');
doc.addSvgAsImage(svg, 10, 10, size.w, size.h);

Type guard

const isFiniteNumber = (v) => typeof v === 'number' && Number.isFinite(v);
// usage: if (!(isFiniteNumber(w) && isFiniteNumber(h))) { /* reject / supply default */ }

Try / catch

try {
  doc.addSvgAsImage(svg, 10, 10, w, h);
} catch (e) {
  if (/Invalid measurements \(width and\/or height\) passed to jsPDF\.addSvgAsImage/.test(e.message)) {
    console.warn('addSvgAsImage skipped: invalid w/h', { w, h });
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Calling doc.addSvgAsImage(svg, 10, 10) and omitting w/h (very common — users expect auto-sizing that is not implemented); passing dimensions as strings; computing height from an aspect ratio when one side is unknown; reading w/h from getBoundingClientRect in Node where no DOM exists.

Common situations: Assuming the SVG's own width/height attributes are honored (they are not — dimensions must be passed in); mixing up the order with addImage; using a layout calculation that returns undefined for one axis; feeding in a percentage string like '100%'.

Related errors


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