From a0b176b0767cf733c2ff0baea152bda3ea02d9c5 Mon Sep 17 00:00:00 2001 From: DarioGii Date: Fri, 13 Jun 2025 14:59:54 +0100 Subject: [PATCH] wip - fixing embed feature --- .../SPDF/config/EndpointConfiguration.java | 2 ++ .../api/misc/AttachmentsController.java | 24 +++++++------- .../controller/web/OtherWebController.java | 4 +-- .../SPDF/service/PDFAttachmentService.java | 32 ++++++++++++------- .../templates/fragments/navElements.html | 2 +- .../main/resources/templates/home-legacy.html | 3 ++ ...{attachments.html => add-attachments.html} | 0 testing/allEndpointsRemovedSettings.yml | 2 +- testing/endpoints.txt | 1 + testing/webpage_urls.txt | 1 + 10 files changed, 43 insertions(+), 28 deletions(-) rename stirling-pdf/src/main/resources/templates/misc/{attachments.html => add-attachments.html} (100%) diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java b/stirling-pdf/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java index c9872992a..a43db93a8 100644 --- a/stirling-pdf/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java @@ -173,6 +173,7 @@ public class EndpointConfiguration { addEndpointToGroup("Other", "get-info-on-pdf"); addEndpointToGroup("Other", "show-javascript"); addEndpointToGroup("Other", "remove-image-pdf"); + addEndpointToGroup("Other", "add-attachments"); // CLI addEndpointToGroup("CLI", "compress-pdf"); @@ -251,6 +252,7 @@ public class EndpointConfiguration { addEndpointToGroup("Java", "pdf-to-text"); addEndpointToGroup("Java", "remove-image-pdf"); addEndpointToGroup("Java", "pdf-to-markdown"); + addEndpointToGroup("Java", "add-attachments"); // Javascript addEndpointToGroup("Javascript", "pdf-organizer"); diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/AttachmentsController.java b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/AttachmentsController.java index 8dc548952..d1027716e 100644 --- a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/AttachmentsController.java +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/AttachmentsController.java @@ -3,10 +3,7 @@ package stirling.software.SPDF.controller.api.misc; import java.io.IOException; import java.util.List; -import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.pdmodel.PDDocumentCatalog; -import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode; -import org.apache.pdfbox.pdmodel.PageMode; +import org.apache.pdfbox.pdmodel.*; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -33,6 +30,7 @@ public class AttachmentsController { private final PDFAttachmentServiceInterface pdfAttachmentService; + @SuppressWarnings("DataFlowIssue") @PostMapping(consumes = "multipart/form-data", value = "/add-attachments") @Operation( summary = "Add attachments to PDF", @@ -50,15 +48,19 @@ public class AttachmentsController { PDDocumentCatalog catalog = document.getDocumentCatalog(); // Create embedded files name tree if it doesn't exist - PDEmbeddedFilesNameTreeNode efTree = catalog.getNames().getEmbeddedFiles(); + PDDocumentNameDictionary documentNames = catalog.getNames(); + PDEmbeddedFilesNameTreeNode embeddedFilesTree = new PDEmbeddedFilesNameTreeNode(); - if (efTree == null) { - efTree = new PDEmbeddedFilesNameTreeNode(); - catalog.getNames().setEmbeddedFiles(efTree); + if (documentNames != null) { + embeddedFilesTree = documentNames.getEmbeddedFiles(); + } else { + documentNames = new PDDocumentNameDictionary(catalog); + documentNames.setEmbeddedFiles(embeddedFilesTree); } // Add attachments - pdfAttachmentService.addAttachment(document, efTree, attachments); + catalog.setNames(documentNames); + pdfAttachmentService.addAttachment(document, embeddedFilesTree, attachments); // Set PageMode to UseAttachments to show the attachments panel catalog.setPageMode(PageMode.USE_ATTACHMENTS); @@ -79,10 +81,8 @@ public class AttachmentsController { public ResponseEntity removeAttachments( @RequestParam("fileInput") MultipartFile pdfFile) throws IOException { - // Load the PDF document + // Load the PDF document and document catalog PDDocument document = pdfDocumentFactory.load(pdfFile, true); - - // Get the document catalog PDDocumentCatalog catalog = document.getDocumentCatalog(); // Remove embedded files diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java index 59c317581..84b656e40 100644 --- a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java @@ -192,10 +192,10 @@ public class OtherWebController { return "misc/auto-rename"; } - @GetMapping("/attachments") + @GetMapping("/add-attachments") @Hidden public String attachmentsForm(Model model) { model.addAttribute("currentPage", "attachments"); - return "misc/attachments"; + return "misc/add-attachments"; } } diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/service/PDFAttachmentService.java b/stirling-pdf/src/main/java/stirling/software/SPDF/service/PDFAttachmentService.java index dae172337..dd39960ce 100644 --- a/stirling-pdf/src/main/java/stirling/software/SPDF/service/PDFAttachmentService.java +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/service/PDFAttachmentService.java @@ -22,18 +22,28 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface { @Override public void addAttachment( PDDocument document, - PDEmbeddedFilesNameTreeNode efTree, + PDEmbeddedFilesNameTreeNode embeddedFilesTree, List attachments) throws IOException { - // Get existing names or create new map - Map existingNames = new java.util.HashMap<>(); +// todo: sanitize attachments first + // todo: find out how to access the embedded files in the PDF + Map existingNames; + try { - existingNames = efTree.getNames(); + existingNames = embeddedFilesTree.getNames(); + if (existingNames == null) { + log.info("No existing embedded files found, creating new names map."); + // Initialize an empty map if no existing names are found + existingNames = new java.util.HashMap<>(); + } + + log.debug("Existing embedded files: {}", existingNames.keySet()); } catch (IOException e) { - log.warn("Could not retrieve existing embedded files, starting with empty map", e); + log.error("Could not retrieve existing embedded files", e); + throw e; } - Map finalExistingNames = existingNames; + final Map existingEmbeddedFiles = existingNames; attachments.forEach( attachment -> { // Create attachments specification @@ -43,7 +53,6 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface { "Embedded attachment: " + attachment.getOriginalFilename()); try { - // Create embedded attachment PDEmbeddedFile embeddedFile = new PDEmbeddedFile( document, new ByteArrayInputStream(attachment.getBytes())); @@ -61,21 +70,20 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface { fileSpecification.setEmbeddedFile(embeddedFile); // Add to the existing names map - finalExistingNames.put(attachment.getOriginalFilename(), fileSpecification); + existingEmbeddedFiles.put(attachment.getOriginalFilename(), fileSpecification); log.info( - "Added embedded attachment: {} ({} bytes)", + "Added attachment: {} ({} bytes)", attachment.getOriginalFilename(), attachment.getSize()); } catch (IOException e) { - log.error( + log.warn( "Failed to create embedded file for attachment: {}", attachment.getOriginalFilename(), e); } }); - // Update the name tree with all names - efTree.setNames(existingNames); + embeddedFilesTree.setNames(existingNames); } } diff --git a/stirling-pdf/src/main/resources/templates/fragments/navElements.html b/stirling-pdf/src/main/resources/templates/fragments/navElements.html index f26abd0a4..38dfa0ac6 100644 --- a/stirling-pdf/src/main/resources/templates/fragments/navElements.html +++ b/stirling-pdf/src/main/resources/templates/fragments/navElements.html @@ -238,7 +238,7 @@ th:replace="~{fragments/navbarEntry :: navbarEntry('unlock-pdf-forms', 'preview_off', 'home.unlockPDFForms.title', 'home.unlockPDFForms.desc', 'unlockPDFForms.tags', 'other')}">
+ th:replace="~{fragments/navbarEntry :: navbarEntry('add-attachments', 'attachment', 'home.attachments.title', 'home.attachments.desc', 'attachments.tags', 'other')}">
diff --git a/stirling-pdf/src/main/resources/templates/home-legacy.html b/stirling-pdf/src/main/resources/templates/home-legacy.html index d60ac220e..9531a359b 100644 --- a/stirling-pdf/src/main/resources/templates/home-legacy.html +++ b/stirling-pdf/src/main/resources/templates/home-legacy.html @@ -290,6 +290,9 @@
+
+
diff --git a/stirling-pdf/src/main/resources/templates/misc/attachments.html b/stirling-pdf/src/main/resources/templates/misc/add-attachments.html similarity index 100% rename from stirling-pdf/src/main/resources/templates/misc/attachments.html rename to stirling-pdf/src/main/resources/templates/misc/add-attachments.html diff --git a/testing/allEndpointsRemovedSettings.yml b/testing/allEndpointsRemovedSettings.yml index 3290d6fef..8230b4418 100644 --- a/testing/allEndpointsRemovedSettings.yml +++ b/testing/allEndpointsRemovedSettings.yml @@ -128,7 +128,7 @@ ui: languages: [] # If empty, all languages are enabled. To display only German and Polish ["de_DE", "pl_PL"]. British English is always enabled. endpoints: # All the possible endpoints are disabled - toRemove: [crop, merge-pdfs, multi-page-layout, overlay-pdfs, pdf-to-single-page, rearrange-pages, remove-image-pdf, remove-pages, rotate-pdf, scale-pages, split-by-size-or-count, split-pages, split-pdf-by-chapters, split-pdf-by-sections, add-password, add-watermark, auto-redact, cert-sign, get-info-on-pdf, redact, remove-cert-sign, remove-password, sanitize-pdf, validate-signature, file-to-pdf, html-to-pdf, img-to-pdf, markdown-to-pdf, pdf-to-csv, pdf-to-html, pdf-to-img, pdf-to-markdown, pdf-to-pdfa, pdf-to-presentation, pdf-to-text, pdf-to-word, pdf-to-xml, url-to-pdf, add-image, add-page-numbers, add-stamp, auto-rename, auto-split-pdf, compress-pdf, decompress-pdf, extract-image-scans, extract-images, flatten, ocr-pdf, remove-blanks, repair, replace-invert-pdf, show-javascript, update-metadata, filter-contains-image, filter-contains-text, filter-file-size, filter-page-count, filter-page-rotation, filter-page-size] # list endpoints to disable (e.g. ['img-to-pdf', 'remove-pages']) + toRemove: [crop, merge-pdfs, multi-page-layout, overlay-pdfs, pdf-to-single-page, rearrange-pages, remove-image-pdf, remove-pages, rotate-pdf, scale-pages, split-by-size-or-count, split-pages, split-pdf-by-chapters, split-pdf-by-sections, add-password, add-watermark, auto-redact, cert-sign, get-info-on-pdf, redact, remove-cert-sign, remove-password, sanitize-pdf, validate-signature, file-to-pdf, html-to-pdf, img-to-pdf, markdown-to-pdf, pdf-to-csv, pdf-to-html, pdf-to-img, pdf-to-markdown, pdf-to-pdfa, pdf-to-presentation, pdf-to-text, pdf-to-word, pdf-to-xml, url-to-pdf, add-image, add-page-numbers, add-stamp, auto-rename, auto-split-pdf, compress-pdf, decompress-pdf, extract-image-scans, extract-images, flatten, ocr-pdf, remove-blanks, repair, replace-invert-pdf, show-javascript, update-metadata, filter-contains-image, filter-contains-text, filter-file-size, filter-page-count, filter-page-rotation, filter-page-size, add-attachments] # list endpoints to disable (e.g. ['img-to-pdf', 'remove-pages']) groupsToRemove: [] # list groups to disable (e.g. ['LibreOffice']) metrics: diff --git a/testing/endpoints.txt b/testing/endpoints.txt index 5468ad6c1..149e3af3a 100644 --- a/testing/endpoints.txt +++ b/testing/endpoints.txt @@ -30,6 +30,7 @@ /api/v1/misc/add-stamp /api/v1/misc/add-page-numbers /api/v1/misc/add-image +/api/v1/misc/add-attachments /api/v1/convert/url/pdf /api/v1/convert/pdf/xml /api/v1/convert/pdf/word diff --git a/testing/webpage_urls.txt b/testing/webpage_urls.txt index 8ccaaf0b1..c6c713dd0 100644 --- a/testing/webpage_urls.txt +++ b/testing/webpage_urls.txt @@ -51,3 +51,4 @@ /swagger-ui/index.html /licenses /releases +/add-attachments