From 483d8b812b794898ea4ff680639e1c4eb0502af0 Mon Sep 17 00:00:00 2001 From: Ping Lin Date: Mon, 4 Aug 2025 15:22:12 +0100 Subject: [PATCH] Fix split mode null crash by defaulting to ALL --- .../api/SplitPdfBySectionsController.java | 34 +++++++++++-------- .../model/api/SplitPdfBySectionsRequest.java | 22 ++++++------ 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java index 9933951c0..08f1dfeb7 100644 --- a/app/core/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java +++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java @@ -4,10 +4,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -61,10 +58,14 @@ public class SplitPdfBySectionsController { MultipartFile file = request.getFileInput(); String pageNumbers = request.getPageNumbers(); - String splitMode = request.getSplitMode(); + SplitTypes splitMode = Optional.ofNullable(request.getSplitMode()) + .map(SplitTypes::valueOf) + .orElse(SplitTypes.SPLIT_ALL); + PDDocument sourceDocument = pdfDocumentFactory.load(file); - Set pagesToSplit = getPagesToSplit(pageNumbers, splitMode, sourceDocument.getNumberOfPages()); + Set pagesToSplit = + getPagesToSplit(pageNumbers, splitMode, sourceDocument.getNumberOfPages()); // Process the PDF based on split parameters int horiz = request.getHorizontalDivisions() + 1; @@ -119,16 +120,17 @@ public class SplitPdfBySectionsController { } // Based on the mode, get the pages that need to be split and return the pages set - private Set getPagesToSplit(String pageNumbers, String splitMode, int totalPages) { + private Set getPagesToSplit(String pageNumbers, SplitTypes splitMode, int totalPages) { Set pagesToSplit = new HashSet<>(); - switch (SplitTypes.valueOf(splitMode)) { + switch (splitMode) { case CUSTOM: if (pageNumbers == null || pageNumbers.isBlank()) { throw new IllegalArgumentException("Custom mode requires page numbers input."); } String[] pageOrderArr = pageNumbers.split(","); - List pageListToSplit = GeneralUtils.parsePageList(pageOrderArr, totalPages, false); + List pageListToSplit = + GeneralUtils.parsePageList(pageOrderArr, totalPages, false); pagesToSplit.addAll(pageListToSplit); break; @@ -163,9 +165,11 @@ public class SplitPdfBySectionsController { return pagesToSplit; } - public List splitPdfPages( - PDDocument document, int horizontalDivisions, int verticalDivisions, Set pagesToSplit) + PDDocument document, + int horizontalDivisions, + int verticalDivisions, + Set pagesToSplit) throws IOException { List splitDocuments = new ArrayList<>(); @@ -193,12 +197,12 @@ public class SplitPdfBySectionsController { subDoc.addPage(subPage); PDFormXObject form = - layerUtility.importPageAsForm( - document, document.getPages().indexOf(originalPage)); + layerUtility.importPageAsForm( + document, document.getPages().indexOf(originalPage)); try (PDPageContentStream contentStream = - new PDPageContentStream( - subDoc, subPage, AppendMode.APPEND, true, true)) { + new PDPageContentStream( + subDoc, subPage, AppendMode.APPEND, true, true)) { // Set clipping area and position float translateX = -subPageWidth * i; diff --git a/app/core/src/main/java/stirling/software/SPDF/model/api/SplitPdfBySectionsRequest.java b/app/core/src/main/java/stirling/software/SPDF/model/api/SplitPdfBySectionsRequest.java index 829dd5371..c22135bbc 100644 --- a/app/core/src/main/java/stirling/software/SPDF/model/api/SplitPdfBySectionsRequest.java +++ b/app/core/src/main/java/stirling/software/SPDF/model/api/SplitPdfBySectionsRequest.java @@ -12,20 +12,20 @@ import stirling.software.common.model.api.PDFFile; @EqualsAndHashCode(callSuper = true) public class SplitPdfBySectionsRequest extends PDFFile { @Schema( - description = "Pages to be split by section", - defaultValue = "all", - requiredMode = Schema.RequiredMode.REQUIRED) + description = "Pages to be split by section", + defaultValue = "all", + requiredMode = Schema.RequiredMode.REQUIRED) private String pageNumbers; @Schema( - implementation = SplitTypes.class, - description = - "Modes for page split. Valid values are:\n" - + "SPLIT_ALL_EXCEPT_FIRST_AND_LAST: Splits all except the first and the last pages.\n" - + "SPLIT_ALL_EXCEPT_FIRST: Splits all except the first page.\n" - + "SPLIT_ALL_EXCEPT_LAST: Splits all except the last page.\n" - + "SPLIT_ALL: Splits all pages.\n" - + "CUSTOM: Custom split.\n") + implementation = SplitTypes.class, + description = + "Modes for page split. Valid values are:\n" + + "SPLIT_ALL_EXCEPT_FIRST_AND_LAST: Splits all except the first and the last pages.\n" + + "SPLIT_ALL_EXCEPT_FIRST: Splits all except the first page.\n" + + "SPLIT_ALL_EXCEPT_LAST: Splits all except the last page.\n" + + "SPLIT_ALL: Splits all pages.\n" + + "CUSTOM: Custom split.\n") private String splitMode; @Schema(