@@ -551,96 +551,111 @@ class WorkerMessageHandler {
551551 return pdfManager . ensureDoc ( "calculationOrderIds" ) ;
552552 } ) ;
553553
554- handler . on ( "ExtractPages" , async function ( { pageInfos } ) {
555- if ( ! pageInfos ) {
556- warn ( "extractPages: nothing to extract." ) ;
557- return null ;
558- }
559- if ( ! Array . isArray ( pageInfos ) ) {
560- pageInfos = [ pageInfos ] ;
561- }
562- let newDocumentId = 0 ;
563- for ( const pageInfo of pageInfos ) {
564- if ( pageInfo . document === null ) {
565- pageInfo . document = pdfManager . pdfDocument ;
566- } else if ( ArrayBuffer . isView ( pageInfo . document ) ) {
567- const manager = new LocalPdfManager ( {
568- source : pageInfo . document ,
569- docId : `${ docId } _extractPages_${ newDocumentId ++ } ` ,
570- handler,
571- password : pageInfo . password ?? null ,
572- evaluatorOptions : Object . assign ( { } , pdfManager . evaluatorOptions ) ,
573- } ) ;
574- let recoveryMode = false ;
575- let isValid = true ;
576- while ( true ) {
577- try {
578- await manager . requestLoadedStream ( ) ;
579- await manager . ensureDoc ( "checkHeader" ) ;
580- await manager . ensureDoc ( "parseStartXRef" ) ;
581- await manager . ensureDoc ( "parse" , [ recoveryMode ] ) ;
582- break ;
583- } catch ( e ) {
584- if ( e instanceof XRefParseException ) {
585- if ( recoveryMode === false ) {
586- recoveryMode = true ;
587- continue ;
554+ handler . on (
555+ "ExtractPages" ,
556+ async function ( { pageInfos, annotationStorage } ) {
557+ if ( ! pageInfos ) {
558+ warn ( "extractPages: nothing to extract." ) ;
559+ return null ;
560+ }
561+ if ( ! Array . isArray ( pageInfos ) ) {
562+ pageInfos = [ pageInfos ] ;
563+ }
564+ let newDocumentId = 0 ;
565+ for ( const pageInfo of pageInfos ) {
566+ if ( pageInfo . document === null ) {
567+ pageInfo . document = pdfManager . pdfDocument ;
568+ } else if ( ArrayBuffer . isView ( pageInfo . document ) ) {
569+ const manager = new LocalPdfManager ( {
570+ source : pageInfo . document ,
571+ docId : `${ docId } _extractPages_${ newDocumentId ++ } ` ,
572+ handler,
573+ password : pageInfo . password ?? null ,
574+ evaluatorOptions : Object . assign ( { } , pdfManager . evaluatorOptions ) ,
575+ } ) ;
576+ let recoveryMode = false ;
577+ let isValid = true ;
578+ while ( true ) {
579+ try {
580+ await manager . requestLoadedStream ( ) ;
581+ await manager . ensureDoc ( "checkHeader" ) ;
582+ await manager . ensureDoc ( "parseStartXRef" ) ;
583+ await manager . ensureDoc ( "parse" , [ recoveryMode ] ) ;
584+ break ;
585+ } catch ( e ) {
586+ if ( e instanceof XRefParseException ) {
587+ if ( recoveryMode === false ) {
588+ recoveryMode = true ;
589+ continue ;
590+ } else {
591+ isValid = false ;
592+ warn ( "extractPages: XRefParseException." ) ;
593+ }
594+ } else if ( e instanceof PasswordException ) {
595+ const task = new WorkerTask (
596+ `PasswordException: response ${ e . code } `
597+ ) ;
598+
599+ startWorkerTask ( task ) ;
600+
601+ try {
602+ const { password } = await handler . sendWithPromise (
603+ "PasswordRequest" ,
604+ e
605+ ) ;
606+ manager . updatePassword ( password ) ;
607+ } catch {
608+ isValid = false ;
609+ warn ( "extractPages: invalid password." ) ;
610+ } finally {
611+ finishWorkerTask ( task ) ;
612+ }
588613 } else {
589614 isValid = false ;
590- warn ( "extractPages: XRefParseException ." ) ;
615+ warn ( "extractPages: invalid document ." ) ;
591616 }
592- } else if ( e instanceof PasswordException ) {
593- const task = new WorkerTask (
594- `PasswordException: response ${ e . code } `
595- ) ;
596-
597- startWorkerTask ( task ) ;
598-
599- try {
600- const { password } = await handler . sendWithPromise (
601- "PasswordRequest" ,
602- e
603- ) ;
604- manager . updatePassword ( password ) ;
605- } catch {
606- isValid = false ;
607- warn ( "extractPages: invalid password." ) ;
608- } finally {
609- finishWorkerTask ( task ) ;
617+ if ( ! isValid ) {
618+ break ;
610619 }
611- } else {
612- isValid = false ;
613- warn ( "extractPages: invalid document." ) ;
614- }
615- if ( ! isValid ) {
616- break ;
617620 }
618621 }
619- }
620- if ( ! isValid ) {
621- pageInfo . document = null ;
622- }
623- const isPureXfa = await manager . ensureDoc ( "isPureXfa" ) ;
624- if ( isPureXfa ) {
625- pageInfo . document = null ;
626- warn ( "extractPages does not support pure XFA documents." ) ;
622+ if ( ! isValid ) {
623+ pageInfo . document = null ;
624+ }
625+ const isPureXfa = await manager . ensureDoc ( "isPureXfa" ) ;
626+ if ( isPureXfa ) {
627+ pageInfo . document = null ;
628+ warn ( "extractPages does not support pure XFA documents." ) ;
629+ } else {
630+ pageInfo . document = manager . pdfDocument ;
631+ }
627632 } else {
628- pageInfo . document = manager . pdfDocument ;
633+ warn ( "extractPages: invalid document." ) ;
634+ }
635+ }
636+ let task ;
637+ try {
638+ const pdfEditor = new PDFEditor ( ) ;
639+ task = new WorkerTask ( `ExtractPages: ${ pageInfos . length } page(s)` ) ;
640+ startWorkerTask ( task ) ;
641+ const buffer = await pdfEditor . extractPages (
642+ pageInfos ,
643+ annotationStorage ,
644+ handler ,
645+ task
646+ ) ;
647+ return buffer ;
648+ } catch ( reason ) {
649+ // eslint-disable-next-line no-console
650+ console . error ( reason ) ;
651+ return null ;
652+ } finally {
653+ if ( task ) {
654+ finishWorkerTask ( task ) ;
629655 }
630- } else {
631- warn ( "extractPages: invalid document." ) ;
632656 }
633657 }
634- try {
635- const pdfEditor = new PDFEditor ( ) ;
636- const buffer = await pdfEditor . extractPages ( pageInfos ) ;
637- return buffer ;
638- } catch ( reason ) {
639- // eslint-disable-next-line no-console
640- console . error ( reason ) ;
641- return null ;
642- }
643- } ) ;
658+ ) ;
644659
645660 handler . on (
646661 "SaveDocument" ,
0 commit comments