@@ -438,54 +438,19 @@ public function getPages()
438438
439439 // Last-resort recovery strategies for malformed/non-standard PDFs,
440440 // tried in order of specificity; first non-empty result wins.
441+ // Closures preserve lazy evaluation while keeping explicit method calls.
441442 $ fallbacks = [
442443 function () {
443444 return $ this ->getRecoveredPagesFromMalformedHeaders ();
444445 },
445- // Encrypted catalog fallback: catalog exists but Pages is unreachable due to encryption
446446 function () {
447- if (!$ this ->trailer ->has ('Encrypt ' ) || !$ this ->hasObjectsByType ('Catalog ' )) {
448- return [];
449- }
450- $ catalogues = $ this ->getObjectsByType ('Catalog ' );
451- $ catalogue = reset ($ catalogues );
452- if (false === $ catalogue ) {
453- return [];
454- }
455- if (!$ catalogue ->get ('Pages ' ) instanceof ElementMissing) {
456- return [];
457- }
458- return [new Page ($ this , new Header ([], $ this ), '' )];
447+ return $ this ->getEncryptedCatalogFallbackPages ();
459448 },
460- // Xref root missing fallback: XRef exists but Catalog/Pages/Page refs are broken
461449 function () {
462- if (
463- !$ this ->hasObjectsByType ('XRef ' )
464- || $ this ->hasObjectsByType ('Catalog ' )
465- || $ this ->hasObjectsByType ('Pages ' )
466- || $ this ->hasObjectsByType ('Page ' )
467- ) {
468- return [];
469- }
470- if (!$ this ->trailer ->has ('Root ' ) || !$ this ->trailer ->get ('Root ' ) instanceof ElementMissing) {
471- return [];
472- }
473- return [new Page ($ this , new Header ([], $ this ), '' )];
450+ return $ this ->getXrefRootMissingFallbackPages ();
474451 },
475- // Catalog missing pages fallback: Catalog exists but its Pages field is missing
476452 function () {
477- if (!$ this ->hasObjectsByType ('Catalog ' )) {
478- return [];
479- }
480- $ catalogues = $ this ->getObjectsByType ('Catalog ' );
481- $ catalogue = reset ($ catalogues );
482- if (false === $ catalogue ) {
483- return [];
484- }
485- if (!$ catalogue ->get ('Pages ' ) instanceof ElementMissing) {
486- return [];
487- }
488- return [new Page ($ this , new Header ([], $ this ), '' )];
453+ return $ this ->getCatalogMissingPagesFallbackPages ();
489454 },
490455 function () {
491456 return $ this ->getCatalogUnresolvablePagesFallbackPages ();
@@ -571,7 +536,71 @@ protected function getRecoveredPagesFromMalformedHeaders(): array
571536 return $ pages ;
572537 }
573538
539+ /**
540+ * @return array<Page>
541+ */
542+ protected function getEncryptedCatalogFallbackPages (): array
543+ {
544+ if (!$ this ->trailer ->has ('Encrypt ' ) || !$ this ->hasObjectsByType ('Catalog ' )) {
545+ return [];
546+ }
547+
548+ $ catalogues = $ this ->getObjectsByType ('Catalog ' );
549+ $ catalogue = reset ($ catalogues );
550+ if (false === $ catalogue ) {
551+ return [];
552+ }
553+
554+ $ pages = $ catalogue ->get ('Pages ' );
555+ if (!$ pages instanceof ElementMissing) {
556+ return [];
557+ }
558+
559+ return [new Page ($ this , new Header ([], $ this ), '' )];
560+ }
574561
562+ /**
563+ * @return array<Page>
564+ */
565+ protected function getXrefRootMissingFallbackPages (): array
566+ {
567+ if (
568+ !$ this ->hasObjectsByType ('XRef ' )
569+ || $ this ->hasObjectsByType ('Catalog ' )
570+ || $ this ->hasObjectsByType ('Pages ' )
571+ || $ this ->hasObjectsByType ('Page ' )
572+ ) {
573+ return [];
574+ }
575+
576+ if (!$ this ->trailer ->has ('Root ' ) || !$ this ->trailer ->get ('Root ' ) instanceof ElementMissing) {
577+ return [];
578+ }
579+
580+ return [new Page ($ this , new Header ([], $ this ), '' )];
581+ }
582+
583+ /**
584+ * @return array<Page>
585+ */
586+ protected function getCatalogMissingPagesFallbackPages (): array
587+ {
588+ if (!$ this ->hasObjectsByType ('Catalog ' )) {
589+ return [];
590+ }
591+
592+ $ catalogues = $ this ->getObjectsByType ('Catalog ' );
593+ $ catalogue = reset ($ catalogues );
594+ if (false === $ catalogue ) {
595+ return [];
596+ }
597+
598+ if (!$ catalogue ->get ('Pages ' ) instanceof ElementMissing) {
599+ return [];
600+ }
601+
602+ return [new Page ($ this , new Header ([], $ this ), '' )];
603+ }
575604
576605 /**
577606 * @return array<Page>
0 commit comments