mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-23 16:05:09 +00:00
wip - fixing embed feature
This commit is contained in:
parent
0f5c549fb4
commit
a0b176b076
@ -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");
|
||||
|
@ -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<byte[]> 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
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
@ -22,18 +22,28 @@ public class PDFAttachmentService implements PDFAttachmentServiceInterface {
|
||||
@Override
|
||||
public void addAttachment(
|
||||
PDDocument document,
|
||||
PDEmbeddedFilesNameTreeNode efTree,
|
||||
PDEmbeddedFilesNameTreeNode embeddedFilesTree,
|
||||
List<MultipartFile> attachments)
|
||||
throws IOException {
|
||||
// Get existing names or create new map
|
||||
Map<String, PDComplexFileSpecification> existingNames = new java.util.HashMap<>();
|
||||
// todo: sanitize attachments first
|
||||
// todo: find out how to access the embedded files in the PDF
|
||||
Map<String, PDComplexFileSpecification> existingNames;
|
||||
|
||||
try {
|
||||
existingNames = efTree.getNames();
|
||||
} catch (IOException e) {
|
||||
log.warn("Could not retrieve existing embedded files, starting with empty map", e);
|
||||
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<>();
|
||||
}
|
||||
|
||||
Map<String, PDComplexFileSpecification> finalExistingNames = existingNames;
|
||||
log.debug("Existing embedded files: {}", existingNames.keySet());
|
||||
} catch (IOException e) {
|
||||
log.error("Could not retrieve existing embedded files", e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
final Map<String, PDComplexFileSpecification> 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);
|
||||
}
|
||||
}
|
||||
|
@ -238,7 +238,7 @@
|
||||
th:replace="~{fragments/navbarEntry :: navbarEntry('unlock-pdf-forms', 'preview_off', 'home.unlockPDFForms.title', 'home.unlockPDFForms.desc', 'unlockPDFForms.tags', 'other')}">
|
||||
</div>
|
||||
<div
|
||||
th:replace="~{fragments/navbarEntry :: navbarEntry('attachments', 'attachment', 'home.attachments.title', 'home.attachments.desc', 'attachments.tags', 'other')}">
|
||||
th:replace="~{fragments/navbarEntry :: navbarEntry('add-attachments', 'attachment', 'home.attachments.title', 'home.attachments.desc', 'attachments.tags', 'other')}">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -290,6 +290,9 @@
|
||||
<div
|
||||
th:replace="~{fragments/card :: card(id='unlock-pdf-forms', cardTitle=#{home.unlockPDFForms.title}, cardText=#{home.unlockPDFForms.desc}, cardLink='unlock-pdf-forms', toolIcon='preview_off', tags=#{unlockPDFForms.tags}, toolGroup='other')}">
|
||||
</div>
|
||||
<div
|
||||
th:replace="~{fragments/navbarEntry :: navbarEntry('add-attachments', 'attachment', 'home.attachments.title', 'home.attachments.desc', 'attachments.tags', 'other')}">
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
@ -51,3 +51,4 @@
|
||||
/swagger-ui/index.html
|
||||
/licenses
|
||||
/releases
|
||||
/add-attachments
|
||||
|
Loading…
x
Reference in New Issue
Block a user