parallax/jsPDF · error · Error

Invalid coordinates passed to jsPDF.addSvgAsImage

Error message

Invalid coordinates passed to jsPDF.addSvgAsImage

What it means

jsPDF.addSvgAsImage rasterizes an SVG onto an HTMLCanvas and then stamps the resulting JPEG onto the PDF page at position (x, y). Before doing anything it asserts isNaN(x)===false && isNaN(y)===false, because a NaN position cannot be mapped onto the page coordinate space. The guard runs on the raw arguments, so undefined (omitted), null, non-numeric strings, and objects all trip it. Note isNaN(Infinity) is false, so Infinity slips past this check even though it is not a usable coordinate.

Source

Thrown at src/modules/svg.js:109

   * @param {string} alias of SVG-Image (if used multiple times)
   * @param {string} compression of the generated JPEG, can have the values 'NONE', 'FAST', 'MEDIUM' and 'SLOW'
   * @param {number} rotation of the image in degrees (0-359)
   *
   * @returns jsPDF jsPDF-instance
   */
  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,

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Pass explicit finite numbers for x and y, e.g. doc.addSvgAsImage(svg, 10, 10, 200, 150).
  2. If the values come from user/config input, coerce and validate them first with Number() and Number.isFinite() before calling the API.
  3. Re-check the argument order against the signature (svg, x, y, w, h, alias, compression, rotation) — a shifted argument is the most common cause.
  4. If x/y are derived, log them immediately before the call to confirm they are finite numbers at runtime.

Example fix

// before
doc.addSvgAsImage(svgMarkup);            // x, y omitted -> undefined -> NaN

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

Strategy: validation

Validate before calling

function assertSvgCoords(svg, x, y, w, h) {
  if (typeof svg !== 'string' || svg.trim() === '') {
    throw new TypeError('addSvgAsImage: svg markup must be a non-empty string');
  }
  if (!Number.isFinite(x) || !Number.isFinite(y)) {
    throw new TypeError('addSvgAsImage: x and y must be finite numbers (got x=' + x + ', y=' + y + ')');
  }
  if (!Number.isFinite(w) || !Number.isFinite(h)) {
    throw new TypeError('addSvgAsImage: w and h must be finite numbers (got w=' + w + ', h=' + h + ')');
  }
}
// call before: assertSvgCoords(svg, x, y, w, h); doc.addSvgAsImage(svg, x, y, w, h);

Type guard

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

Try / catch

try {
  doc.addSvgAsImage(svg, x, y, w, h);
} catch (e) {
  if (/Invalid coordinates passed to jsPDF\.addSvgAsImage/.test(e.message)) {
    console.warn('addSvgAsImage skipped: invalid coordinates', { x, y });
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Calling doc.addSvgAsImage(svg) with x/y omitted (becomes undefined -> NaN); passing coordinates as strings like "10"; shifting the argument order (e.g. forgetting the svg and passing x first); supplying x/y from a parseFloat that returned NaN; reading coordinates from a layout object whose fields are missing.

Common situations: Migrating from doc.addImage and misremembering the signature; pulling geometry from a DOM form or layout config that returns strings or empty values; computing a coordinate from a division whose denominator is zero (yields NaN); pulling values from getBoundingClientRect in an environment where the element isn't measured yet.

Related errors


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