diff --git a/common/build.gradle b/common/build.gradle index cdfc11b8f..6dfd222bf 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -16,6 +16,7 @@ spotless { } dependencies { api 'org.springframework.boot:spring-boot-starter-web' + api 'org.springframework.boot:spring-boot-starter-aop' api 'org.springframework.boot:spring-boot-starter-thymeleaf' api 'com.googlecode.owasp-java-html-sanitizer:owasp-java-html-sanitizer:20240325.1' api 'com.fathzer:javaluator:3.0.6' @@ -28,5 +29,5 @@ dependencies { api 'org.snakeyaml:snakeyaml-engine:2.9' api "org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.9" api 'jakarta.mail:jakarta.mail-api:2.1.3' - api 'org.springframework.boot:spring-boot-starter-aop' + runtimeOnly 'org.eclipse.angus:angus-mail:2.0.3' } diff --git a/common/src/main/java/stirling/software/common/util/AttachmentUtils.java b/common/src/main/java/stirling/software/common/util/AttachmentUtils.java new file mode 100644 index 000000000..32830a9f0 --- /dev/null +++ b/common/src/main/java/stirling/software/common/util/AttachmentUtils.java @@ -0,0 +1,50 @@ +package stirling.software.common.util; + +import org.apache.pdfbox.cos.COSDictionary; +import org.apache.pdfbox.cos.COSName; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDDocumentCatalog; +import org.apache.pdfbox.pdmodel.PageMode; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class AttachmentUtils { + + /** + * Sets the PDF catalog viewer preferences to display attachments in the viewer. + * + * @param document The PDDocument to modify. + * @param pageMode The PageMode to set for the PDF viewer. PageMode + * values: UseNone, UseOutlines, UseThumbs, + * FullScreen, UseOC, UseAttachments. + */ + public static void setCatalogViewerPreferences(PDDocument document, PageMode pageMode) { + try { + PDDocumentCatalog catalog = document.getDocumentCatalog(); + if (catalog != null) { + COSDictionary catalogDict = catalog.getCOSObject(); + + catalog.setPageMode(pageMode); + catalogDict.setName(COSName.PAGE_MODE, pageMode.stringValue()); + + COSDictionary viewerPrefs = + (COSDictionary) catalogDict.getDictionaryObject(COSName.VIEWER_PREFERENCES); + if (viewerPrefs == null) { + viewerPrefs = new COSDictionary(); + catalogDict.setItem(COSName.VIEWER_PREFERENCES, viewerPrefs); + } + + viewerPrefs.setName( + COSName.getPDFName("NonFullScreenPageMode"), pageMode.stringValue()); + + viewerPrefs.setBoolean(COSName.getPDFName("DisplayDocTitle"), true); + + log.info( + "Set PDF PageMode to UseAttachments to automatically show attachments pane"); + } + } catch (Exception e) { + log.error("Failed to set catalog viewer preferences for attachments", e); + } + } +} diff --git a/common/src/main/java/stirling/software/common/util/EmlToPdf.java b/common/src/main/java/stirling/software/common/util/EmlToPdf.java index b08bc16a5..6c0514822 100644 --- a/common/src/main/java/stirling/software/common/util/EmlToPdf.java +++ b/common/src/main/java/stirling/software/common/util/EmlToPdf.java @@ -1,5 +1,7 @@ package stirling.software.common.util; +import static stirling.software.common.util.AttachmentUtils.setCatalogViewerPreferences; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -20,13 +22,11 @@ import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.pdfbox.cos.COSDictionary; -import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary; import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode; import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.PageMode; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification; import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile; @@ -42,10 +42,13 @@ import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; import stirling.software.common.model.api.converters.EmlToPdfRequest; +import stirling.software.common.model.api.converters.HTMLToPdfRequest; +import stirling.software.common.service.CustomPDFDocumentFactory; @Slf4j @UtilityClass public class EmlToPdf { + private static final class StyleConstants { // Font and layout constants static final int DEFAULT_FONT_SIZE = 12; @@ -194,8 +197,7 @@ public class EmlToPdf { boolean disableSanitize) throws IOException, InterruptedException { - stirling.software.common.model.api.converters.HTMLToPdfRequest htmlRequest = - createHtmlRequest(request); + HTMLToPdfRequest htmlRequest = createHtmlRequest(request); try { return FileToPdf.convertHtmlToPdf( @@ -879,33 +881,33 @@ public class EmlToPdf { Class messageClass = message.getClass(); // Extract headers via reflection - java.lang.reflect.Method getSubject = messageClass.getMethod("getSubject"); + Method getSubject = messageClass.getMethod("getSubject"); String subject = (String) getSubject.invoke(message); content.setSubject(subject != null ? safeMimeDecode(subject) : "No Subject"); - java.lang.reflect.Method getFrom = messageClass.getMethod("getFrom"); + Method getFrom = messageClass.getMethod("getFrom"); Object[] fromAddresses = (Object[]) getFrom.invoke(message); content.setFrom( fromAddresses != null && fromAddresses.length > 0 ? safeMimeDecode(fromAddresses[0].toString()) : ""); - java.lang.reflect.Method getAllRecipients = messageClass.getMethod("getAllRecipients"); + Method getAllRecipients = messageClass.getMethod("getAllRecipients"); Object[] recipients = (Object[]) getAllRecipients.invoke(message); content.setTo( recipients != null && recipients.length > 0 ? safeMimeDecode(recipients[0].toString()) : ""); - java.lang.reflect.Method getSentDate = messageClass.getMethod("getSentDate"); + Method getSentDate = messageClass.getMethod("getSentDate"); content.setDate((Date) getSentDate.invoke(message)); // Extract content - java.lang.reflect.Method getContent = messageClass.getMethod("getContent"); + Method getContent = messageClass.getMethod("getContent"); Object messageContent = getContent.invoke(message); if (messageContent instanceof String stringContent) { - java.lang.reflect.Method getContentType = messageClass.getMethod("getContentType"); + Method getContentType = messageClass.getMethod("getContentType"); String contentType = (String) getContentType.invoke(message); if (contentType != null && contentType.toLowerCase().contains("text/html")) { content.setHtmlBody(stringContent); @@ -944,11 +946,10 @@ public class EmlToPdf { } Class multipartClass = multipart.getClass(); - java.lang.reflect.Method getCount = multipartClass.getMethod("getCount"); + Method getCount = multipartClass.getMethod("getCount"); int count = (Integer) getCount.invoke(multipart); - java.lang.reflect.Method getBodyPart = - multipartClass.getMethod("getBodyPart", int.class); + Method getBodyPart = multipartClass.getMethod("getBodyPart", int.class); for (int i = 0; i < count; i++) { Object part = getBodyPart.invoke(multipart, i); @@ -969,12 +970,12 @@ public class EmlToPdf { } Class partClass = part.getClass(); - java.lang.reflect.Method isMimeType = partClass.getMethod("isMimeType", String.class); - java.lang.reflect.Method getContent = partClass.getMethod("getContent"); - java.lang.reflect.Method getDisposition = partClass.getMethod("getDisposition"); - java.lang.reflect.Method getFileName = partClass.getMethod("getFileName"); - java.lang.reflect.Method getContentType = partClass.getMethod("getContentType"); - java.lang.reflect.Method getHeader = partClass.getMethod("getHeader", String.class); + Method isMimeType = partClass.getMethod("isMimeType", String.class); + Method getContent = partClass.getMethod("getContent"); + Method getDisposition = partClass.getMethod("getDisposition"); + Method getFileName = partClass.getMethod("getFileName"); + Method getContentType = partClass.getMethod("getContentType"); + Method getHeader = partClass.getMethod("getHeader", String.class); Object disposition = getDisposition.invoke(part); String filename = (String) getFileName.invoke(part); @@ -1181,7 +1182,7 @@ public class EmlToPdf { private static byte[] attachFilesToPdf( byte[] pdfBytes, List attachments, - stirling.software.common.service.CustomPDFDocumentFactory pdfDocumentFactory) + CustomPDFDocumentFactory pdfDocumentFactory) throws IOException { try (PDDocument document = pdfDocumentFactory.load(pdfBytes); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { @@ -1239,15 +1240,13 @@ public class EmlToPdf { document, new ByteArrayInputStream(attachment.getData())); embeddedFile.setSize(attachment.getData().length); embeddedFile.setCreationDate(new GregorianCalendar()); - if (attachment.getContentType() != null) { - embeddedFile.setSubtype(attachment.getContentType()); - } // Create file specification PDComplexFileSpecification fileSpec = new PDComplexFileSpecification(); fileSpec.setFile(uniqueFilename); fileSpec.setEmbeddedFile(embeddedFile); if (attachment.getContentType() != null) { + embeddedFile.setSubtype(attachment.getContentType()); fileSpec.setFileDescription("Email attachment: " + uniqueFilename); } @@ -1269,7 +1268,7 @@ public class EmlToPdf { efTree.setNames(efMap); // Set catalog viewer preferences to automatically show attachments pane - setCatalogViewerPreferences(document); + setCatalogViewerPreferences(document, PageMode.USE_ATTACHMENTS); } // Add attachment annotations to the first page for each embedded file @@ -1423,41 +1422,7 @@ public class EmlToPdf { } } - private static void setCatalogViewerPreferences(PDDocument document) { - try { - PDDocumentCatalog catalog = document.getDocumentCatalog(); - if (catalog != null) { - // Get the catalog's COS dictionary to work with low-level PDF objects - COSDictionary catalogDict = catalog.getCOSObject(); - - // Set PageMode to UseAttachments - this is the standard PDF specification approach - // PageMode values: UseNone, UseOutlines, UseThumbs, FullScreen, UseOC, - // UseAttachments - catalogDict.setName(COSName.PAGE_MODE, "UseAttachments"); - - // Also set viewer preferences for better attachment viewing experience - COSDictionary viewerPrefs = - (COSDictionary) catalogDict.getDictionaryObject(COSName.VIEWER_PREFERENCES); - if (viewerPrefs == null) { - viewerPrefs = new COSDictionary(); - catalogDict.setItem(COSName.VIEWER_PREFERENCES, viewerPrefs); - } - - // Set NonFullScreenPageMode to UseAttachments as fallback for viewers that support - // it - viewerPrefs.setName(COSName.getPDFName("NonFullScreenPageMode"), "UseAttachments"); - - // Additional viewer preferences that may help with attachment display - viewerPrefs.setBoolean(COSName.getPDFName("DisplayDocTitle"), true); - - log.info( - "Set PDF PageMode to UseAttachments to automatically show attachments pane"); - } - } catch (Exception e) { - // Log warning but don't fail the entire operation for viewer preferences - log.warn("Failed to set catalog viewer preferences for attachments", e); - } - } + // MIME header decoding functionality for RFC 2047 encoded headers - moved to constants private static String decodeMimeHeader(String encodedText) { if (encodedText == null || encodedText.trim().isEmpty()) { diff --git a/common/src/main/java/stirling/software/common/util/WebResponseUtils.java b/common/src/main/java/stirling/software/common/util/WebResponseUtils.java index 62a0e3246..745f5d5ec 100644 --- a/common/src/main/java/stirling/software/common/util/WebResponseUtils.java +++ b/common/src/main/java/stirling/software/common/util/WebResponseUtils.java @@ -16,12 +16,12 @@ import io.github.pixee.security.Filenames; public class WebResponseUtils { - public static ResponseEntity boasToWebResponse( + public static ResponseEntity baosToWebResponse( ByteArrayOutputStream baos, String docName) throws IOException { return WebResponseUtils.bytesToWebResponse(baos.toByteArray(), docName); } - public static ResponseEntity boasToWebResponse( + public static ResponseEntity baosToWebResponse( ByteArrayOutputStream baos, String docName, MediaType mediaType) throws IOException { return WebResponseUtils.bytesToWebResponse(baos.toByteArray(), docName, mediaType); } @@ -44,8 +44,7 @@ public class WebResponseUtils { headers.setContentType(mediaType); headers.setContentLength(bytes.length); String encodedDocName = - URLEncoder.encode(docName, StandardCharsets.UTF_8.toString()) - .replaceAll("\\+", "%20"); + URLEncoder.encode(docName, StandardCharsets.UTF_8).replaceAll("\\+", "%20"); headers.setContentDispositionFormData("attachment", encodedDocName); return new ResponseEntity<>(bytes, headers, HttpStatus.OK); } @@ -61,9 +60,8 @@ public class WebResponseUtils { // Open Byte Array and save document to it ByteArrayOutputStream baos = new ByteArrayOutputStream(); document.save(baos); - // Close the document document.close(); - return boasToWebResponse(baos, docName); + return baosToWebResponse(baos, docName); } } diff --git a/common/src/test/java/stirling/software/common/util/WebResponseUtilsTest.java b/common/src/test/java/stirling/software/common/util/WebResponseUtilsTest.java index f5ce5a6b1..70286fbf7 100644 --- a/common/src/test/java/stirling/software/common/util/WebResponseUtilsTest.java +++ b/common/src/test/java/stirling/software/common/util/WebResponseUtilsTest.java @@ -25,7 +25,7 @@ public class WebResponseUtilsTest { String docName = "sample.pdf"; ResponseEntity responseEntity = - WebResponseUtils.boasToWebResponse(baos, docName); + WebResponseUtils.baosToWebResponse(baos, docName); assertNotNull(responseEntity); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); 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 2e7a197de..361eeace3 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 @@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; + import stirling.software.common.model.ApplicationProperties; @Service @@ -173,6 +174,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 +253,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/MergeController.java b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/MergeController.java index 5e37314a6..ddd988ef9 100644 --- a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/MergeController.java +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/MergeController.java @@ -225,7 +225,7 @@ public class MergeController { String mergedFileName = files[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_merged_unsigned.pdf"; - return WebResponseUtils.boasToWebResponse( + return WebResponseUtils.baosToWebResponse( baos, mergedFileName); // Return the modified PDF } catch (Exception ex) { diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/AttachmentController.java b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/AttachmentController.java new file mode 100644 index 000000000..b36065612 --- /dev/null +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/AttachmentController.java @@ -0,0 +1,57 @@ +package stirling.software.SPDF.controller.api.misc; + +import java.io.IOException; +import java.util.List; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import io.github.pixee.security.Filenames; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import stirling.software.SPDF.model.api.misc.AddAttachmentRequest; +import stirling.software.SPDF.service.AttachmentServiceInterface; +import stirling.software.common.service.CustomPDFDocumentFactory; +import stirling.software.common.util.WebResponseUtils; + +@Slf4j +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1/misc") +@Tag(name = "Misc", description = "Miscellaneous APIs") +public class AttachmentController { + + private final CustomPDFDocumentFactory pdfDocumentFactory; + + private final AttachmentServiceInterface pdfAttachmentService; + + @PostMapping(consumes = "multipart/form-data", value = "/add-attachments") + @Operation( + summary = "Add attachments to PDF", + description = + "This endpoint adds attachments to a PDF. Input:PDF, Output:PDF Type:MISO") + public ResponseEntity addAttachments(@ModelAttribute AddAttachmentRequest request) + throws IOException { + MultipartFile fileInput = request.getFileInput(); + List attachments = request.getAttachments(); + + PDDocument document = + pdfAttachmentService.addAttachment( + pdfDocumentFactory.load(fileInput, false), attachments); + + return WebResponseUtils.pdfDocToWebResponse( + document, + Filenames.toSimpleFileName(fileInput.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_with_attachments.pdf"); + } +} diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java index a7314fc7e..7d5086b4c 100644 --- a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java @@ -144,7 +144,7 @@ public class BlankPageController { zos.close(); log.info("Returning ZIP file: {}", filename + "_processed.zip"); - return WebResponseUtils.boasToWebResponse( + return WebResponseUtils.baosToWebResponse( baos, filename + "_processed.zip", MediaType.APPLICATION_OCTET_STREAM); } catch (IOException e) { diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java index 4ec844485..cb06b9f4d 100644 --- a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java @@ -148,7 +148,7 @@ public class ExtractImagesController { // Create ByteArrayResource from byte array byte[] zipContents = baos.toByteArray(); - return WebResponseUtils.boasToWebResponse( + return WebResponseUtils.baosToWebResponse( baos, filename + "_extracted-images.zip", MediaType.APPLICATION_OCTET_STREAM); } diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineController.java b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineController.java index d573301d0..d6b4fa0da 100644 --- a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineController.java +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineController.java @@ -118,7 +118,7 @@ public class PipelineController { } zipOut.close(); log.info("Returning zipped file response..."); - return WebResponseUtils.boasToWebResponse( + return WebResponseUtils.baosToWebResponse( baos, "output.zip", MediaType.APPLICATION_OCTET_STREAM); } catch (Exception e) { log.error("Error handling data: ", e); diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/security/CertSignController.java b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/security/CertSignController.java index 3260eb31f..612c666c4 100644 --- a/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/security/CertSignController.java +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/controller/api/security/CertSignController.java @@ -205,7 +205,7 @@ public class CertSignController { location, reason, showLogo); - return WebResponseUtils.boasToWebResponse( + return WebResponseUtils.baosToWebResponse( baos, Filenames.toSimpleFileName(pdf.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_signed.pdf"); 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 25333d495..aac9cb6a0 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 @@ -191,4 +191,11 @@ public class OtherWebController { model.addAttribute("currentPage", "auto-rename"); return "misc/auto-rename"; } + + @GetMapping("/add-attachments") + @Hidden + public String attachmentsForm(Model model) { + model.addAttribute("currentPage", "add-attachments"); + return "misc/add-attachments"; + } } diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/model/api/misc/AddAttachmentRequest.java b/stirling-pdf/src/main/java/stirling/software/SPDF/model/api/misc/AddAttachmentRequest.java new file mode 100644 index 000000000..cf85451f4 --- /dev/null +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/model/api/misc/AddAttachmentRequest.java @@ -0,0 +1,23 @@ +package stirling.software.SPDF.model.api.misc; + +import java.util.List; + +import org.springframework.web.multipart.MultipartFile; + +import io.swagger.v3.oas.annotations.media.Schema; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import stirling.software.common.model.api.PDFFile; + +@Data +@EqualsAndHashCode(callSuper = true) +public class AddAttachmentRequest extends PDFFile { + + @Schema( + description = "The image file to be overlaid onto the PDF.", + requiredMode = Schema.RequiredMode.REQUIRED, + format = "binary") + private List attachments; +} diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/service/AttachmentService.java b/stirling-pdf/src/main/java/stirling/software/SPDF/service/AttachmentService.java new file mode 100644 index 000000000..4aa6dfe41 --- /dev/null +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/service/AttachmentService.java @@ -0,0 +1,105 @@ +package stirling.software.SPDF.service; + +import static stirling.software.common.util.AttachmentUtils.setCatalogViewerPreferences; + +import java.io.IOException; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDDocumentCatalog; +import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary; +import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode; +import org.apache.pdfbox.pdmodel.PageMode; +import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification; +import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Service +public class AttachmentService implements AttachmentServiceInterface { + + @Override + public PDDocument addAttachment(PDDocument document, List attachments) + throws IOException { + PDEmbeddedFilesNameTreeNode embeddedFilesTree = getEmbeddedFilesTree(document); + Map existingNames; + + try { + Map names = embeddedFilesTree.getNames(); + + if (names == null) { + log.debug("No existing embedded files found, creating new names map."); + existingNames = new HashMap<>(); + } else { + existingNames = new HashMap<>(names); + log.debug("Embedded files: {}", existingNames.keySet()); + } + } catch (IOException e) { + log.error("Could not retrieve existing embedded files", e); + throw e; + } + + attachments.forEach( + attachment -> { + String filename = attachment.getOriginalFilename(); + + try { + PDEmbeddedFile embeddedFile = + new PDEmbeddedFile(document, attachment.getInputStream()); + embeddedFile.setSize((int) attachment.getSize()); + embeddedFile.setCreationDate(new GregorianCalendar()); + embeddedFile.setModDate(new GregorianCalendar()); + String contentType = attachment.getContentType(); + if (StringUtils.isNotBlank(contentType)) { + embeddedFile.setSubtype(contentType); + } + + // Create attachments specification and associate embedded attachment with + // file + PDComplexFileSpecification fileSpecification = + new PDComplexFileSpecification(); + fileSpecification.setFile(filename); + fileSpecification.setFileUnicode(filename); + fileSpecification.setFileDescription("Embedded attachment: " + filename); + fileSpecification.setEmbeddedFile(embeddedFile); + fileSpecification.setEmbeddedFileUnicode(embeddedFile); + + existingNames.put(filename, fileSpecification); + + log.info("Added attachment: {} ({} bytes)", filename, attachment.getSize()); + } catch (IOException e) { + log.warn("Failed to create embedded file for attachment: {}", filename, e); + } + }); + + embeddedFilesTree.setNames(existingNames); + setCatalogViewerPreferences(document, PageMode.USE_ATTACHMENTS); + + return document; + } + + private PDEmbeddedFilesNameTreeNode getEmbeddedFilesTree(PDDocument document) { + PDDocumentCatalog catalog = document.getDocumentCatalog(); + PDDocumentNameDictionary documentNames = catalog.getNames(); + + if (documentNames == null) { + documentNames = new PDDocumentNameDictionary(catalog); + } + + catalog.setNames(documentNames); + PDEmbeddedFilesNameTreeNode embeddedFilesTree = documentNames.getEmbeddedFiles(); + + if (embeddedFilesTree == null) { + embeddedFilesTree = new PDEmbeddedFilesNameTreeNode(); + documentNames.setEmbeddedFiles(embeddedFilesTree); + } + return embeddedFilesTree; + } +} diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/service/AttachmentServiceInterface.java b/stirling-pdf/src/main/java/stirling/software/SPDF/service/AttachmentServiceInterface.java new file mode 100644 index 000000000..c684a429d --- /dev/null +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/service/AttachmentServiceInterface.java @@ -0,0 +1,13 @@ +package stirling.software.SPDF.service; + +import java.io.IOException; +import java.util.List; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.springframework.web.multipart.MultipartFile; + +public interface AttachmentServiceInterface { + + PDDocument addAttachment(PDDocument document, List attachments) + throws IOException; +} diff --git a/stirling-pdf/src/main/resources/messages_en_GB.properties b/stirling-pdf/src/main/resources/messages_en_GB.properties index 22cbfaf17..d28657b91 100644 --- a/stirling-pdf/src/main/resources/messages_en_GB.properties +++ b/stirling-pdf/src/main/resources/messages_en_GB.properties @@ -525,6 +525,10 @@ home.addImage.title=Add image home.addImage.desc=Adds a image onto a set location on the PDF addImage.tags=img,jpg,picture,photo +home.attachments.title=Add Attachments +home.attachments.desc=Add or remove embedded files (attachments) to/from a PDF +attachments.tags=embed,attach,file,attachment,attachments + home.watermark.title=Add Watermark home.watermark.desc=Add a custom watermark to your PDF document. watermark.tags=Text,repeating,label,own,copyright,trademark,img,jpg,picture,photo @@ -1205,6 +1209,12 @@ addImage.everyPage=Every Page? addImage.upload=Add image addImage.submit=Add image +#attachments +attachments.title=Add Attachments +attachments.header=Add attachments +attachments.description=Allows you to add attachments to the PDF +attachments.descriptionPlaceholder=Enter a description for the attachments... +attachments.addButton=Add Attachments #merge merge.title=Merge @@ -1594,6 +1604,7 @@ fileChooser.dragAndDropPDF=Drag & Drop PDF file fileChooser.dragAndDropImage=Drag & Drop Image file fileChooser.hoveredDragAndDrop=Drag & Drop file(s) here fileChooser.extractPDF=Extracting... +fileChooser.addAttachments=drag & drop attachments here #release notes releases.footer=Releases diff --git a/stirling-pdf/src/main/resources/static/js/file-icon-factory.js b/stirling-pdf/src/main/resources/static/js/file-icon-factory.js index 81328f423..645ae323e 100644 --- a/stirling-pdf/src/main/resources/static/js/file-icon-factory.js +++ b/stirling-pdf/src/main/resources/static/js/file-icon-factory.js @@ -6,6 +6,9 @@ class FileIconFactory { return this.createPDFIcon(); case "csv": return this.createCSVIcon(); + case "xls": + case "xlsx": + return this.createXLSXIcon(); case "jpe": case "jpg": case "jpeg": @@ -44,8 +47,29 @@ class FileIconFactory { return ``; } + static createCSVIcon() { + return ` + + + + `; + } + + static createXLSXIcon() { + return ` + + + + + `; + } + static createUnknownFileIcon() { - return ``; + return ` + + + + `; } } diff --git a/stirling-pdf/src/main/resources/static/js/fileInput.js b/stirling-pdf/src/main/resources/static/js/fileInput.js index e874ea1c2..f06371986 100644 --- a/stirling-pdf/src/main/resources/static/js/fileInput.js +++ b/stirling-pdf/src/main/resources/static/js/fileInput.js @@ -45,6 +45,8 @@ function setupFileInput(chooser) { inputContainer.querySelector('#dragAndDrop').innerHTML = window.fileInput.dragAndDropPDF; } else if (inputContainer.id === 'image-upload-input-container') { inputContainer.querySelector('#dragAndDrop').innerHTML = window.fileInput.dragAndDropImage; + } else if (inputContainer.id === 'attachments-input-container') { + inputContainer.querySelector('#dragAndDrop').innerHTML = window.fileInput.addAttachments; } let allFiles = []; let overlay; diff --git a/stirling-pdf/src/main/resources/templates/fragments/common.html b/stirling-pdf/src/main/resources/templates/fragments/common.html index 02d919b2b..38e71f04f 100644 --- a/stirling-pdf/src/main/resources/templates/fragments/common.html +++ b/stirling-pdf/src/main/resources/templates/fragments/common.html @@ -268,6 +268,7 @@ window.fileInput = { dragAndDropPDF: '[[#{fileChooser.dragAndDropPDF}]]', dragAndDropImage: '[[#{fileChooser.dragAndDropImage}]]', + addAttachments: '[[#{fileChooser.addAttachments}]]', extractPDF: '[[#{fileChooser.extractPDF}]]', loading: '[[#{loading}]]' }; diff --git a/stirling-pdf/src/main/resources/templates/fragments/navElements.html b/stirling-pdf/src/main/resources/templates/fragments/navElements.html index cd7fae74b..38dfa0ac6 100644 --- a/stirling-pdf/src/main/resources/templates/fragments/navElements.html +++ b/stirling-pdf/src/main/resources/templates/fragments/navElements.html @@ -236,7 +236,10 @@
-
+ +
+
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/add-attachments.html b/stirling-pdf/src/main/resources/templates/misc/add-attachments.html new file mode 100644 index 000000000..60bb16c96 --- /dev/null +++ b/stirling-pdf/src/main/resources/templates/misc/add-attachments.html @@ -0,0 +1,42 @@ + + + + + + + + +
+
+ +

+
+
+
+
+ attachment + +
+ +
+ +
+
+ + +
+
+ + + +
+
+
+
+
+ +
+ + + \ No newline at end of file diff --git a/stirling-pdf/src/test/java/stirling/software/SPDF/controller/api/misc/AttachmentControllerTest.java b/stirling-pdf/src/test/java/stirling/software/SPDF/controller/api/misc/AttachmentControllerTest.java new file mode 100644 index 000000000..9047bffb5 --- /dev/null +++ b/stirling-pdf/src/test/java/stirling/software/SPDF/controller/api/misc/AttachmentControllerTest.java @@ -0,0 +1,133 @@ +package stirling.software.SPDF.controller.api.misc; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import org.mockito.MockedStatic; + +import java.io.IOException; +import java.util.List; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.web.multipart.MultipartFile; + +import stirling.software.SPDF.model.api.misc.AddAttachmentRequest; +import stirling.software.SPDF.service.AttachmentServiceInterface; +import stirling.software.common.service.CustomPDFDocumentFactory; +import stirling.software.common.util.WebResponseUtils; + +@ExtendWith(MockitoExtension.class) +class AttachmentControllerTest { + + @Mock + private CustomPDFDocumentFactory pdfDocumentFactory; + + @Mock + private AttachmentServiceInterface pdfAttachmentService; + + @InjectMocks + private AttachmentController attachmentController; + + private MockMultipartFile pdfFile; + private MockMultipartFile attachment1; + private MockMultipartFile attachment2; + private AddAttachmentRequest request; + private PDDocument mockDocument; + private PDDocument modifiedMockDocument; + + @BeforeEach + void setUp() { + pdfFile = new MockMultipartFile("fileInput", "test.pdf", "application/pdf", "PDF content".getBytes()); + attachment1 = new MockMultipartFile("attachment1", "file1.txt", "text/plain", "File 1 content".getBytes()); + attachment2 = new MockMultipartFile("attachment2", "file2.jpg", "image/jpeg", "Image content".getBytes()); + request = new AddAttachmentRequest(); + mockDocument = mock(PDDocument.class); + modifiedMockDocument = mock(PDDocument.class); + } + + @Test + void addAttachments_Success() throws IOException { + List attachments = List.of(attachment1, attachment2); + request.setAttachments(attachments); + request.setFileInput(pdfFile); + ResponseEntity expectedResponse = ResponseEntity.ok("modified PDF content".getBytes()); + + when(pdfDocumentFactory.load(pdfFile, false)).thenReturn(mockDocument); + when(pdfAttachmentService.addAttachment(mockDocument, attachments)).thenReturn(modifiedMockDocument); + + try (MockedStatic mockedWebResponseUtils = mockStatic(WebResponseUtils.class)) { + mockedWebResponseUtils.when(() -> WebResponseUtils.pdfDocToWebResponse(eq(modifiedMockDocument), eq("test_with_attachments.pdf"))) + .thenReturn(expectedResponse); + + ResponseEntity response = attachmentController.addAttachments(request); + + assertNotNull(response); + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertNotNull(response.getBody()); + verify(pdfDocumentFactory).load(pdfFile, false); + verify(pdfAttachmentService).addAttachment(mockDocument, attachments); + } + } + + @Test + void addAttachments_SingleAttachment() throws IOException { + List attachments = List.of(attachment1); + request.setAttachments(attachments); + request.setFileInput(pdfFile); + ResponseEntity expectedResponse = ResponseEntity.ok("modified PDF content".getBytes()); + + when(pdfDocumentFactory.load(pdfFile, false)).thenReturn(mockDocument); + when(pdfAttachmentService.addAttachment(mockDocument, attachments)).thenReturn(modifiedMockDocument); + + try (MockedStatic mockedWebResponseUtils = mockStatic(WebResponseUtils.class)) { + mockedWebResponseUtils.when(() -> WebResponseUtils.pdfDocToWebResponse(eq(modifiedMockDocument), eq("test_with_attachments.pdf"))) + .thenReturn(expectedResponse); + + ResponseEntity response = attachmentController.addAttachments(request); + + assertNotNull(response); + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertNotNull(response.getBody()); + verify(pdfDocumentFactory).load(pdfFile, false); + verify(pdfAttachmentService).addAttachment(mockDocument, attachments); + } + } + + @Test + void addAttachments_IOExceptionFromPDFLoad() throws IOException { + List attachments = List.of(attachment1); + request.setAttachments(attachments); + request.setFileInput(pdfFile); + IOException ioException = new IOException("Failed to load PDF"); + + when(pdfDocumentFactory.load(pdfFile, false)).thenThrow(ioException); + + assertThrows(IOException.class, () -> attachmentController.addAttachments(request)); + verify(pdfDocumentFactory).load(pdfFile, false); + verifyNoInteractions(pdfAttachmentService); + } + + @Test + void addAttachments_IOExceptionFromAttachmentService() throws IOException { + List attachments = List.of(attachment1); + request.setAttachments(attachments); + request.setFileInput(pdfFile); + IOException ioException = new IOException("Failed to add attachment"); + + when(pdfDocumentFactory.load(pdfFile, false)).thenReturn(mockDocument); + when(pdfAttachmentService.addAttachment(mockDocument, attachments)).thenThrow(ioException); + + assertThrows(IOException.class, () -> attachmentController.addAttachments(request)); + verify(pdfAttachmentService).addAttachment(mockDocument, attachments); + } +} diff --git a/stirling-pdf/src/test/java/stirling/software/SPDF/service/AttachmentServiceTest.java b/stirling-pdf/src/test/java/stirling/software/SPDF/service/AttachmentServiceTest.java new file mode 100644 index 000000000..837530533 --- /dev/null +++ b/stirling-pdf/src/test/java/stirling/software/SPDF/service/AttachmentServiceTest.java @@ -0,0 +1,105 @@ +package stirling.software.SPDF.service; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.List; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.web.multipart.MultipartFile; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class AttachmentServiceTest { + + private AttachmentService attachmentService; + + @BeforeEach + void setUp() { + attachmentService = new AttachmentService(); + } + + @Test + void addAttachmentToPDF() throws IOException { + try (var document = new PDDocument()) { + document.setDocumentId(100L); + var attachments = List.of(mock(MultipartFile.class)); + + when(attachments.get(0).getOriginalFilename()).thenReturn("test.txt"); + when(attachments.get(0).getInputStream()).thenReturn( + new ByteArrayInputStream("Test content".getBytes())); + when(attachments.get(0).getSize()).thenReturn(12L); + when(attachments.get(0).getContentType()).thenReturn("text/plain"); + + PDDocument result = attachmentService.addAttachment(document, attachments); + + assertNotNull(result); + assertEquals(document.getDocumentId(), result.getDocumentId()); + assertNotNull(result.getDocumentCatalog().getNames()); + } + } + + @Test + void addAttachmentToPDF_MultipleAttachments() throws IOException { + try (var document = new PDDocument()) { + document.setDocumentId(100L); + var attachment1 = mock(MultipartFile.class); + var attachment2 = mock(MultipartFile.class); + var attachments = List.of(attachment1, attachment2); + + when(attachment1.getOriginalFilename()).thenReturn("document.pdf"); + when(attachment1.getInputStream()).thenReturn( + new ByteArrayInputStream("PDF content".getBytes())); + when(attachment1.getSize()).thenReturn(15L); + when(attachment1.getContentType()).thenReturn("application/pdf"); + + when(attachment2.getOriginalFilename()).thenReturn("image.jpg"); + when(attachment2.getInputStream()).thenReturn( + new ByteArrayInputStream("Image content".getBytes())); + when(attachment2.getSize()).thenReturn(20L); + when(attachment2.getContentType()).thenReturn("image/jpeg"); + + PDDocument result = attachmentService.addAttachment(document, attachments); + + assertNotNull(result); + assertNotNull(result.getDocumentCatalog().getNames()); + } + } + + @Test + void addAttachmentToPDF_WithBlankContentType() throws IOException { + try (var document = new PDDocument()) { + document.setDocumentId(100L); + var attachments = List.of(mock(MultipartFile.class)); + + when(attachments.get(0).getOriginalFilename()).thenReturn("image.jpg"); + when(attachments.get(0).getInputStream()).thenReturn( + new ByteArrayInputStream("Image content".getBytes())); + when(attachments.get(0).getSize()).thenReturn(25L); + when(attachments.get(0).getContentType()).thenReturn(""); + + PDDocument result = attachmentService.addAttachment(document, attachments); + + assertNotNull(result); + assertNotNull(result.getDocumentCatalog().getNames()); + } + } + + @Test + void addAttachmentToPDF_AttachmentInputStreamThrowsIOException() throws IOException { + try (var document = new PDDocument()) { + var attachments = List.of(mock(MultipartFile.class)); + var ioException = new IOException("Failed to read attachment stream"); + + when(attachments.get(0).getOriginalFilename()).thenReturn("test.txt"); + when(attachments.get(0).getInputStream()).thenThrow(ioException); + when(attachments.get(0).getSize()).thenReturn(10L); + + PDDocument result = attachmentService.addAttachment(document, attachments); + + assertNotNull(result); + assertNotNull(result.getDocumentCatalog().getNames()); + } + } +} 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