wip - working on opening attachments in a viewer

This commit is contained in:
Dario Ghunney Ware 2025-06-16 16:39:59 +01:00
parent a0b176b076
commit 279dc3f912
2 changed files with 30 additions and 12 deletions

View File

@ -42,7 +42,7 @@ public class AttachmentsController {
throws IOException {
// Load the PDF document
PDDocument document = pdfDocumentFactory.load(pdfFile, true);
PDDocument document = pdfDocumentFactory.load(pdfFile, false);
// Get or create the document catalog
PDDocumentCatalog catalog = document.getDocumentCatalog();
@ -65,7 +65,6 @@ public class AttachmentsController {
// Set PageMode to UseAttachments to show the attachments panel
catalog.setPageMode(PageMode.USE_ATTACHMENTS);
// Return the modified PDF
return WebResponseUtils.pdfDocToWebResponse(
document,
Filenames.toSimpleFileName(pdfFile.getOriginalFilename())
@ -82,7 +81,7 @@ public class AttachmentsController {
@RequestParam("fileInput") MultipartFile pdfFile) throws IOException {
// Load the PDF document and document catalog
PDDocument document = pdfDocumentFactory.load(pdfFile, true);
PDDocument document = pdfDocumentFactory.load(pdfFile);
PDDocumentCatalog catalog = document.getDocumentCatalog();
// Remove embedded files

View File

@ -1,6 +1,5 @@
package stirling.software.SPDF.service;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@ -10,6 +9,7 @@ import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@ -25,19 +25,16 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface {
PDEmbeddedFilesNameTreeNode embeddedFilesTree,
List<MultipartFile> attachments)
throws IOException {
// todo: sanitize attachments first
// todo: find out how to access the embedded files in the PDF
Map<String, PDComplexFileSpecification> existingNames;
try {
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
log.debug("No existing embedded files found, creating new names map.");
existingNames = new java.util.HashMap<>();
}
log.debug("Existing embedded files: {}", existingNames.keySet());
log.debug("Embedded files: {}", existingNames.keySet());
} catch (IOException e) {
log.error("Could not retrieve existing embedded files", e);
throw e;
@ -49,15 +46,17 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface {
// Create attachments specification
PDComplexFileSpecification fileSpecification = new PDComplexFileSpecification();
fileSpecification.setFile(attachment.getOriginalFilename());
fileSpecification.setFileUnicode(attachment.getOriginalFilename());
fileSpecification.setFileDescription(
"Embedded attachment: " + attachment.getOriginalFilename());
try {
// Create embedded attachment
PDEmbeddedFile embeddedFile =
new PDEmbeddedFile(
document, new ByteArrayInputStream(attachment.getBytes()));
new PDEmbeddedFile(document, attachment.getInputStream());
embeddedFile.setSize((int) attachment.getSize());
embeddedFile.setCreationDate(new java.util.GregorianCalendar());
embeddedFile.setFile(fileSpecification);
embeddedFile.setModDate(new java.util.GregorianCalendar());
// Set MIME type if available
@ -68,9 +67,11 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface {
// Associate embedded attachment with file specification
fileSpecification.setEmbeddedFile(embeddedFile);
fileSpecification.setEmbeddedFileUnicode(embeddedFile);
// Add to the existing names map
existingEmbeddedFiles.put(attachment.getOriginalFilename(), fileSpecification);
existingEmbeddedFiles.put(
attachment.getOriginalFilename(), fileSpecification);
log.info(
"Added attachment: {} ({} bytes)",
@ -85,5 +86,23 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface {
});
embeddedFilesTree.setNames(existingNames);
// Ensure document has proper access permissions for embedded files
grantAccessPermissions(document);
}
private void grantAccessPermissions(PDDocument document) {
AccessPermission currentPermissions = document.getCurrentAccessPermission();
currentPermissions.setCanAssembleDocument(true);
currentPermissions.setCanFillInForm(currentPermissions.canFillInForm());
currentPermissions.setCanModify(true);
currentPermissions.setCanPrint(true);
currentPermissions.setCanPrintFaithful(true);
// Ensure these permissions are enabled for embedded file access
currentPermissions.setCanExtractContent(true);
currentPermissions.setCanExtractForAccessibility(true);
currentPermissions.setCanModifyAnnotations(true);
}
}