From 1c6e5df77d10fcd6f77c7b129041931fb1ac2428 Mon Sep 17 00:00:00 2001 From: maxi322 <66129899+maxi322@users.noreply.github.com> Date: Mon, 23 Sep 2024 21:52:57 +0200 Subject: [PATCH] [fix]: check for encryption in PageNumbers (#1949) [fix]: check for empty password encryption on load Co-authored-by: maxi322 --- .../api/SplitPdfByChaptersController.java | 9 --------- .../SPDF/service/CustomPDDocumentFactory.java | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/SplitPdfByChaptersController.java b/src/main/java/stirling/software/SPDF/controller/api/SplitPdfByChaptersController.java index 9d03f8b6..0cca8f78 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/SplitPdfByChaptersController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/SplitPdfByChaptersController.java @@ -67,15 +67,6 @@ public class SplitPdfByChaptersController { } PDDocument sourceDocument = Loader.loadPDF(file.getBytes()); - // checks if the document is encrypted by an empty user password - if (sourceDocument.isEncrypted()) { - try { - sourceDocument.setAllSecurityToBeRemoved(true); - logger.info("Removing security from the source document "); - } catch (Exception e) { - logger.warn("Cannot decrypt the pdf"); - } - } PDDocumentOutline outline = sourceDocument.getDocumentCatalog().getDocumentOutline(); if (outline == null) { diff --git a/src/main/java/stirling/software/SPDF/service/CustomPDDocumentFactory.java b/src/main/java/stirling/software/SPDF/service/CustomPDDocumentFactory.java index b3f9eec7..2d3d8990 100644 --- a/src/main/java/stirling/software/SPDF/service/CustomPDDocumentFactory.java +++ b/src/main/java/stirling/software/SPDF/service/CustomPDDocumentFactory.java @@ -7,6 +7,8 @@ import java.io.InputStream; import org.apache.pdfbox.Loader; import org.apache.pdfbox.pdmodel.PDDocument; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; @@ -18,6 +20,8 @@ import stirling.software.SPDF.model.api.PDFFile; @Component public class CustomPDDocumentFactory { + private static final Logger logger = LoggerFactory.getLogger(CustomPDDocumentFactory.class); + private final PdfMetadataService pdfMetadataService; @Autowired @@ -71,6 +75,7 @@ public class CustomPDDocumentFactory { public PDDocument load(byte[] input) throws IOException { PDDocument document = Loader.loadPDF(input); pdfMetadataService.setDefaultMetadata(document); + removezeropassword(document); return document; } @@ -96,5 +101,17 @@ public class CustomPDDocumentFactory { return document; } + private PDDocument removezeropassword(PDDocument document) throws IOException { + if (document.isEncrypted()) { + try { + logger.info("Removing security from the source document"); + document.setAllSecurityToBeRemoved(true); + } catch (Exception e) { + logger.warn("Cannot decrypt the pdf"); + } + } + return document; + } + // Add other load methods as needed, following the same pattern }