mozilla/pdf.js · error · Error

extractPages: invalid page index.

Error message

extractPages: invalid page index.

What it means

Plain Error thrown by reservePageSlot in extractPages when a computed or supplied newPageIndex is not an integer or is negative. Page slots must be non-negative integers because they index into the oldPages array of the new document.

Source

Thrown at src/core/editor/pdf_editor.js:991

   *  the annotations.
   * @param {WorkerTask} task - The worker task to use for reporting progress
   *  and cancellation.
   * @return {Promise<void>}
   */
  async extractPages(
    pageInfos,
    annotationStorage,
    primaryDocument,
    handler,
    task
  ) {
    this.#primaryDocument = primaryDocument;
    pageInfos = this.#resolveInsertAfterIndices(pageInfos);
    const promises = [];
    let newIndex = 0;
    const reservePageSlot = newPageIndex => {
      if (!Number.isInteger(newPageIndex) || newPageIndex < 0) {
        throw new Error("extractPages: invalid page index.");
      }
      if (this.oldPages[newPageIndex] !== undefined) {
        throw new Error("extractPages: overlapping pageIndices.");
      }
      // Reserve the slot immediately because page/image collection can be
      // async.
      this.oldPages[newPageIndex] = null;
    };
    const allDocumentData = [];

    if (annotationStorage) {
      this.#newAnnotationsParams = {
        handler,
        task,
        newAnnotationsByPage: getNewAnnotationsMap(annotationStorage),
        imagesPromises: AnnotationFactory.generateImages(
          annotationStorage.values(),
          this.xrefWrapper,

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Validate every pageIndex with Number.isInteger(x) && x >= 0 before building pageInfos.
  2. Sanitize user-supplied insertAfter values to integers >= -1.
  3. Add a unit test that feeds edge-case pageIndices to your pageInfo builder.

Example fix

// before
pageInfos = [{ document, pageIndices: [0.5, -1, 2] }];

// after
const idx = [0.5, -1, 2].filter(x => Number.isInteger(x) && x >= 0);
if (idx.length !== 3) throw new Error('pageIndices must be non-negative integers');
pageInfos = [{ document, pageIndices: idx }];
Defensive patterns

Strategy: validation

Validate before calling

function assertValidPageIndices(indices) {
  for (const idx of indices) {
    if (!Number.isInteger(idx) || idx < 0) {
      throw new Error(`Invalid pageIndex: ${idx}`);
    }
  }
}

Type guard

function isValidPageIndex(x) {
  return Number.isInteger(x) && x >= 0;
}

Try / catch

try {
  await editor.extractPages(pageInfos, ...);
} catch (e) {
  if (e.message === 'extractPages: invalid page index.') {
    pageInfos.forEach(p => p.pageIndices?.forEach(i => console.warn('bad index', i)));
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing pageIndices containing a non-integer (NaN, float, string-coerced) or a negative value; an internal arithmetic bug producing NaN; an insertAfter value of -2 or lower combined with offset math that goes negative.

Common situations: Caller builds pageIndices from user input without validation; floating-point off-by-one from index calculations; off-by-one in insertAfter resolution.

Related errors


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