From 0b9a23c5507cf713cf68b3f219627fa28098eef1 Mon Sep 17 00:00:00 2001 From: Lucas Smith Date: Thu, 2 Apr 2026 18:58:13 +1100 Subject: [PATCH] fix: handle malformed pdf cropbox/mediabox entries (#2668) Some PDFs have CropBox or MediaBox entries stored as a PDFDict instead of the expected PDFArray, causing pdf-lib to throw during lookup. Wrap both box lookups in try-catch and fall back to A4 dimensions when neither can be parsed --- packages/lib/server-only/pdf/get-page-size.ts | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/lib/server-only/pdf/get-page-size.ts b/packages/lib/server-only/pdf/get-page-size.ts index 8c88912b0..982810f1d 100644 --- a/packages/lib/server-only/pdf/get-page-size.ts +++ b/packages/lib/server-only/pdf/get-page-size.ts @@ -13,14 +13,31 @@ const MIN_CERT_PAGE_HEIGHT = 300; * Falls back to MediaBox when it's smaller than CropBox, following typical PDF reader behavior. */ export const getPageSize = (page: PDFPage) => { - const cropBox = page.getCropBox(); - const mediaBox = page.getMediaBox(); + let mediaBox; + let cropBox; - if (mediaBox.width < cropBox.width || mediaBox.height < cropBox.height) { - return mediaBox; + try { + mediaBox = page.getMediaBox(); + } catch { + // MediaBox lookup can fail for malformed PDFs where the entry is not a valid PDFArray. } - return cropBox; + try { + cropBox = page.getCropBox(); + } catch { + // CropBox lookup can fail for malformed PDFs where the entry is not a valid PDFArray. + } + + if (mediaBox && cropBox) { + if (mediaBox.width < cropBox.width || mediaBox.height < cropBox.height) { + return mediaBox; + } + + return cropBox; + } + + // If either box is missing or invalid, fall back to MediaBox if available, otherwise CropBox, or default to A4 size. + return mediaBox || cropBox || PDF_SIZE_A4_72PPI; }; export const getLastPageDimensions = (pdfDoc: PDF): { width: number; height: number } => {