mozilla/pdf.js · error · FormatError

WhitePoint missing - required for color space CalGray

Error message

WhitePoint missing - required for color space CalGray

What it means

Thrown by the CalGrayCS constructor when the /WhitePoint array is missing or falsy for a /CalGray color space. Per the PDF spec WhitePoint is mandatory for CalGray calibration, and pdf.js refuses to invent a default because the resulting color would be wrong.

Source

Thrown at src/core/colorspace.js:817

    }
  }

  getOutputLength(inputLength, alpha01) {
    return ((inputLength / 4) * (3 + alpha01)) | 0;
  }
}

/**
 * CalGrayCS: Based on "PDF Reference, Sixth Ed", p.245
 *
 * The default color is `new Float32Array([0])`.
 */
class CalGrayCS extends ColorSpace {
  constructor(whitePoint, blackPoint, gamma) {
    super("CalGray", 1);

    if (!whitePoint) {
      throw new FormatError(
        "WhitePoint missing - required for color space CalGray"
      );
    }
    // Translate arguments to spec variables.
    [this.XW, this.YW, this.ZW] = whitePoint;
    [this.XB, this.YB, this.ZB] = blackPoint || [0, 0, 0];
    this.G = gamma || 1;

    // Validate variables as per spec.
    if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
      throw new FormatError(
        `Invalid WhitePoint components for ${this.name}, no fallback available`
      );
    }

    if (this.XB < 0 || this.YB < 0 || this.ZB < 0) {
      info(`Invalid BlackPoint for ${this.name}, falling back to default.`);
      this.XB = this.YB = this.ZB = 0;

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Repair the PDF to include a valid /WhitePoint array (commonly [X, 1, Z] with Y normalized to 1).
  2. Update pdf.js.
Defensive patterns

Strategy: try-catch

Type guard

function hasWhitePoint(csDict) {
  return Array.isArray(csDict.get('WhitePoint')) && csDict.get('WhitePoint').length === 3;
}

Try / catch

try {
  cs = ColorSpace.parse(res, xref, cs, pdfFunctionFactory);
} catch (e) {
  if (e instanceof FormatError && /WhitePoint missing.*CalGray/.test(e.message)) {
    console.warn('CalGray missing WhitePoint, falling back', e);
    cs = null;
  } else throw e;
}

Prevention

When it happens

Trigger: Constructing CalGrayCS with whitePoint = null/undefined/[], or parsing a PDF /CalGray dictionary that lacks a /WhitePoint entry.

Common situations: Corrupt PDF missing the /WhitePoint array; a faulty color-profile extractor; hand-authored PDF with an incomplete CalGray dict.

Related errors


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