mozilla/pdf.js · error · FormatError

Bad domain for stiched function

Error message

Bad domain for stiched function

What it means

Thrown by PDFFunction.constructStiched (FunctionType 3) when /Domain exists but its length is not exactly 2 (i.e. inputSize !== 1). The PDF spec restricts stitching functions to a single input dimension, so any Domain with more than one interval is illegal for this function type.

Source

Thrown at src/core/function.js:299

    return function constructInterpolatedFn(src, srcOffset, dest, destOffset) {
      const x = n === 1 ? src[srcOffset] : src[srcOffset] ** n;

      for (let j = 0; j < length; ++j) {
        dest[destOffset + j] = c0[j] + x * diff[j];
      }
    };
  }

  static constructStiched(factory, dict) {
    const domain = toNumberArray(dict.getArray("Domain"));

    if (!domain) {
      throw new FormatError("No domain");
    }

    const inputSize = domain.length / 2;
    if (inputSize !== 1) {
      throw new FormatError("Bad domain for stiched function");
    }
    const { xref } = factory;

    const fns = [];
    for (const fn of dict.get("Functions")) {
      fns.push(this.parse(factory, xref.fetchIfRef(fn)));
    }

    const bounds = toNumberArray(dict.getArray("Bounds"));
    const encode = toNumberArray(dict.getArray("Encode"));
    const tmpBuf = new Float32Array(1);

    return function constructStichedFn(src, srcOffset, dest, destOffset) {
      // Clamp to domain.
      const v = MathClamp(src[srcOffset], domain[0], domain[1]);
      // calculate which bound the value is in
      const length = bounds.length;
      let i;

View on GitHub (pinned to 5903d58d58)

Solutions

  1. If you control generation, ensure stitching functions use a single-input Domain of length 2.
  2. Validate against PDF 32000-1 §7.10.4 which mandates one input dimension for Type 3 functions.
  3. Catch FormatError at render time and skip the shading.
  4. Repair/regenerate the PDF with a conformant writer (e.g. Ghostscript) and re-render.
Defensive patterns

Strategy: validation

Validate before calling

function stitchingDomainIsSingleInput(dict) {
  const d = dict.getArray?.('Domain');
  return Array.isArray(d) && d.length === 2 && d.every(Number.isFinite);
}

Type guard

function isSingleIntervalDomain(arr) {
  return Array.isArray(arr) && arr.length === 2 &&
    typeof arr[0] === 'number' && typeof arr[1] === 'number';
}

Try / catch

try { await page.render({ canvasContext }).promise; }
catch (err) {
  if (err?.name === 'FormatError' && /Bad domain for stiched/.test(err.message)) {
    console.warn('Stitching function has multi-dimensional domain; skipping.');
  } else throw err;
}

Prevention

When it happens

Trigger: A FunctionType 3 dictionary has a /Domain array whose length is not 2 (e.g. [0 1 0 1] for a 2-D function). Reached during rendering of shading/gradients that reference the stitching function.

Common situations: PDF generation tools that wrongly reuse a multi-dimensional domain on a stitching function, or hand-authored PDFs where Domain was copy-pasted from a sampled function. Surfaced when rendering gradients with multiple stops.

Related errors


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