mozilla/pdf.js · error · FormatError

Unknown function type

Error message

Unknown function type: ${typeNum}

What it means

Thrown by PDFFunctionFactory when a PDF function dictionary's /FunctionType is not one of the four supported values (0=SAMPLED, 2=EXPONENTIAL_INTERPOLATION, 3=STITCHING, 4=POSTSCRIPT_CALCULATOR). The switch in PDFFunction.parse falls through and aborts construction of the function object, which PDF.js uses for gradients, color mappings, and transfer functions.

Solutions

  1. Confirm the PDF is valid by opening it in Acrobat; if it renders there, attach the file to a pdf.js issue since the parser may need to tolerate the value.
  2. If you control the PDF generation, ensure every function object sets /FunctionType to 0, 2, 3, or 4 per PDF 32000-1 §7.10.
  3. Wrap page rendering in a try/catch and degrade gracefully (skip the offending annotation/shading) rather than aborting the whole render.
  4. Pre-validate with a PDF/A conformance checker which flags non-standard function types.

Example fix

// before: rendering a page aborts on a bad function type
await page.render({ canvasContext }).promise;

// after: tolerate per-shading failures
try {
  await page.render({ canvasContext }).promise;
} catch (err) {
  if (err?.message?.startsWith('Unknown function type:')) {
    console.warn('Skipping page with unsupported PDF function:', err.message);
  } else throw err;
}
Defensive patterns

Strategy: try-catch

Validate before calling

// No public pre-check exists; PDFFunctionFactory is internal to the worker.
// If you parse PDFs yourself, validate before passing to pdf.js:
function isValidFunctionType(dict) {
  const t = dict.get('FunctionType');
  return [0, 2, 3, 4].includes(t);
}

Type guard

function isKnownFunctionType(typeNum) {
  return typeNum === 0 || typeNum === 2 || typeNum === 3 || typeNum === 4;
}

Try / catch

try {
  await page.render({ canvasContext }).promise;
} catch (err) {
  if (err?.message?.startsWith('Unknown function type:')) {
    console.warn('Unsupported PDF function type; skipping page.');
  } else throw err;
}

Prevention

When it happens

Trigger: A PDF contains a function stream/dict whose FunctionType entry is missing, null, undefined, or any integer other than 0/2/3/4 (e.g. 1, 5, or a non-integer). Reached via getDocument() rendering of any page using shading patterns, Indexed color spaces with lookup functions, or halftone/transfer functions.

Common situations: Encountered with malformed or writer-buggy PDFs that emit a reserved/invalid function type, or with PDFs that omit /FunctionType entirely. Also seen when a corrupt xref causes dict.get('FunctionType') to resolve to garbage.

Related errors


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

Appendix: source

Thrown at src/core/function.js:145

    }
    return array;
  }

  static parse(factory, fn) {
    const dict = fn.dict || fn;
    const typeNum = dict.get("FunctionType");

    switch (typeNum) {
      case FunctionType.SAMPLED:
        return this.constructSampled(factory, fn, dict);
      case FunctionType.EXPONENTIAL_INTERPOLATION:
        return this.constructInterpolated(factory, dict);
      case FunctionType.STITCHING:
        return this.constructStiched(factory, dict);
      case FunctionType.POSTSCRIPT_CALCULATOR:
        return this.constructPostScript(factory, fn, dict);
    }
    throw new FormatError(`Unknown function type: ${typeNum}`);
  }

  static parseArray(factory, fnObj) {
    const { xref } = factory;

    const fnArray = [];
    for (const fn of fnObj) {
      fnArray.push(this.parse(factory, xref.fetchIfRef(fn)));
    }
    return function (src, srcOffset, dest, destOffset) {
      for (let i = 0, ii = fnArray.length; i < ii; i++) {
        fnArray[i](src, srcOffset, dest, destOffset + i);
      }
    };
  }

  static constructSampled(factory, fn, dict) {
    // See chapter 3, page 109 of the PDF reference

View on GitHub (pinned to 5903d58d58)