From e7e3b34b37de10a60751421d5e3f572fa8d2eaed Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:37:40 +0100 Subject: [PATCH] fix for #1035 (#1137) * fix for #1035 * Update ConvertImgPDFController.java --- .../converters/ConvertImgPDFController.java | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 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 86a70472..eeb4db08 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 @@ -6,10 +6,6 @@ import java.net.URLConnection; import org.apache.pdfbox.rendering.ImageType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.core.io.ByteArrayResource; -import org.springframework.core.io.Resource; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ModelAttribute; @@ -39,7 +35,7 @@ public class ConvertImgPDFController { summary = "Convert PDF to image(s)", description = "This endpoint converts a PDF file to image(s) with the specified image format, color type, and DPI. Users can choose to get a single image or multiple images. Input:PDF Output:Image Type:SI-Conditional") - public ResponseEntity convertToImage(@ModelAttribute ConvertToImageRequest request) + public ResponseEntity convertToImage(@ModelAttribute ConvertToImageRequest request) throws IOException { MultipartFile file = request.getFileInput(); String imageFormat = request.getImageFormat(); @@ -76,22 +72,15 @@ public class ConvertImgPDFController { // TODO Auto-generated catch block e.printStackTrace(); } + if (singleImage) { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.parseMediaType(getMediaType(imageFormat))); - ResponseEntity response = - new ResponseEntity<>(new ByteArrayResource(result), headers, HttpStatus.OK); - return response; + String docName = filename + "." + imageFormat; + MediaType mediaType = MediaType.parseMediaType(getMediaType(imageFormat)); + return WebResponseUtils.bytesToWebResponse(result, docName, mediaType); } else { - ByteArrayResource resource = new ByteArrayResource(result); - // return the Resource in the response - return ResponseEntity.ok() - .header( - HttpHeaders.CONTENT_DISPOSITION, - "attachment; filename=" + filename + "_convertedToImages.zip") - .contentType(MediaType.APPLICATION_OCTET_STREAM) - .contentLength(resource.contentLength()) - .body(resource); + String zipFilename = filename + "_convertedToImages.zip"; + return WebResponseUtils.bytesToWebResponse( + result, zipFilename, MediaType.APPLICATION_OCTET_STREAM); } }