File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -176,6 +176,24 @@ describe("PDF document extractor", () => {
176176 ) ;
177177 } ) ;
178178
179+ it ( "rejects selected pages outside the PDF page count before extraction" , async ( ) => {
180+ pdfDocument . pageCount = 1 ;
181+ pdfDocument . extract . mockResolvedValueOnce ( { text : "" , images : [ ] } ) ;
182+ const extractor = createPdfDocumentExtractor ( ) ;
183+
184+ await expect ( extractor . extract ( request ( { pageNumbers : [ 2 ] } ) ) ) . rejects . toThrow (
185+ "No requested PDF pages exist in this 1-page document." ,
186+ ) ;
187+ expect ( pdfDocument . extract ) . not . toHaveBeenCalled ( ) ;
188+ expect ( pdfDocument . destroy ) . toHaveBeenCalledTimes ( 1 ) ;
189+
190+ await expect ( extractor . extract ( request ( { pageNumbers : [ ] } ) ) ) . resolves . toEqual ( {
191+ text : "" ,
192+ images : [ ] ,
193+ } ) ;
194+ expect ( pdfDocument . destroy ) . toHaveBeenCalledTimes ( 2 ) ;
195+ } ) ;
196+
179197 it ( "reports image fallback failures and returns extracted text" , async ( ) => {
180198 const onImageExtractionError = vi . fn ( ) ;
181199 const failure = new Error ( "render failed" ) ;
Original file line number Diff line number Diff line change @@ -70,6 +70,9 @@ async function extractPdfContent(
7070 . filter ( ( p ) => Number . isInteger ( p ) && p >= 1 && p <= pdf . pageCount )
7171 . slice ( 0 , request . maxPages )
7272 : undefined ;
73+ if ( request . pageNumbers ?. length && pages ?. length === 0 ) {
74+ throw new Error ( `No requested PDF pages exist in this ${ pdf . pageCount } -page document.` ) ;
75+ }
7376 const pageSelection = pages ? { pages } : { maxPages : request . maxPages } ;
7477
7578 const textResult = await pdf . extract ( {
You can’t perform that action at this time.
0 commit comments