{"record":{"id":"93376b112d27b3c4","repo":"Stirling-Tools/Stirling-PDF","slug":"one-or-both-documents-have-no-pages","errorCode":null,"errorMessage":"One or both documents have no pages.","messagePattern":"One or both documents have no pages\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"frontend/editor/src/core/workers/pixelCompareWorker.ts","lineNumber":439,"sourceCode":"        getDocument(loaderOpts(baseBuffer)).promise,\n        getDocument(loaderOpts(comparisonBuffer)).promise,\n      ]);\n\n      const basePages = baseDoc.numPages;\n      const compPages = compDoc.numPages;\n      const sharedPages = Math.min(basePages, compPages);\n\n      if (basePages !== compPages) {\n        warnings.push(\n          formatWarning(warningTemplates.pageCountMismatch, {\n            base: basePages,\n            comparison: compPages,\n            shared: sharedPages,\n          }),\n        );\n      }\n      if (Math.max(basePages, compPages) === 0) {\n        throw new Error(warningTemplates.noPages);\n      }\n\n      // Walk every page in the longer document. Pages past the shorter document's length\n      // are processed one-sided: the missing side is rendered blank and the whole present\n      // side is treated as a diff.\n      const totalPages = Math.max(basePages, compPages);\n      const pageNumbers = Array.from({ length: totalPages }, (_, i) => i + 1);\n      const hwThreads = self.navigator?.hardwareConcurrency ?? 4;\n      const defaultConcurrency = Math.max(2, Math.min(8, hwThreads - 1));\n      const poolSize = Math.max(\n        1,\n        Math.min(concurrency ?? defaultConcurrency, totalPages),\n      );\n\n      let totalDiffPixels = 0;\n      let totalPixelsCount = 0;\n      let pagesWithChanges = 0;\n","sourceCodeStart":421,"sourceCodeEnd":457,"githubUrl":"https://github.com/Stirling-Tools/Stirling-PDF/blob/9ef20dcab80b85041912f045e17a6aea1d08f969/frontend/editor/src/core/workers/pixelCompareWorker.ts#L421-L457","documentation":"Thrown in the compare handler when Math.max(basePages, compPages) === 0, i.e. both PDFs report zero pages. pdfjs loaded both documents (no password error) but found no renderable pages. This rejects the comparison because there is nothing to compare; a page-count mismatch alone only produces a warning, but zero-on-both is fatal.","triggerScenarios":"Both inputs are valid PDF files with empty page trees (e.g. a PDF container with no pages); corrupt PDFs that parse but expose numPages=0; a non-PDF file mislabeled and partially parsed to 0 pages.","commonSituations":"User selects placeholder/empty PDFs; a generator emitted a valid header but no page objects; one of the inputs is actually an image or other format that pdfjs loaded as 0 pages.","solutions":["Validate each file's page count (numPages > 0) before queuing the comparison.","Reject empty/corrupt PDFs at upload with a clear message instead of starting the worker.","Confirm MIME type is application/pdf and that pdfjs reports a positive numPages.","If only one PDF is empty, treat the comparison as fully-different rather than failing."],"exampleFix":"// before\nif (Math.max(basePages, compPages) === 0) {\n  throw new Error(warningTemplates.noPages);\n}\n\n// after\nif (basePages === 0 && compPages === 0) {\n  throw new Error(\"Both documents have no pages. Select valid PDFs containing at least one page.\");\n}\nif (Math.max(basePages, compPages) === 0) {\n  throw new Error(warningTemplates.noPages);\n}","handlingStrategy":"validation","validationCode":"function hasPages(doc: PDFDocumentProxy): boolean {\n  return typeof doc.numPages === \"number\" && doc.numPages > 0;\n}\n\nif (!hasPages(baseDoc) && !hasPages(compDoc)) {\n  throw new Error(\"Both documents have no pages. Select PDFs with at least one page.\");\n}","typeGuard":"const hasPages = (doc: PDFDocumentProxy | null): doc is PDFDocumentProxy =>\n  !!doc && typeof doc.numPages === \"number\" && doc.numPages > 0;","tryCatchPattern":null,"preventionTips":["Validate numPages > 0 for each PDF before queuing the comparison.","Reject empty/corrupt PDFs at upload with a clear message.","Confirm the file is application/pdf and pdfjs reports a positive numPages."],"tags":["pdfjs","validation","pixel-compare","empty-document","user-input"],"backgroundTag":null,"analyzedSha":"9ef20dcab80b85041912f045e17a6aea1d08f969","analyzedAt":"2026-08-13T22:11:39.827Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}