{"record":{"id":"ce20b836fc39c7c9","repo":"parallax/jsPDF","slug":"invalid-coordinates-passed-to-jspdf-addimage","errorCode":null,"errorMessage":"Invalid coordinates passed to jsPDF.addImage","messagePattern":"Invalid coordinates passed to jsPDF\\.addImage","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/addimage.js","lineNumber":837,"sourceCode":"      imageData = options.imageData;\n      format = options.format || format || UNKNOWN;\n      x = options.x || x || 0;\n      y = options.y || y || 0;\n      w = options.w || options.width || w;\n      h = options.h || options.height || h;\n      alias = options.alias || alias;\n      compression = options.compression || compression;\n      rotation = options.rotation || options.angle || rotation;\n    }\n\n    //If compression is not explicitly set, determine if we should use compression\n    var filter = this.internal.getFilters();\n    if (compression === undefined && filter.indexOf(\"FlateEncode\") !== -1) {\n      compression = \"SLOW\";\n    }\n\n    if (isNaN(x) || isNaN(y)) {\n      throw new Error(\"Invalid coordinates passed to jsPDF.addImage\");\n    }\n\n    initialize.call(this);\n\n    var image = processImageData.call(\n      this,\n      imageData,\n      format,\n      alias,\n      compression\n    );\n\n    writeImageToPDF.call(this, x, y, w, h, image, rotation);\n\n    return this;\n  };\n\n  var processImageData = function(imageData, format, alias, compression) {","sourceCodeStart":819,"sourceCodeEnd":855,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/addimage.js#L819-L855","documentation":"addImage validates the x and y position coordinates with isNaN() before placing the image on the page. NaN coordinates are typically the result of undefined variables, failed parseFloat, or arithmetic on undefined values. The check prevents writing corrupt coordinate data to the PDF output stream which would produce a malformed document.","triggerScenarios":"Passing undefined for x or y (e.g., doc.addImage(img, 'PNG', undefined, 10)). Passing a variable that was never assigned. Using parseFloat on a non-numeric string. Passing null for coordinates. Using object-form options where x/y are missing.","commonSituations":"Destructuring variables from an object where x or y keys don't exist. Reading coordinates from DOM elements (getBoundingClientRect) that return NaN due to display:none. Passing options object with wrong key names (e.g., left/top instead of x/y).","solutions":["Ensure x and y are finite numbers: doc.addImage(img, 'PNG', Number(x) || 0, Number(y) || 0, w, h)","Check for undefined before the call: if (isNaN(x) || isNaN(y)) throw new Error('Position must be a number')","When using the options object form, verify key names: { imageData, format, x, y, w, h } not { left, top }","Initialize position variables to 0 as a default rather than leaving them undefined"],"exampleFix":"// before\nvar x, y; // undefined\ndoc.addImage(img, 'PNG', x, y, 100, 100); // throws\n\n// after\nvar x = 10, y = 20;\ndoc.addImage(img, 'PNG', x, y, 100, 100);\n// or with defaults:\ndoc.addImage(img, 'PNG', x ?? 0, y ?? 0, 100, 100);","handlingStrategy":"validation","validationCode":"// Validate coordinates before addImage\nfunction safeAddImage(doc, img, x, y, w, h) {\n  x = Number(x) || 0;\n  y = Number(y) || 0;\n  if (!isFinite(x) || !isFinite(y)) {\n    throw new TypeError('x and y must be finite numbers');\n  }\n  doc.addImage(img, 'PNG', x, y, w, h);\n}","typeGuard":"/**\n * @param {*} coord\n * @returns {boolean}\n */\nfunction isValidCoordinate(coord) {\n  return typeof coord === 'number' && isFinite(coord);\n}","tryCatchPattern":"try {\n  doc.addImage(img, 'PNG', x, y, w, h);\n} catch (e) {\n  if (e.message.includes('Invalid coordinates')) {\n    doc.addImage(img, 'PNG', 0, 0, w, h); // default to origin\n  } else throw e;\n}","preventionTips":["Initialize position variables to 0 rather than leaving them undefined","Use Number(x) || 0 for defensive coercion of coordinate inputs","When using options object form, verify x and y keys exist"],"tags":["addimage","coordinates","nan","validation"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}