mozilla/pdf.js · error · FormatError

Invalid image width: ${width} or height: ${height}

Error message

Invalid image width: ${width} or height: ${height}

What it means

Thrown by the PDFImage constructor when neither the image dictionary's Width/Height nor the underlying stream's width/height is a positive number, and no parent fallbackDims are available to inherit from. Width and Height are mandatory for any PDF image object (PDF 32000-1 §8.9).

Source

Thrown at src/core/image.js:159

      Number.isInteger(image.width) &&
      image.width > 0 &&
      Number.isInteger(image.height) &&
      image.height > 0 &&
      (image.width !== width || image.height !== height)
    ) {
      warn(
        "PDFImage - using the Width/Height of the image data, " +
          "rather than the image dictionary."
      );
      width = image.width;
      height = image.height;
    } else {
      const validWidth = typeof width === "number" && width > 0,
        validHeight = typeof height === "number" && height > 0;

      if (!validWidth || !validHeight) {
        if (!image.fallbackDims) {
          throw new FormatError(
            `Invalid image width: ${width} or height: ${height}`
          );
        }
        warn(
          "PDFImage - using the Width/Height of the parent image, for SMask/Mask data."
        );
        if (!validWidth) {
          width = image.fallbackDims.width;
        }
        if (!validHeight) {
          height = image.fallbackDims.height;
        }
      }
    }
    this.width = width;
    this.height = height;

    this.interpolate = dict.get("I", "Interpolate");

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Regenerate or repair the PDF so every image XObject has positive integer /Width and /Height.
  2. Run qpdf --check or mutool clean to detect and repair broken image objects.
  3. Catch FormatError at render time and skip the image while continuing the page.
  4. If the image is yours, ensure the writer emits Width/Height as required integers.
Defensive patterns

Strategy: validation

Validate before calling

// If you build image dicts yourself, validate before writing:
function imageDimsValid(w, h) {
  return Number.isInteger(w) && w > 0 && Number.isInteger(h) && h > 0;
}

Type guard

function isPositiveIntegerDimensions(width, height) {
  return Number.isInteger(width) && Number.isInteger(height) &&
    width > 0 && height > 0;
}

Try / catch

try { await page.render({ canvasContext }).promise; }
catch (err) {
  if (/Invalid image width/.test(err?.message || '')) {
    console.warn('Image missing valid Width/Height; skipping image.');
  } else throw err;
}

Prevention

When it happens

Trigger: An inline or XObject image whose /Width and/or /Height is missing, zero, negative, or non-numeric, and the image is not an SMask/Mask child (no fallbackDims). Reached while rendering any page that draws the bad image.

Common situations: Corrupt image XObjects from damaged PDFs; images whose Dimensions were lost during a copy/merge; PDFs produced by buggy scanners that omit the entries. Most common with inline images in content streams.

Related errors


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