From e5e793545653d42a9a824507025780269dd430e4 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Fri, 9 May 2025 16:42:20 +0100 Subject: [PATCH] pixel changes, redact color fix, version bump, aggressive compression (#3502) # Description of Changes Please provide a summary of the changes, including: - What was changed - Why the change was made - Any challenges encountered Closes #(issue_number) --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details. --- build.gradle | 2 +- .../software/SPDF/EE/EEAppConfig.java | 5 ++ .../software/SPDF/config/AppConfig.java | 37 ++++++++++++++ .../api/misc/CompressController.java | 51 +++++++++++-------- .../api/security/RedactController.java | 11 ++-- .../model/api/security/RedactPdfRequest.java | 2 +- .../resources/templates/fragments/common.html | 10 +++- src/main/resources/templates/home.html | 2 +- 8 files changed, 90 insertions(+), 30 deletions(-) diff --git a/build.gradle b/build.gradle index e91f0a467..7359d0b6a 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ ext { } group = "stirling.software" -version = "0.46.0" +version = "0.46.1" java { // 17 is lowest but we support and recommend 21 diff --git a/src/main/java/stirling/software/SPDF/EE/EEAppConfig.java b/src/main/java/stirling/software/SPDF/EE/EEAppConfig.java index a83b17090..0bbb531ef 100644 --- a/src/main/java/stirling/software/SPDF/EE/EEAppConfig.java +++ b/src/main/java/stirling/software/SPDF/EE/EEAppConfig.java @@ -33,6 +33,11 @@ public class EEAppConfig { public boolean runningProOrHigher() { return licenseKeyChecker.getPremiumLicenseEnabledResult() != License.NORMAL; } + + @Bean(name = "license") + public String licenseType() { + return licenseKeyChecker.getPremiumLicenseEnabledResult().name(); + } @Bean(name = "runningEE") public boolean runningEnterprise() { diff --git a/src/main/java/stirling/software/SPDF/config/AppConfig.java b/src/main/java/stirling/software/SPDF/config/AppConfig.java index f07368527..5b52ed3a5 100644 --- a/src/main/java/stirling/software/SPDF/config/AppConfig.java +++ b/src/main/java/stirling/software/SPDF/config/AppConfig.java @@ -5,6 +5,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import java.util.Locale; import java.util.Properties; import java.util.function.Predicate; @@ -15,6 +16,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Scope; +import org.springframework.core.env.Environment; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; @@ -33,6 +35,8 @@ public class AppConfig { private final ApplicationProperties applicationProperties; + private final Environment env; + @Bean @ConditionalOnProperty(name = "system.customHTMLFiles", havingValue = "true") public SpringTemplateEngine templateEngine(ResourceLoader resourceLoader) { @@ -193,4 +197,37 @@ public class AppConfig { public String uuid() { return applicationProperties.getAutomaticallyGenerated().getUUID(); } + + @Bean(name = "disablePixel") + public boolean disablePixel() { + return Boolean.getBoolean(env.getProperty("DISABLE_PIXEL")); + } + + @Bean(name = "machineType") + public String determineMachineType() { + try { + boolean isDocker = runningInDocker(); + boolean isKubernetes = System.getenv("KUBERNETES_SERVICE_HOST") != null; + boolean isBrowserOpen = "true".equalsIgnoreCase(env.getProperty("BROWSER_OPEN")); + + if (isKubernetes) { + return "Kubernetes"; + } else if (isDocker) { + return "Docker"; + } else if (isBrowserOpen) { + String os = System.getProperty("os.name").toLowerCase(Locale.ROOT); + if (os.contains("win")) { + return "Client-windows"; + } else if (os.contains("mac")) { + return "Client-mac"; + } else { + return "Client-unix"; + } + } else { + return "Server-jar"; + } + } catch (Exception e) { + return "Unknown"; + } + } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/CompressController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/CompressController.java index 79d46cc45..da4a77962 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/CompressController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/CompressController.java @@ -626,25 +626,32 @@ public class CompressController { // Scale factors for different optimization levels private double getScaleFactorForLevel(int optimizeLevel) { - return switch (optimizeLevel) { - case 4 -> 0.9; // 90% - lite compression - case 5 -> 0.8; // 80% - lite compression - case 6 -> 0.7; // 70% - lite compression - case 7 -> 0.6; // 60% - intense compression - case 8 -> 0.5; // 50% - intense compression - case 9, 10 -> 0.4; // 40% - intense compression - default -> 1.0; // No scaling for levels 1-3 - }; + return switch (optimizeLevel) { + case 3 -> 0.85; + case 4 -> 0.75; + case 5 -> 0.65; + case 6 -> 0.55; + case 7 -> 0.45; + case 8 -> 0.35; + case 9 -> 0.25; + case 10 -> 0.15; + default -> 1.0; + }; } // JPEG quality for different optimization levels private float getJpegQualityForLevel(int optimizeLevel) { - return switch (optimizeLevel) { - case 7 -> 0.8f; // 80% quality - case 8 -> 0.6f; // 60% quality - case 9, 10 -> 0.4f; // 40% quality - default -> 0.7f; // 70% quality for levels 1-6 - }; + return switch (optimizeLevel) { + case 3 -> 0.85f; + case 4 -> 0.80f; + case 5 -> 0.75f; + case 6 -> 0.70f; + case 7 -> 0.60f; + case 8 -> 0.50f; + case 9 -> 0.35f; + case 10 -> 0.2f; + default -> 0.7f; + }; } @PostMapping(consumes = "multipart/form-data", value = "/compress-pdf") @@ -698,7 +705,7 @@ public class CompressController { while (!sizeMet && optimizeLevel <= 9) { // Apply image compression for levels 4-9 - if ((optimizeLevel >= 4 || Boolean.TRUE.equals(convertToGrayscale)) + if ((optimizeLevel >= 3 || Boolean.TRUE.equals(convertToGrayscale)) && !imageCompressionApplied) { double scaleFactor = getScaleFactorForLevel(optimizeLevel); float jpegQuality = getJpegQualityForLevel(optimizeLevel); @@ -790,10 +797,14 @@ public class CompressController { log.info("Pre-QPDF file size: {}", GeneralUtils.formatBytes(preQpdfSize)); // Map optimization levels to QPDF compression levels - int qpdfCompressionLevel = - optimizeLevel <= 3 - ? optimizeLevel * 3 // Level 1->3, 2->6, 3->9 - : 9; // Max compression for levels 4-9 + int qpdfCompressionLevel; + if (optimizeLevel == 1) { + qpdfCompressionLevel = 5; + } else if (optimizeLevel == 2) { + qpdfCompressionLevel = 9; + } else { + qpdfCompressionLevel = 9; + } // Create output file for QPDF Path qpdfOutputFile = Files.createTempFile("qpdf_output_", ".pdf"); diff --git a/src/main/java/stirling/software/SPDF/controller/api/security/RedactController.java b/src/main/java/stirling/software/SPDF/controller/api/security/RedactController.java index 0c8a7f7e6..df5e2499e 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/security/RedactController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/security/RedactController.java @@ -170,16 +170,17 @@ public class RedactController { } private Color decodeOrDefault(String hex, Color defaultColor) { - Color color = null; try { - color = Color.decode(hex); + if (hex != null && !hex.startsWith("#")) { + hex = "#" + hex; + } + return Color.decode(hex); } catch (Exception e) { - color = defaultColor; + return defaultColor; } - - return color; } + private List getPageNumbers(ManualRedactPdfRequest request, int pagesCount) { String pageNumbersInput = request.getPageNumbers(); String[] parsedPageNumbers = diff --git a/src/main/java/stirling/software/SPDF/model/api/security/RedactPdfRequest.java b/src/main/java/stirling/software/SPDF/model/api/security/RedactPdfRequest.java index 54c6a0aad..e92ae7c5a 100644 --- a/src/main/java/stirling/software/SPDF/model/api/security/RedactPdfRequest.java +++ b/src/main/java/stirling/software/SPDF/model/api/security/RedactPdfRequest.java @@ -23,7 +23,7 @@ public class RedactPdfRequest extends PDFFile { @Schema(description = "Whether to use whole word search", defaultValue = "false") private boolean wholeWordSearch; - @Schema(description = "The color for redaction", defaultValue = "#000000") + @Schema(description = "Hexadecimal color code for redaction, e.g. #FF0000 or 000000", defaultValue = "#000000") private String redactColor = "#000000"; @Schema(description = "Custom padding for redaction", type = "number") diff --git a/src/main/resources/templates/fragments/common.html b/src/main/resources/templates/fragments/common.html index 7fd26c777..02d919b2b 100644 --- a/src/main/resources/templates/fragments/common.html +++ b/src/main/resources/templates/fragments/common.html @@ -41,8 +41,14 @@ - - + + diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html index bba498db5..a7cbbbd80 100644 --- a/src/main/resources/templates/home.html +++ b/src/main/resources/templates/home.html @@ -43,7 +43,7 @@ th:insert="~{fragments/navbarEntry :: navbarEntry ('multi-tool', 'construction', 'home.multiTool.title', 'home.multiTool.desc', 'multiTool.tags', 'organize')}">
+ th:insert="~{fragments/navbarEntry :: navbarEntry('compress-pdf', 'zoom_in_map', 'home.compressPdfs.title', 'home.compressPdfs.desc', 'compressPDFs.tags', 'advance')}">