parallax/jsPDF · error · Error

Invalid arguments passed to jsPDF.context2d.fillRect

Error message

Invalid arguments passed to jsPDF.context2d.fillRect

What it means

fillRect runs the same four-argument isNaN guard as rect, throwing 'Invalid arguments passed to jsPDF.context2d.fillRect' before any fill work or transparency check happens. Note the guard fires even when the fill style is transparent (the early-return for isFillTransparent sits AFTER the validation), so you cannot rely on transparency to suppress the error.

Source

Thrown at src/modules/context2d.js:1078

    this.lineTo(x + w, y);
    this.lineTo(x, y);
  };

  /**
   * Draws a "filled" rectangle
   *
   * @name fillRect
   * @function
   * @param x {Number} The x-coordinate of the upper-left corner of the rectangle
   * @param y {Number} The y-coordinate of the upper-left corner of the rectangle
   * @param w {Number} The width of the rectangle, in pixels
   * @param h {Number} The height of the rectangle, in pixels
   * @description The fillRect() method draws a "filled" rectangle. The default color of the fill is black.
   */
  Context2D.prototype.fillRect = function(x, y, w, h) {
    if (isNaN(x) || isNaN(y) || isNaN(w) || isNaN(h)) {
      console.error("jsPDF.context2d.fillRect: Invalid arguments", arguments);
      throw new Error("Invalid arguments passed to jsPDF.context2d.fillRect");
    }
    if (isFillTransparent.call(this)) {
      return;
    }
    var tmp = {};
    if (this.lineCap !== "butt") {
      tmp.lineCap = this.lineCap;
      this.lineCap = "butt";
    }
    if (this.lineJoin !== "miter") {
      tmp.lineJoin = this.lineJoin;
      this.lineJoin = "miter";
    }

    this.beginPath();
    this.rect(x, y, w, h);
    this.fill();

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Coerce all four args to finite numbers and default to 0: const f = [x,y,w,h].map(Number); ctx.fillRect(...f);
  2. Skip the fillRect call when dimensions are not finite (Number.isFinite check).
  3. Log the offending values upstream so missing config is visible before jsPDF is reached.

Example fix

// before
ctx.fillRect(rect.left, rect.top, rect.width, rect.height);

// after
const [fx, fy, fw, fh] = [rect.left, rect.top, rect.width, rect.height].map(Number);
if ([fx, fy, fw, fh].every(Number.isFinite)) ctx.fillRect(fx, fy, fw, fh);
Defensive patterns

Strategy: validation

Validate before calling

function safeFillRect(ctx, x, y, w, h) {
  const a = [x, y, w, h].map(Number);
  if (!a.every(Number.isFinite)) return;
  ctx.fillRect(...a);
}

Type guard

const isFinite4 = (x,y,w,h) => [x,y,w,h].every(v => typeof v === 'number' && Number.isFinite(v));

Try / catch

try { ctx.fillRect(x, y, w, h); }
catch (e) { if (!/context2d.fillRect/.test(e.message)) throw e; /* else log+skip */ }

Prevention

When it happens

Trigger: ctx.fillRect(x, y, w, h) with any argument undefined, missing, or non-numeric; computing width/height from an empty container or zero-divisor.

Common situations: Rendering reports where some rows have no metric (undefined -> NaN); responsive layouts measuring an element before layout completes (w=NaN); passing stringified numbers from JSON config.

Related errors


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