{"record":{"id":"1998cecf7d50636a","repo":"Stirling-Tools/Stirling-PDF","slug":"no-pages-to-export","errorCode":null,"errorMessage":"No pages to export","messagePattern":"No pages to export","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"frontend/editor/src/core/services/pdfExportService.ts","lineNumber":43,"sourceCode":"   * Export PDF document with applied operations (single file source)\n   */\n  async exportPDF(\n    pdfDocument: PDFDocument,\n    selectedPageIds: string[] = [],\n    options: ExportOptions = {},\n  ): Promise<{ blob: Blob; filename: string }> {\n    const { selectedOnly = false, filename } = options;\n\n    try {\n      const pagesToExport =\n        selectedOnly && selectedPageIds.length > 0\n          ? pdfDocument.pages.filter((page) =>\n              selectedPageIds.includes(page.id),\n            )\n          : pdfDocument.pages;\n\n      if (pagesToExport.length === 0) {\n        throw new Error(\"No pages to export\");\n      }\n\n      const originalPDFBytes = await pdfDocument.file.arrayBuffer();\n      const blob = await this.createSingleDocument(\n        originalPDFBytes,\n        pagesToExport,\n      );\n      const exportFilename = this.generateFilename(\n        filename || pdfDocument.name,\n      );\n\n      return { blob, filename: exportFilename };\n    } catch (error) {\n      console.error(\"PDF export error:\", error);\n      throw new Error(\n        `Failed to export PDF: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n        { cause: error },\n      );","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/Stirling-Tools/Stirling-PDF/blob/9ef20dcab80b85041912f045e17a6aea1d08f969/frontend/editor/src/core/services/pdfExportService.ts#L25-L61","documentation":"`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`.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate before calling export: `if (selectedOnly && selectedPageIds.every(id => !pdfDocument.pages.some(p => p.id === id)))` — surface a UI message instead of throwing.","Disable the Export button in the UI when the page list is empty.","When `selectedOnly` is true but `selectedPageIds` matches nothing, either fall back to exporting all pages or return a clear validation error.","Ensure `selectedPageIds` uses the same type as `page.id` (string) everywhere."],"exampleFix":"// before\nif (pagesToExport.length === 0) {\n  throw new Error(\"No pages to export\");\n}\n\n// caller-side guard before invoking exportPDF\nconst ids = pdfDocument.pages.map(p => p.id);\nconst effective = selectedOnly ? selectedPageIds.filter(id => ids.includes(id)) : ids;\nif (effective.length === 0) {\n  return notifyUser(\"Select at least one page to export.\");\n}","handlingStrategy":"validation","validationCode":"// Validate before exporting\nconst allIds = pdfDocument.pages.map(p => p.id);\nconst effective = selectedOnly ? selectedPageIds.filter(id => allIds.includes(id)) : allIds;\nif (effective.length === 0) {\n  notifyUser(\"Select at least one page to export.\");\n  return;\n}","typeGuard":"function hasExportablePages(doc: PDFDocument, selectedOnly: boolean, ids: string[]): boolean {\n  if (!selectedOnly) return doc.pages.length > 0;\n  return doc.pages.some(p => ids.includes(p.id));\n}","tryCatchPattern":null,"preventionTips":["Disable the Export button when the page list is empty.","When selectedOnly is true, verify selectedPageIds match at least one page.id before exporting.","Keep selectedPageIds and page.id types consistent (string) to avoid silent filter misses.","Re-derive selection from the live document rather than trusting cached IDs."],"tags":["export","validation","pdf","page-selection"],"backgroundTag":null,"analyzedSha":"9ef20dcab80b85041912f045e17a6aea1d08f969","analyzedAt":"2026-08-13T22:11:39.827Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}