Stirling-Tools/Stirling-PDF · warning · Error

No pages to export

Error message

No pages to export

What it means

`exportPDF` computes the export set: when `selectedOnly` is true it filters `pdfDocument.pages` by `selectedPageIds`; otherwise it uses all pages. If the result is empty it throws. So this is a precondition failure — either the document truly has no pages, or none of the supplied `selectedPageIds` match any `page.id`.

Source

Thrown at frontend/editor/src/core/services/pdfExportService.ts:43

   * Export PDF document with applied operations (single file source)
   */
  async exportPDF(
    pdfDocument: PDFDocument,
    selectedPageIds: string[] = [],
    options: ExportOptions = {},
  ): Promise<{ blob: Blob; filename: string }> {
    const { selectedOnly = false, filename } = options;

    try {
      const pagesToExport =
        selectedOnly && selectedPageIds.length > 0
          ? pdfDocument.pages.filter((page) =>
              selectedPageIds.includes(page.id),
            )
          : pdfDocument.pages;

      if (pagesToExport.length === 0) {
        throw new Error("No pages to export");
      }

      const originalPDFBytes = await pdfDocument.file.arrayBuffer();
      const blob = await this.createSingleDocument(
        originalPDFBytes,
        pagesToExport,
      );
      const exportFilename = this.generateFilename(
        filename || pdfDocument.name,
      );

      return { blob, filename: exportFilename };
    } catch (error) {
      console.error("PDF export error:", error);
      throw new Error(
        `Failed to export PDF: ${error instanceof Error ? error.message : "Unknown error"}`,
        { cause: error },
      );

View on GitHub (pinned to 9ef20dcab8)

Solutions

  1. Validate before calling export: `if (selectedOnly && selectedPageIds.every(id => !pdfDocument.pages.some(p => p.id === id)))` — surface a UI message instead of throwing.
  2. Disable the Export button in the UI when the page list is empty.
  3. When `selectedOnly` is true but `selectedPageIds` matches nothing, either fall back to exporting all pages or return a clear validation error.
  4. Ensure `selectedPageIds` uses the same type as `page.id` (string) everywhere.

Example fix

// before
if (pagesToExport.length === 0) {
  throw new Error("No pages to export");
}

// caller-side guard before invoking exportPDF
const ids = pdfDocument.pages.map(p => p.id);
const effective = selectedOnly ? selectedPageIds.filter(id => ids.includes(id)) : ids;
if (effective.length === 0) {
  return notifyUser("Select at least one page to export.");
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate before exporting
const allIds = pdfDocument.pages.map(p => p.id);
const effective = selectedOnly ? selectedPageIds.filter(id => allIds.includes(id)) : allIds;
if (effective.length === 0) {
  notifyUser("Select at least one page to export.");
  return;
}

Type guard

function hasExportablePages(doc: PDFDocument, selectedOnly: boolean, ids: string[]): boolean {
  if (!selectedOnly) return doc.pages.length > 0;
  return doc.pages.some(p => ids.includes(p.id));
}

Prevention

When it happens

Trigger: Export invoked on a freshly-created empty document; `selectedOnly:true` with `selectedPageIds` containing stale IDs that no longer exist (pages were deleted); `selectedOnly:true` with an empty `selectedPageIds` array; an ID-type mismatch (string vs number comparison) so the filter matches nothing.

Common situations: User deletes all pages then hits Export; selection state desyncs from the document after an undo/redo; the caller passes the page objects instead of their `.id` strings.

Related errors


AI-assisted analysis of Stirling-Tools/Stirling-PDF@9ef20dcab8 (2026-08-13). Data as JSON: /api/errors/1998cecf7d50636a. Report an issue: GitHub.