mozilla/pdf.js · error · Error

createImage: invalid bitmap dimensions ${width}x${height}

Error message

createImage: invalid bitmap dimensions ${width}x${height}

What it means

Plain Error thrown by createImage when the supplied bitmap's width or height is not a positive integer (zero, negative, NaN, Infinity, or fractional). The function immediately calls new OffscreenCanvas(width, height), which requires positive integer dimensions, so the guard prevents a cryptic TypeError from the platform.

Source

Thrown at src/core/editor/pdf_images.js:170

  return createRawImage(compressed, dict);
}

async function createImage(bitmap, xref, { closeBitmap = false } = {}) {
  // TODO: when printing, we could have a specific internal colorspace
  // (e.g. something like DeviceRGBA) in order avoid any conversion (i.e. no
  // jpeg, no rgba to rgb conversion, etc...)

  const { width, height } = bitmap;
  if (
    !Number.isInteger(width) ||
    !Number.isInteger(height) ||
    width <= 0 ||
    height <= 0
  ) {
    if (closeBitmap) {
      bitmap.close?.();
    }
    throw new Error(
      `createImage: invalid bitmap dimensions ${width}x${height}`
    );
  }
  const canvas = new OffscreenCanvas(width, height);
  const ctx = canvas.getContext("2d", {
    alpha: true,
    willReadFrequently: true,
  });

  let data;
  try {
    ctx.drawImage(bitmap, 0, 0);
    data = ctx.getImageData(0, 0, width, height).data;
  } finally {
    if (closeBitmap) {
      bitmap.close?.();
    }
  }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Validate bitmap.width and bitmap.height with Number.isInteger and > 0 before calling createImage.
  2. Ensure the upstream createImageBitmap / decode succeeded (check the bitmap is not closed and has positive dimensions).
  3. Catch the error and skip or substitute a placeholder image.

Example fix

// before
const pdfImage = await createImage(bitmap, xref, { closeBitmap: true });

// after
if (!Number.isInteger(bitmap.width) || !Number.isInteger(bitmap.height) || bitmap.width <= 0 || bitmap.height <= 0) {
  throw new Error(`Bitmap has invalid dimensions ${bitmap.width}x${bitmap.height}; source image may be corrupt.`);
}
const pdfImage = await createImage(bitmap, xref, { closeBitmap: true });
Defensive patterns

Strategy: validation

Validate before calling

function isValidBitmap(b) {
  return b && Number.isInteger(b.width) && Number.isInteger(b.height) && b.width > 0 && b.height > 0;
}
if (!isValidBitmap(bitmap)) {
  throw new Error(`Bitmap dimensions invalid: ${bitmap?.width}x${bitmap?.height}`);
}

Type guard

function isValidBitmap(b) {
  return b != null &&
    Number.isInteger(b.width) && b.width > 0 &&
    Number.isInteger(b.height) && b.height > 0 &&
    typeof b.close === 'function';
}

Try / catch

try {
  return await createImage(bitmap, xref, { closeBitmap: true });
} catch (e) {
  if (/invalid bitmap dimensions/.test(e.message)) {
    bitmap.close?.();
    return fallbackPlaceholderImage(xref);
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing a degenerate ImageBitmap (e.g., from createImageBitmap of an empty/corrupt source); a canvas whose backing store was lost; passing a custom bitmap-like object with NaN/Infinity dimensions; a closed ImageBitmap whose width/height read as 0.

Common situations: Decoding a corrupt image file via createImageBitmap then handing the result to createImage; memory pressure where the bitmap failed to allocate; passing width/height from user input without validation.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/16718d69c20568a2. Report an issue: GitHub.