mozilla/pdf.js · error · FormatError

Invalid ${partialMsg}

Error message

Invalid ${partialMsg}

What it means

Error "Invalid ${partialMsg}" thrown in mozilla/pdf.js.

Source

Thrown at src/core/evaluator.js:5439

              argsLength++;
            }
          }

          if (argsLength < numArgs) {
            const partialMsg =
              `command ${cmd}: expected ${numArgs} args, ` +
              `but received ${argsLength} args.`;

            // Incomplete path operators, in particular, can result in fairly
            // chaotic rendering artifacts. Hence the following heuristics is
            // used to error, rather than just warn, once a number of invalid
            // path operators have been encountered (fixes bug1443140.pdf).
            if (
              this._isPathOp &&
              ++this._numInvalidPathOPS >
                EvaluatorPreprocessor.MAX_INVALID_PATH_OPS
            ) {
              throw new FormatError(`Invalid ${partialMsg}`);
            }
            // If we receive too few arguments, it's not possible to execute
            // the command, hence we skip the command.
            warn(`Skipping ${partialMsg}`);
            if (args !== null) {
              args.length = 0;
            }
            continue;
          }
        } else if (argsLength > numArgs) {
          info(
            `Command ${cmd}: expected [0, ${numArgs}] args, ` +
              `but received ${argsLength} args.`
          );
        }

        // TODO figure out how to type-check vararg functions
        this.preprocessCommand(fn, args);

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Re-export the PDF from the originating vector/illustration software with corrected path data.
  2. Run the PDF through Ghostscript (gs -sDEVICE=pdfwrite) which re-flattens and normalizes content streams.
  3. Use stopAtErrors: false (default) — but note this specific check throws regardless once the threshold is exceeded; isolate the page with per-page try/catch instead.
  4. If authoring content streams, validate operator arity against the PDF spec Table A.1 before writing.

Example fix

// before
await page.render({ canvasContext, viewport }); // throws after threshold

// after
try {
  await page.render({ canvasContext, viewport });
} catch (e) {
  if (/Invalid command/.test(e.message)) {
    console.warn('Page content stream corrupt, skipping render');
  } else throw e;
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await page.render({ canvasContext, viewport });
} catch (e) {
  if (e.name === 'FormatError' && /Invalid command.*expected.*args/.test(e.message)) {
    // too many malformed path operators — skip this page
  } else throw e;
}

Prevention

When it happens

Trigger: A content stream with many malformed path operators — e.g. repeated 'l' (lineto) tokens missing their coordinate operands, or 'c' (curveto) with fewer than 6 numbers. Once the running _numInvalidPathOPS counter exceeds EvaluatorPreprocessor.MAX_INVALID_PATH_OPS, this FormatError is thrown. Fires during operator-list preprocessing for the page.

Common situations: Vector-heavy PDFs from buggy illustration exporters, hand-authored content streams with typos, or PDFs corrupted in their content-stream region. Also seen after OCR/vectorization tools that emit degenerate path data. The 'Invalid' prefix is followed by the partial message naming the offending operator and argument counts.

Related errors


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