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
This commit is contained in:
Lucas Smith 2026-04-02 18:58:13 +11:00 committed by GitHub
parent 3cca8cdae8
commit 0b9a23c550
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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 } => {