{"record":{"id":"71531560eb32bdc1","repo":"parallax/jsPDF","slug":"invalid-coordinates-passed-to-jspdf-addsvgasimage","errorCode":null,"errorMessage":"Invalid coordinates passed to jsPDF.addSvgAsImage","messagePattern":"Invalid coordinates passed to jsPDF\\.addSvgAsImage","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/svg.js","lineNumber":109,"sourceCode":"   * @param {string} alias of SVG-Image (if used multiple times)\n   * @param {string} compression of the generated JPEG, can have the values 'NONE', 'FAST', 'MEDIUM' and 'SLOW'\n   * @param {number} rotation of the image in degrees (0-359)\n   *\n   * @returns jsPDF jsPDF-instance\n   */\n  jsPDFAPI.addSvgAsImage = function(\n    svg,\n    x,\n    y,\n    w,\n    h,\n    alias,\n    compression,\n    rotation\n  ) {\n    if (isNaN(x) || isNaN(y)) {\n      console.error(\"jsPDF.addSvgAsImage: Invalid coordinates\", arguments);\n      throw new Error(\"Invalid coordinates passed to jsPDF.addSvgAsImage\");\n    }\n\n    if (isNaN(w) || isNaN(h)) {\n      console.error(\"jsPDF.addSvgAsImage: Invalid measurements\", arguments);\n      throw new Error(\n        \"Invalid measurements (width and/or height) passed to jsPDF.addSvgAsImage\"\n      );\n    }\n\n    var canvas = document.createElement(\"canvas\");\n    canvas.width = w;\n    canvas.height = h;\n    var ctx = canvas.getContext(\"2d\");\n    ctx.fillStyle = \"#fff\"; /// set white fill style\n    ctx.fillRect(0, 0, canvas.width, canvas.height);\n\n    var options = {\n      ignoreMouse: true,","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/svg.js#L91-L127","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass explicit finite numbers for x and y, e.g. doc.addSvgAsImage(svg, 10, 10, 200, 150).","If the values come from user/config input, coerce and validate them first with Number() and Number.isFinite() before calling the API.","Re-check the argument order against the signature (svg, x, y, w, h, alias, compression, rotation) — a shifted argument is the most common cause.","If x/y are derived, log them immediately before the call to confirm they are finite numbers at runtime."],"exampleFix":"// before\ndoc.addSvgAsImage(svgMarkup);            // x, y omitted -> undefined -> NaN\n\n// after\ndoc.addSvgAsImage(svgMarkup, 10, 10, 200, 150);","handlingStrategy":"validation","validationCode":"function assertSvgCoords(svg, x, y, w, h) {\n  if (typeof svg !== 'string' || svg.trim() === '') {\n    throw new TypeError('addSvgAsImage: svg markup must be a non-empty string');\n  }\n  if (!Number.isFinite(x) || !Number.isFinite(y)) {\n    throw new TypeError('addSvgAsImage: x and y must be finite numbers (got x=' + x + ', y=' + y + ')');\n  }\n  if (!Number.isFinite(w) || !Number.isFinite(h)) {\n    throw new TypeError('addSvgAsImage: w and h must be finite numbers (got w=' + w + ', h=' + h + ')');\n  }\n}\n// call before: assertSvgCoords(svg, x, y, w, h); doc.addSvgAsImage(svg, x, y, w, h);","typeGuard":"const isFiniteNumber = (v) => typeof v === 'number' && Number.isFinite(v);\n// usage: if (!(isFiniteNumber(x) && isFiniteNumber(y))) { /* reject */ }","tryCatchPattern":"try {\n  doc.addSvgAsImage(svg, x, y, w, h);\n} catch (e) {\n  if (/Invalid coordinates passed to jsPDF\\.addSvgAsImage/.test(e.message)) {\n    console.warn('addSvgAsImage skipped: invalid coordinates', { x, y });\n  } else {\n    throw e;\n  }\n}","preventionTips":["Validate all geometry with Number.isFinite (stricter than the library's isNaN, which lets Infinity through).","Centralize addSvgAsImage behind a wrapper that coerces and checks arguments once.","Always pass all four numeric args explicitly; never rely on default/omitted values.","Keep a comment of the signature (svg, x, y, w, h, alias, compression, rotation) at the call site."],"tags":["svg","validation","coordinates","jspdf","numeric"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}