{"record":{"id":"cadd0c18d4f9e9d6","repo":"parallax/jsPDF","slug":"invalid-measurements-width-and-or-height-passed","errorCode":null,"errorMessage":"Invalid measurements (width and/or height) passed to jsPDF.addSvgAsImage","messagePattern":"Invalid measurements \\(width and/or height\\) passed to jsPDF\\.addSvgAsImage","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/svg.js","lineNumber":114,"sourceCode":"   */\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,\n      ignoreAnimation: true,\n      ignoreDimensions: true\n    };\n    var doc = this;\n    return loadCanvg()","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/svg.js#L96-L132","documentation":"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.","triggerScenarios":"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.","commonSituations":"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%'.","solutions":["Pass explicit numeric width and height in the document's units, e.g. doc.addSvgAsImage(svg, 10, 10, 200, 150).","If you want the SVG's natural size, parse it yourself (svg width/height attributes, or viewBox) and pass those numbers explicitly.","Coerce and validate with Number()/Number.isFinite() before the call when dimensions come from external input.","Confirm both w and h are defined — omitting either one defaults to undefined -> NaN."],"exampleFix":"// before\ndoc.addSvgAsImage(svgMarkup, 10, 10);     // w, h omitted -> undefined -> NaN\n\n// after\ndoc.addSvgAsImage(svgMarkup, 10, 10, 200, 150);","handlingStrategy":"validation","validationCode":"function svgNaturalSize(svgMarkup) {\n  var m = svgMarkup.match(/<svg[^>]*(?:\\swidth=[\"']([\\d.]+)[\"'][^>]*\\sheight=[\"']([\\d.]+)[\"']|\\sheight=[\"']([\\d.]+)[\"'][^>]*\\swidth=[\"']([\\d.]+)[\"'])/i);\n  if (!m) return null;\n  return { w: parseFloat(m[1] || m[4]), h: parseFloat(m[2] || m[3]) };\n}\n// before calling:\nvar size = svgNaturalSize(svg) || { w: 200, h: 150 };\nif (!Number.isFinite(size.w) || !Number.isFinite(size.h)) throw new TypeError('SVG w/h not finite');\ndoc.addSvgAsImage(svg, 10, 10, size.w, size.h);","typeGuard":"const isFiniteNumber = (v) => typeof v === 'number' && Number.isFinite(v);\n// usage: if (!(isFiniteNumber(w) && isFiniteNumber(h))) { /* reject / supply default */ }","tryCatchPattern":"try {\n  doc.addSvgAsImage(svg, 10, 10, w, h);\n} catch (e) {\n  if (/Invalid measurements \\(width and\\/or height\\) passed to jsPDF\\.addSvgAsImage/.test(e.message)) {\n    console.warn('addSvgAsImage skipped: invalid w/h', { w, h });\n  } else {\n    throw e;\n  }\n}","preventionTips":["Never assume addSvgAsImage auto-sizes from the SVG — always pass explicit w and h.","Extract dimensions from the SVG's width/height/viewBox yourself and pass them as numbers.","Use Number.isFinite to reject undefined, NaN, Infinity, and strings at once.","Wrap addSvgAsImage in a helper that requires all four numeric dimensions."],"tags":["svg","validation","dimensions","jspdf","numeric"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}