mozilla/pdf.js · error · FormatError

Count must be a (positive) integer.

Error message

Count must be a (positive) integer.

What it means

Thrown during getPageIndex kid counting when an intermediate /Pages node has a /Count entry that is not a non-negative integer. Intermediate nodes use /Count to declare how many leaf pages lie beneath them; an invalid count makes index computation impossible.

Source

Thrown at src/core/catalog.js:1659

        if (!(kid instanceof Ref)) {
          throw new FormatError("Kid must be a reference.");
        }
        if (isRefsEqual(kid, ref)) {
          found = true;
          break;
        }
        kidPromises.push(
          xref.fetchAsync(kid).then(obj => {
            if (!(obj instanceof Dict)) {
              throw new FormatError("Kid node must be a dictionary.");
            }
            if (obj.has("Count")) {
              const count = obj.get("Count");
              if (Number.isInteger(count) && count >= 0) {
                total += count;
                return;
              }
              throw new FormatError("Count must be a (positive) integer.");
            }
            // Page leaf node.
            total++;
          })
        );
      }
      if (!found) {
        throw new FormatError("Kid reference not found in parent's kids.");
      }
      await Promise.all(kidPromises);
      ref = parentRef;
    }

    this.pageIndexCache.put(pageRef, total);
    return total;
  }

  get baseUrl() {

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Catch the FormatError from getPageIndex and fall back to a linear page scan.
  2. Rebuild the PDF with Ghostscript/qpdf so intermediate /Count values are correct integers.
  3. Regenerate from source with a compliant PDF library.

Example fix

// before
const pageIndex = await pdfDocument.getPageIndex(pageRef);

// after
let pageIndex = -1;
try {
  pageIndex = await pdfDocument.getPageIndex(pageRef);
} catch (e) {
  for (let i = 0; i < pdfDocument.numPages; i++) {
    const page = await pdfDocument.getPage(i);
    if (page.ref && page.ref.num === pageRef.num) { pageIndex = i; break; }
  }
}
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

try {
  idx = await pdfDocument.getPageIndex(ref);
} catch (e) {
  for (let i = 0; i < pdfDocument.numPages; i++) {
    const p = await pdfDocument.getPage(i);
    if (p.ref && p.ref.num === ref.num) { idx = i; break; }
  }
}

Prevention

When it happens

Trigger: A kid node (resolved to a Dict) has a /Count entry that is a float, negative number, string, or other non-integer. The guard is `Number.isInteger(count) && count >= 0` failing while `obj.has('Count')` is true.

Common situations: Generators that write /Count as a real number or leave a placeholder value; corrupt files where /Count bytes were altered; spec-violating producers that omit or miscompute /Count on intermediate nodes.

Related errors


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