{"record":{"id":"691ad4084efe3805","repo":"mozilla/pdf.js","slug":"unknown-function-type-typenum","errorCode":null,"errorMessage":"Unknown function type: ${typeNum}","messagePattern":"Unknown function type: (.+?)","errorType":"exception","errorClass":"FormatError","httpStatus":null,"severity":"error","filePath":"src/core/function.js","lineNumber":145,"sourceCode":"    }\n    return array;\n  }\n\n  static parse(factory, fn) {\n    const dict = fn.dict || fn;\n    const typeNum = dict.get(\"FunctionType\");\n\n    switch (typeNum) {\n      case FunctionType.SAMPLED:\n        return this.constructSampled(factory, fn, dict);\n      case FunctionType.EXPONENTIAL_INTERPOLATION:\n        return this.constructInterpolated(factory, dict);\n      case FunctionType.STITCHING:\n        return this.constructStiched(factory, dict);\n      case FunctionType.POSTSCRIPT_CALCULATOR:\n        return this.constructPostScript(factory, fn, dict);\n    }\n    throw new FormatError(`Unknown function type: ${typeNum}`);\n  }\n\n  static parseArray(factory, fnObj) {\n    const { xref } = factory;\n\n    const fnArray = [];\n    for (const fn of fnObj) {\n      fnArray.push(this.parse(factory, xref.fetchIfRef(fn)));\n    }\n    return function (src, srcOffset, dest, destOffset) {\n      for (let i = 0, ii = fnArray.length; i < ii; i++) {\n        fnArray[i](src, srcOffset, dest, destOffset + i);\n      }\n    };\n  }\n\n  static constructSampled(factory, fn, dict) {\n    // See chapter 3, page 109 of the PDF reference","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/function.js#L127-L163","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","If you control the PDF generation, ensure every function object sets /FunctionType to 0, 2, 3, or 4 per PDF 32000-1 §7.10.","Wrap page rendering in a try/catch and degrade gracefully (skip the offending annotation/shading) rather than aborting the whole render.","Pre-validate with a PDF/A conformance checker which flags non-standard function types."],"exampleFix":"// before: rendering a page aborts on a bad function type\nawait page.render({ canvasContext }).promise;\n\n// after: tolerate per-shading failures\ntry {\n  await page.render({ canvasContext }).promise;\n} catch (err) {\n  if (err?.message?.startsWith('Unknown function type:')) {\n    console.warn('Skipping page with unsupported PDF function:', err.message);\n  } else throw err;\n}","handlingStrategy":"try-catch","validationCode":"// No public pre-check exists; PDFFunctionFactory is internal to the worker.\n// If you parse PDFs yourself, validate before passing to pdf.js:\nfunction isValidFunctionType(dict) {\n  const t = dict.get('FunctionType');\n  return [0, 2, 3, 4].includes(t);\n}","typeGuard":"function isKnownFunctionType(typeNum) {\n  return typeNum === 0 || typeNum === 2 || typeNum === 3 || typeNum === 4;\n}","tryCatchPattern":"try {\n  await page.render({ canvasContext }).promise;\n} catch (err) {\n  if (err?.message?.startsWith('Unknown function type:')) {\n    console.warn('Unsupported PDF function type; skipping page.');\n  } else throw err;\n}","preventionTips":["Render pages in isolated try/catch so one bad function does not abort the document.","Validate generated PDFs against PDF/A before publishing.","Report unrenderable-but-Acrobat-renderable files upstream to pdf.js."],"tags":["pdf","function","parsing","shading","malformed-pdf"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}