From 5e4de6cc5f9a9e58c21ec77b1b38f3a56eeb7fd6 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Fri, 26 May 2023 14:39:08 +0100 Subject: [PATCH] some filename fixes for nicer extraction --- .../controller/api/converters/ConvertImgPDFController.java | 5 +++-- .../SPDF/controller/api/other/ExtractImagesController.java | 6 +++--- src/main/java/stirling/software/SPDF/utils/PdfUtils.java | 4 ++-- src/main/resources/templates/fragments/common.html | 5 ++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java index 05afb751..74f87cce 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java @@ -56,8 +56,9 @@ public class ConvertImgPDFController { // returns bytes for image boolean singleImage = singleOrMultiple.equals("single"); byte[] result = null; + String filename = file.getOriginalFilename().replaceFirst("[.][^.]+$", ""); try { - result = PdfUtils.convertFromPdf(pdfBytes, imageFormat.toUpperCase(), colorTypeResult, singleImage, Integer.valueOf(dpi)); + result = PdfUtils.convertFromPdf(pdfBytes, imageFormat.toUpperCase(), colorTypeResult, singleImage, Integer.valueOf(dpi), filename); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -74,7 +75,7 @@ public class ConvertImgPDFController { ByteArrayResource resource = new ByteArrayResource(result); // return the Resource in the response return ResponseEntity.ok() - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_convertedToImages.zip") + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename + "_convertedToImages.zip") .contentType(MediaType.APPLICATION_OCTET_STREAM).contentLength(resource.contentLength()).body(resource); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/other/ExtractImagesController.java b/src/main/java/stirling/software/SPDF/controller/api/other/ExtractImagesController.java index cf31dda7..60313644 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/other/ExtractImagesController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/other/ExtractImagesController.java @@ -59,7 +59,7 @@ public class ExtractImagesController { zos.setLevel(Deflater.BEST_COMPRESSION); int imageIndex = 1; - + String filename = file.getOriginalFilename().replaceFirst("[.][^.]+$", ""); int pageNum = 1; // Iterate over each page for (PDPage page : document.getPages()) { @@ -81,7 +81,7 @@ public class ExtractImagesController { } // Write image to zip file - String imageName = "Image " + imageIndex + " (Page " + pageNum + ")." + format; + String imageName = filename + "_" + imageIndex + " (Page " + pageNum + ")." + format; ZipEntry zipEntry = new ZipEntry(imageName); zos.putNextEntry(zipEntry); @@ -106,7 +106,7 @@ public class ExtractImagesController { // Create ByteArrayResource from byte array byte[] zipContents = baos.toByteArray(); - return PdfUtils.boasToWebResponse(baos, file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_extracted-images.zip", MediaType.APPLICATION_OCTET_STREAM); + return PdfUtils.boasToWebResponse(baos, filename + "_extracted-images.zip", MediaType.APPLICATION_OCTET_STREAM); } } diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 337e0335..8b9b3bd2 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -69,7 +69,7 @@ public class PdfUtils { return bytesToWebResponse(bytes, docName, MediaType.APPLICATION_PDF); } - public static byte[] convertFromPdf(byte[] inputStream, String imageType, ImageType colorType, boolean singleImage, int DPI) throws IOException, Exception { + public static byte[] convertFromPdf(byte[] inputStream, String imageType, ImageType colorType, boolean singleImage, int DPI, String filename) throws IOException, Exception { try (PDDocument document = PDDocument.load(new ByteArrayInputStream(inputStream))) { PDFRenderer pdfRenderer = new PDFRenderer(document); int pageCount = document.getNumberOfPages(); @@ -107,7 +107,7 @@ public class PdfUtils { ImageIO.write(image, imageType, baosImage); // Add the image to the zip file - zos.putNextEntry(new ZipEntry(String.format("page_%d.%s", i + 1, imageType.toLowerCase()))); + zos.putNextEntry(new ZipEntry(String.format(filename + "_%d.%s", i + 1, imageType.toLowerCase()))); zos.write(baosImage.toByteArray()); } } diff --git a/src/main/resources/templates/fragments/common.html b/src/main/resources/templates/fragments/common.html index 7d12a536..2bf0d875 100644 --- a/src/main/resources/templates/fragments/common.html +++ b/src/main/resources/templates/fragments/common.html @@ -400,16 +400,15 @@ document.addEventListener("DOMContentLoaded", function () { } let contentDisposition = response.headers.get('content-disposition'); - let fileName = "file.pdf" + let fileName = "file.pdf" if (!contentDisposition) { //throw new Error('Content-Disposition header not found for file ' + i); } else { - fileName = contentDisposition.split('filename=')[1].replace(/"/g, ''); + fileName = decodeURIComponent(contentDisposition.split('filename=')[1].replace(/"/g, '')); } console.log('Received response for file ' + i + ': ' + response); - let blob = await response.blob(); if (zipFiles) { // Add the file to the ZIP archive