mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-23 07:55:07 +00:00
wip - working on opening attachments in a viewer
This commit is contained in:
parent
a0b176b076
commit
279dc3f912
@ -42,7 +42,7 @@ public class AttachmentsController {
|
|||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
// Load the PDF document
|
// Load the PDF document
|
||||||
PDDocument document = pdfDocumentFactory.load(pdfFile, true);
|
PDDocument document = pdfDocumentFactory.load(pdfFile, false);
|
||||||
|
|
||||||
// Get or create the document catalog
|
// Get or create the document catalog
|
||||||
PDDocumentCatalog catalog = document.getDocumentCatalog();
|
PDDocumentCatalog catalog = document.getDocumentCatalog();
|
||||||
@ -65,7 +65,6 @@ public class AttachmentsController {
|
|||||||
// Set PageMode to UseAttachments to show the attachments panel
|
// Set PageMode to UseAttachments to show the attachments panel
|
||||||
catalog.setPageMode(PageMode.USE_ATTACHMENTS);
|
catalog.setPageMode(PageMode.USE_ATTACHMENTS);
|
||||||
|
|
||||||
// Return the modified PDF
|
|
||||||
return WebResponseUtils.pdfDocToWebResponse(
|
return WebResponseUtils.pdfDocToWebResponse(
|
||||||
document,
|
document,
|
||||||
Filenames.toSimpleFileName(pdfFile.getOriginalFilename())
|
Filenames.toSimpleFileName(pdfFile.getOriginalFilename())
|
||||||
@ -82,7 +81,7 @@ public class AttachmentsController {
|
|||||||
@RequestParam("fileInput") MultipartFile pdfFile) throws IOException {
|
@RequestParam("fileInput") MultipartFile pdfFile) throws IOException {
|
||||||
|
|
||||||
// Load the PDF document and document catalog
|
// Load the PDF document and document catalog
|
||||||
PDDocument document = pdfDocumentFactory.load(pdfFile, true);
|
PDDocument document = pdfDocumentFactory.load(pdfFile);
|
||||||
PDDocumentCatalog catalog = document.getDocumentCatalog();
|
PDDocumentCatalog catalog = document.getDocumentCatalog();
|
||||||
|
|
||||||
// Remove embedded files
|
// Remove embedded files
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package stirling.software.SPDF.service;
|
package stirling.software.SPDF.service;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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.PDEmbeddedFilesNameTreeNode;
|
||||||
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
|
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
|
||||||
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
|
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
|
||||||
|
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@ -25,19 +25,16 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface {
|
|||||||
PDEmbeddedFilesNameTreeNode embeddedFilesTree,
|
PDEmbeddedFilesNameTreeNode embeddedFilesTree,
|
||||||
List<MultipartFile> attachments)
|
List<MultipartFile> attachments)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
// todo: sanitize attachments first
|
|
||||||
// todo: find out how to access the embedded files in the PDF
|
|
||||||
Map<String, PDComplexFileSpecification> existingNames;
|
Map<String, PDComplexFileSpecification> existingNames;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
existingNames = embeddedFilesTree.getNames();
|
existingNames = embeddedFilesTree.getNames();
|
||||||
if (existingNames == null) {
|
if (existingNames == null) {
|
||||||
log.info("No existing embedded files found, creating new names map.");
|
log.debug("No existing embedded files found, creating new names map.");
|
||||||
// Initialize an empty map if no existing names are found
|
|
||||||
existingNames = new java.util.HashMap<>();
|
existingNames = new java.util.HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
log.debug("Existing embedded files: {}", existingNames.keySet());
|
log.debug("Embedded files: {}", existingNames.keySet());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Could not retrieve existing embedded files", e);
|
log.error("Could not retrieve existing embedded files", e);
|
||||||
throw e;
|
throw e;
|
||||||
@ -49,15 +46,17 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface {
|
|||||||
// Create attachments specification
|
// Create attachments specification
|
||||||
PDComplexFileSpecification fileSpecification = new PDComplexFileSpecification();
|
PDComplexFileSpecification fileSpecification = new PDComplexFileSpecification();
|
||||||
fileSpecification.setFile(attachment.getOriginalFilename());
|
fileSpecification.setFile(attachment.getOriginalFilename());
|
||||||
|
fileSpecification.setFileUnicode(attachment.getOriginalFilename());
|
||||||
fileSpecification.setFileDescription(
|
fileSpecification.setFileDescription(
|
||||||
"Embedded attachment: " + attachment.getOriginalFilename());
|
"Embedded attachment: " + attachment.getOriginalFilename());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Create embedded attachment
|
||||||
PDEmbeddedFile embeddedFile =
|
PDEmbeddedFile embeddedFile =
|
||||||
new PDEmbeddedFile(
|
new PDEmbeddedFile(document, attachment.getInputStream());
|
||||||
document, new ByteArrayInputStream(attachment.getBytes()));
|
|
||||||
embeddedFile.setSize((int) attachment.getSize());
|
embeddedFile.setSize((int) attachment.getSize());
|
||||||
embeddedFile.setCreationDate(new java.util.GregorianCalendar());
|
embeddedFile.setCreationDate(new java.util.GregorianCalendar());
|
||||||
|
embeddedFile.setFile(fileSpecification);
|
||||||
embeddedFile.setModDate(new java.util.GregorianCalendar());
|
embeddedFile.setModDate(new java.util.GregorianCalendar());
|
||||||
|
|
||||||
// Set MIME type if available
|
// Set MIME type if available
|
||||||
@ -68,9 +67,11 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface {
|
|||||||
|
|
||||||
// Associate embedded attachment with file specification
|
// Associate embedded attachment with file specification
|
||||||
fileSpecification.setEmbeddedFile(embeddedFile);
|
fileSpecification.setEmbeddedFile(embeddedFile);
|
||||||
|
fileSpecification.setEmbeddedFileUnicode(embeddedFile);
|
||||||
|
|
||||||
// Add to the existing names map
|
// Add to the existing names map
|
||||||
existingEmbeddedFiles.put(attachment.getOriginalFilename(), fileSpecification);
|
existingEmbeddedFiles.put(
|
||||||
|
attachment.getOriginalFilename(), fileSpecification);
|
||||||
|
|
||||||
log.info(
|
log.info(
|
||||||
"Added attachment: {} ({} bytes)",
|
"Added attachment: {} ({} bytes)",
|
||||||
@ -85,5 +86,23 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface {
|
|||||||
});
|
});
|
||||||
|
|
||||||
embeddedFilesTree.setNames(existingNames);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user