mozilla/pdf.js · warning · FormatError

Duplicate entry in "${this._type}" tree.

Error message

Duplicate entry in "${this._type}" tree.

What it means

Thrown as a FormatError by NameOrNumberTree.getAll() while traversing Kids. A RefSet tracks visited kid references; if the same Ref appears twice, a cycle/duplicate is detected and the error fires. This prevents infinite loops from malformed PDF name/number trees with circular Kids references.

Source

Thrown at src/core/name_number_tree.js:62

    const processed = new RefSet();
    if (this.root instanceof Ref) {
      processed.put(this.root);
    }
    const queue = [this.root];
    for (const node of queue) {
      const obj = xref.fetchIfRef(node);
      if (!(obj instanceof Dict)) {
        continue;
      }
      if (obj.has("Kids")) {
        const kids = obj.get("Kids");
        if (!Array.isArray(kids)) {
          continue;
        }
        for (const kid of kids) {
          if (kid instanceof Ref) {
            if (processed.has(kid)) {
              throw new FormatError(`Duplicate entry in "${this._type}" tree.`);
            }
            processed.put(kid);
          }
          queue.push(kid);
        }
        continue;
      }
      const entries = obj.get(this._type);
      if (!Array.isArray(entries)) {
        continue;
      }
      for (let i = 0, ii = entries.length; i < ii; i += 2) {
        map.set(
          isRaw ? entries[i] : xref.fetchIfRef(entries[i]),
          isRaw ? entries[i + 1] : xref.fetchIfRef(entries[i + 1])
        );
      }
    }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Repair the PDF with a tool like qpdf --fix or Ghostscript to normalize the tree structure.
  2. If generating PDFs, ensure Kids references are unique and acyclic.
  3. If you cannot fix the source, wrap the PDF.js API call (e.g. getAttachments, getPageLabels) in a try/catch on FormatError.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const entries = nameTree.getAll();
} catch (e) {
  if (e.name === 'FormatError' && /Duplicate entry/.test(e.message)) { entries = new Map(); }
  else throw e;
}

Prevention

When it happens

Trigger: getAll() iterates tree nodes; for each node with Kids, each kid that is a Ref is checked against the processed RefSet. A second occurrence of the same Ref triggers the throw. The _type in the message is 'Names' or 'Nums' depending on the tree kind.

Common situations: A malformed PDF where a NameTree or NumberTree (used for embedded files, JavaScript, destinations, page labels, etc.) has a Kids array pointing to the same child object twice, or a genuine cycle. Produced by buggy PDF generators or by manual/corrupt editing.

Related errors


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