* fix for #1035

* Update ConvertImgPDFController.java
This commit is contained in:
Anthony Stirling 2024-04-28 22:37:40 +01:00 committed by GitHub
parent 6ed9e1c707
commit e7e3b34b37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,10 +6,6 @@ import java.net.URLConnection;
import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.ImageType;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
@ -39,7 +35,7 @@ public class ConvertImgPDFController {
summary = "Convert PDF to image(s)", summary = "Convert PDF to image(s)",
description = 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") "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<Resource> convertToImage(@ModelAttribute ConvertToImageRequest request) public ResponseEntity<byte[]> convertToImage(@ModelAttribute ConvertToImageRequest request)
throws IOException { throws IOException {
MultipartFile file = request.getFileInput(); MultipartFile file = request.getFileInput();
String imageFormat = request.getImageFormat(); String imageFormat = request.getImageFormat();
@ -76,22 +72,15 @@ public class ConvertImgPDFController {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
if (singleImage) { if (singleImage) {
HttpHeaders headers = new HttpHeaders(); String docName = filename + "." + imageFormat;
headers.setContentType(MediaType.parseMediaType(getMediaType(imageFormat))); MediaType mediaType = MediaType.parseMediaType(getMediaType(imageFormat));
ResponseEntity<Resource> response = return WebResponseUtils.bytesToWebResponse(result, docName, mediaType);
new ResponseEntity<>(new ByteArrayResource(result), headers, HttpStatus.OK);
return response;
} else { } else {
ByteArrayResource resource = new ByteArrayResource(result); String zipFilename = filename + "_convertedToImages.zip";
// return the Resource in the response return WebResponseUtils.bytesToWebResponse(
return ResponseEntity.ok() result, zipFilename, MediaType.APPLICATION_OCTET_STREAM);
.header(
HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=" + filename + "_convertedToImages.zip")
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.contentLength(resource.contentLength())
.body(resource);
} }
} }