mozilla/pdf.js · error · Error

extractPages: partial pageIndices cannot be combined with in

Error message

extractPages: partial pageIndices cannot be combined with insertAfter entries.

What it means

Plain Error thrown by #resolveInsertAfterIndices during extractPages when at least one pageInfo uses insertAfter and another uses a partial pageIndices (fewer entries than the document's filtered page count). insertAfter relies on auto-filling sequential slots, which races with the explicit slot assignment partial pageIndices performs; the two cannot be combined deterministically.

Source

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

        insertAfterList.push({ i, insertAfter: info.insertAfter, count });
      }
    }
    if (insertAfterList.length === 0) {
      return pageInfos;
    }

    const hasContent = info => !!(info.document || info.image);

    // Partial pageIndices rely on auto-fill in extractPages, which races with
    // the slots insertAfter assigns here.
    for (let i = 0; i < pageInfos.length; i++) {
      const info = pageInfos[i];
      if (
        hasContent(info) &&
        info.pageIndices &&
        info.pageIndices.length < counts[i]
      ) {
        throw new Error(
          "extractPages: partial pageIndices cannot be combined with insertAfter entries."
        );
      }
    }

    insertAfterList.sort((a, b) => a.insertAfter - b.insertAfter || a.i - b.i);

    // If there is no base sequential sequence, resolve insertAfter against the
    // explicit layout. Shift pageIndices values but keep their array order:
    // extractPages maps each filtered source page to the corresponding
    // pageIndices entry.
    if (
      sequence.length === 0 &&
      pageInfos.some(info => hasContent(info) && info.pageIndices)
    ) {
      const updatedPageInfos = pageInfos.slice();
      let maxExistingPos = -1;
      for (const info of pageInfos) {

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Pick one placement strategy: convert all entries to explicit pageIndices, or use insertAfter for all.
  2. If you need both, pre-resolve insertAfter into explicit pageIndices yourself before calling extractPages.
  3. Validate the pageInfos shape before invoking the editor API.

Example fix

// before
pageInfos = [
  { document, pageIndices: [0] },           // partial
  { document, insertAfter: 2 },              // incompatible
];

// after — use explicit pageIndices everywhere
pageInfos = [
  { document, pageIndices: [0] },
  { document, pageIndices: [3] },            // pre-computed target slot
];
Defensive patterns

Strategy: validation

Validate before calling

function normalizePageInfos(pageInfos) {
  const usesInsertAfter = pageInfos.some(p => p.insertAfter !== undefined);
  const partial = pageInfos.some(p => p.pageIndices && p.pageIndices.length < (p.document?.numPages ?? 1));
  if (usesInsertAfter && partial) {
    throw new Error('Cannot mix insertAfter with partial pageIndices; resolve all to explicit pageIndices.');
  }
}

Try / catch

try {
  await editor.extractPages(pageInfos, ...);
} catch (e) {
  if (e.message.includes('partial pageIndices cannot be combined')) {
    pageInfos = resolveAllToExplicitIndices(pageInfos);
    return editor.extractPages(pageInfos, ...);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling the editor/extractPages API (used by PDFDocumentProxy.copyPages / insertPage-style operations) with a pageInfos array where one entry has insertAfter set and another has pageIndices shorter than its filtered page count.

Common situations: Custom integrations building new documents from multiple sources where some inserts use insertAfter and others use partial pageIndices.

Related errors


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