From 06ef09035da7805b5159599c0401b2646f2d85a2 Mon Sep 17 00:00:00 2001 From: sbplat <71648843+sbplat@users.noreply.github.com> Date: Mon, 22 Jan 2024 10:39:29 -0500 Subject: [PATCH] fix: remove blank pages not handling EXIT_FAILURE code properly --- .../security/UserAuthenticationFilter.java | 2 +- .../api/SplitPdfBySectionsController.java | 4 ++-- .../api/misc/BlankPageController.java | 24 ++++++++++++------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/config/security/UserAuthenticationFilter.java b/src/main/java/stirling/software/SPDF/config/security/UserAuthenticationFilter.java index 61b209de..47423eb6 100644 --- a/src/main/java/stirling/software/SPDF/config/security/UserAuthenticationFilter.java +++ b/src/main/java/stirling/software/SPDF/config/security/UserAuthenticationFilter.java @@ -115,4 +115,4 @@ public class UserAuthenticationFilter extends OncePerRequestFilter { return false; } -} \ No newline at end of file +} diff --git a/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java b/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java index de3e5a4b..90418169 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java @@ -121,8 +121,8 @@ public class SplitPdfBySectionsController { subDoc, subPage, AppendMode.APPEND, true, true)) { // Set clipping area and position float translateX = -subPageWidth * i; - - //float translateY = height - subPageHeight * (verticalDivisions - j); + + // float translateY = height - subPageHeight * (verticalDivisions - j); float translateY = -subPageHeight * (verticalDivisions - 1 - j); contentStream.saveGraphicsState(); diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java index 8a17e3de..21e5987b 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java @@ -33,7 +33,6 @@ import io.swagger.v3.oas.annotations.tags.Tag; import stirling.software.SPDF.model.api.misc.RemoveBlankPagesRequest; import stirling.software.SPDF.utils.PdfUtils; import stirling.software.SPDF.utils.ProcessExecutor; -import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult; import stirling.software.SPDF.utils.WebResponseUtils; @RestController @@ -85,7 +84,7 @@ public class BlankPageController { List command = new ArrayList<>( Arrays.asList( - "python3", + "python", System.getProperty("user.dir") + "/scripts/detect-blank-pages.py", tempFile.toString(), @@ -94,18 +93,25 @@ public class BlankPageController { "--white_percent", String.valueOf(whitePercent))); + Boolean blank = false; // Run CLI command - ProcessExecutorResult returnCode = - ProcessExecutor.getInstance(ProcessExecutor.Processes.PYTHON_OPENCV) - .runCommandWithOutputHandling(command); + try { + ProcessExecutor.getInstance(ProcessExecutor.Processes.PYTHON_OPENCV) + .runCommandWithOutputHandling(command); + } catch (IOException e) { + // From detect-blank-pages.py + // Return code 1: The image is considered blank. + // Return code 0: The image is not considered blank. + // Since the process returned with a failure code, it should be blank. + blank = true; + } - // does contain data - if (returnCode.getRc() == 0) { + if (blank) { + System.out.println("Skipping, Image was blank for page #" + pageIndex); + } else { System.out.println( "page " + pageIndex + " has image which is not blank"); pagesToKeepIndex.add(pageIndex); - } else { - System.out.println("Skipping, Image was blank for page #" + pageIndex); } } }