From 8260eced2d4f81e2e88a1b92edd34f19e1c55fe1 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Tue, 25 Jul 2023 23:36:19 +0100 Subject: [PATCH] auto split cleanup --- .../api/other/AutoSplitPdfController.java | 28 +++++++++++-------- .../resources/templates/auto-split-pdf.html | 4 +++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/other/AutoSplitPdfController.java b/src/main/java/stirling/software/SPDF/controller/api/other/AutoSplitPdfController.java index e69c0597..9e08e9a7 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/other/AutoSplitPdfController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/other/AutoSplitPdfController.java @@ -41,20 +41,22 @@ public class AutoSplitPdfController { @PostMapping(value = "/auto-split-pdf", consumes = "multipart/form-data") @Operation(summary = "Auto split PDF pages into separate documents", description = "This endpoint accepts a PDF file, scans each page for a specific QR code, and splits the document at the QR code boundaries. The output is a zip file containing each separate PDF document. Input:PDF Output:ZIP Type:SISO") public ResponseEntity autoSplitPdf( - @RequestParam("fileInput") @Parameter(description = "The input PDF file which needs to be split into separate documents based on QR code boundaries.", required = true) MultipartFile file) + @RequestParam("fileInput") @Parameter(description = "The input PDF file which needs to be split into separate documents based on QR code boundaries.", required = true) MultipartFile file, + @RequestParam(value ="duplexMode",defaultValue = "false") @Parameter(description = "Flag indicating if the duplex mode is active, where the page after the divider also gets removed.", required = false) boolean duplexMode) throws IOException { + InputStream inputStream = file.getInputStream(); PDDocument document = PDDocument.load(inputStream); PDFRenderer pdfRenderer = new PDFRenderer(document); List splitDocuments = new ArrayList<>(); - List splitDocumentsBoas = new ArrayList<>(); // create this list to store ByteArrayOutputStreams for zipping + List splitDocumentsBoas = new ArrayList<>(); for (int page = 0; page < document.getNumberOfPages(); ++page) { BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 150); String result = decodeQRCode(bim); - - if(QR_CONTENT.equals(result) && page != 0) { + + if (QR_CONTENT.equals(result) && page != 0) { splitDocuments.add(new PDDocument()); } @@ -65,9 +67,16 @@ public class AutoSplitPdfController { firstDocument.addPage(document.getPage(page)); splitDocuments.add(firstDocument); } + + // If duplexMode is true and current page is a divider, then skip next page + if (duplexMode && QR_CONTENT.equals(result)) { + page++; + } } - // After all pages are added to splitDocuments, convert each to ByteArrayOutputStream and add to splitDocumentsBoas + // Remove split documents that have no pages + splitDocuments.removeIf(pdDocument -> pdDocument.getNumberOfPages() == 0); + for (PDDocument splitDocument : splitDocuments) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); splitDocument.save(baos); @@ -77,18 +86,16 @@ public class AutoSplitPdfController { document.close(); - // After this line, you can find your zip logic integrated Path zipFile = Files.createTempFile("split_documents", ".zip"); String filename = file.getOriginalFilename().replaceFirst("[.][^.]+$", ""); byte[] data; + try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipFile))) { - // loop through the split documents and write them to the zip file for (int i = 0; i < splitDocumentsBoas.size(); i++) { - String fileName = filename + "_" + (i + 1) + ".pdf"; // You should replace "originalFileName" with the real file name + String fileName = filename + "_" + (i + 1) + ".pdf"; ByteArrayOutputStream baos = splitDocumentsBoas.get(i); byte[] pdf = baos.toByteArray(); - // Add PDF file to the zip ZipEntry pdfEntry = new ZipEntry(fileName); zipOut.putNextEntry(pdfEntry); zipOut.write(pdf); @@ -101,9 +108,6 @@ public class AutoSplitPdfController { Files.delete(zipFile); } - - - // return the Resource in the response return WebResponseUtils.bytesToWebResponse(data, filename + ".zip", MediaType.APPLICATION_OCTET_STREAM); } diff --git a/src/main/resources/templates/auto-split-pdf.html b/src/main/resources/templates/auto-split-pdf.html index 8085f4d0..4f3045e0 100644 --- a/src/main/resources/templates/auto-split-pdf.html +++ b/src/main/resources/templates/auto-split-pdf.html @@ -25,6 +25,10 @@

+
+ + +