parallax/jsPDF · error · Error
Invalid coordinates passed to jsPDF.addImage
Error message
Invalid coordinates passed to jsPDF.addImage
What it means
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.
Source
Thrown at src/modules/addimage.js:837
imageData = options.imageData;
format = options.format || format || UNKNOWN;
x = options.x || x || 0;
y = options.y || y || 0;
w = options.w || options.width || w;
h = options.h || options.height || h;
alias = options.alias || alias;
compression = options.compression || compression;
rotation = options.rotation || options.angle || rotation;
}
//If compression is not explicitly set, determine if we should use compression
var filter = this.internal.getFilters();
if (compression === undefined && filter.indexOf("FlateEncode") !== -1) {
compression = "SLOW";
}
if (isNaN(x) || isNaN(y)) {
throw new Error("Invalid coordinates passed to jsPDF.addImage");
}
initialize.call(this);
var image = processImageData.call(
this,
imageData,
format,
alias,
compression
);
writeImageToPDF.call(this, x, y, w, h, image, rotation);
return this;
};
var processImageData = function(imageData, format, alias, compression) {View on GitHub (pinned to a3930ce03a)
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
Example fix
// before var x, y; // undefined doc.addImage(img, 'PNG', x, y, 100, 100); // throws // after var x = 10, y = 20; doc.addImage(img, 'PNG', x, y, 100, 100); // or with defaults: doc.addImage(img, 'PNG', x ?? 0, y ?? 0, 100, 100);
Defensive patterns
Strategy: validation
Validate before calling
// Validate coordinates before addImage
function safeAddImage(doc, img, x, y, w, h) {
x = Number(x) || 0;
y = Number(y) || 0;
if (!isFinite(x) || !isFinite(y)) {
throw new TypeError('x and y must be finite numbers');
}
doc.addImage(img, 'PNG', x, y, w, h);
} Type guard
/**
* @param {*} coord
* @returns {boolean}
*/
function isValidCoordinate(coord) {
return typeof coord === 'number' && isFinite(coord);
} Try / catch
try {
doc.addImage(img, 'PNG', x, y, w, h);
} catch (e) {
if (e.message.includes('Invalid coordinates')) {
doc.addImage(img, 'PNG', 0, 0, w, h); // default to origin
} else throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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).
Related errors
- Invalid arguments passed to jsPDF.context2d.moveTo
- Invalid arguments passed to jsPDF.context2d.lineTo
- Invalid arguments passed to jsPDF.context2d.quadraticCurveTo
- Invalid arguments passed to jsPDF.context2d.bezierCurveTo
- Invalid arguments passed to jsPDF.context2d.arc
AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13).
Data as JSON: /api/errors/ce20b836fc39c7c9.
Report an issue: GitHub.