[fix]: check for encryption in PageNumbers (#1949)

[fix]: check for empty password encryption on load

Co-authored-by: maxi322 <maxi322@users.noreply.github.com>
This commit is contained in:
maxi322 2024-09-23 21:52:57 +02:00 committed by GitHub
parent df901db1f8
commit 1c6e5df77d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 9 deletions

View File

@ -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) {

View File

@ -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
}