mozilla/pdf.js · warning · TypeError
Invalid function in AFSimple_Calculate
Error message
Invalid function in AFSimple_Calculate
What it means
Thrown as a TypeError by AFSimple_Calculate when cFunction is not a key of its internal actions map (AVG/SUM/PRD/MIN/MAX). Note this is a TypeError, not a plain Error, because the function guards the actions lookup before iterating fields. It runs in the sandboxed PDF-JS environment.
Source
Thrown at src/scripting_api/aform.js:376
return Math.min(value1, value2);
case "MAX":
return Math.max(value1, value2);
}
throw new Error("Invalid cFunction in AFSimple");
}
AFSimple_Calculate(cFunction, cFields) {
const actions = {
AVG: args => Math.sumPrecise(args) / args.length,
SUM: args => Math.sumPrecise(args),
PRD: args => args.reduce((acc, value) => acc * value, 1),
MIN: args => Math.min(...args),
MAX: args => Math.max(...args),
};
if (!(cFunction in actions)) {
throw new TypeError("Invalid function in AFSimple_Calculate");
}
const event = globalThis.event;
const values = [];
cFields = this.AFMakeArrayFromList(cFields);
for (const cField of cFields) {
const field = this._document.getField(cField);
if (!field) {
continue;
}
for (const child of field.getArray()) {
const number = this.AFMakeNumber(child.value);
values.push(number ?? 0);
}
}
if (values.length === 0) {View on GitHub (pinned to 5903d58d58)
Solutions
- Repair or report the PDF form script.
- Update PDF.js for broader function support.
- Catch TypeError separately in the embedding app if you inspect scripting errors.
- If authoring the PDF, use only AVG/SUM/PRD/MIN/MAX.
Defensive patterns
Strategy: try-catch
Type guard
function isSupportedAFormCalculateFn(fn) {
return ['AVG', 'SUM', 'PRD', 'MIN', 'MAX'].includes(fn);
} Try / catch
try {
await runFormScript(...);
} catch (e) {
if (e instanceof TypeError && /AFSimple_Calculate/.test(e.message)) { /* skip */ }
} Prevention
- Catch TypeError distinctly from Error when inspecting sandbox failures.
- Repair PDFs that reference unsupported calculate functions.
- Update PDF.js for broader function support.
- Keep the viewer resilient to per-field scripting failures.
When it happens
Trigger: A PDF form script calls AFSimple_Calculate(cFunction, cFields) with cFunction not present in the actions map.
Common situations: A malformed or non-standard calculate action in the PDF; an authoring tool emitting an unsupported function name.
Related errors
- Invalid nValue1 in AFSimple
- Invalid nValue2 in AFSimple
- Invalid cFunction in AFSimple
- Invalid nDec value in AFPercent_Format
- Invalid psf in AFSpecial_Format
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/5e1941bed75985f6.
Report an issue: GitHub.