mirror of
https://github.com/documenso/documenso
synced 2026-04-21 13:27:18 +00:00
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:
parent
3cca8cdae8
commit
0b9a23c550
1 changed files with 22 additions and 5 deletions
|
|
@ -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 } => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue