diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/ScalePagesController.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/ScalePagesController.java index 027457bbd..e90aab8eb 100644 --- a/app/core/src/main/java/stirling/software/SPDF/controller/api/ScalePagesController.java +++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/ScalePagesController.java @@ -105,13 +105,20 @@ public class ScalePagesController { private PDRectangle getTargetSize(String targetPDRectangle, PDDocument sourceDocument) { if ("KEEP".equals(targetPDRectangle)) { if (sourceDocument.getNumberOfPages() == 0) { - return null; + // Do not return null here; throw a clear exception so callers don't get a nullable + // PDRectangle. + throw ExceptionUtils.createInvalidPageSizeException("KEEP"); } // use the first page to determine the target page size PDPage sourcePage = sourceDocument.getPage(0); PDRectangle sourceSize = sourcePage.getMediaBox(); + if (sourceSize == null) { + // If media box is unexpectedly null, treat it as invalid + throw ExceptionUtils.createInvalidPageSizeException("KEEP"); + } + return sourceSize; }