From 1a0dd6fefefc13d6d45bb836a8458d6f95badbbe Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Tue, 31 Jan 2023 19:59:38 +0000 Subject: [PATCH] Update ConvertPDFController.java --- .../SPDF/controller/ConvertPDFController.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/stirling/software/SPDF/controller/ConvertPDFController.java b/src/main/java/stirling/software/SPDF/controller/ConvertPDFController.java index 9ac68249..c37c5e8a 100644 --- a/src/main/java/stirling/software/SPDF/controller/ConvertPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/ConvertPDFController.java @@ -42,4 +42,29 @@ public class ConvertPDFController { ResponseEntity response = new ResponseEntity<>(bytes, headers, HttpStatus.OK); return response; } + + @PostMapping("/convert-from-pdf") + public ResponseEntity convertToImage(@RequestParam("fileInput") MultipartFile file, + @RequestParam("imageFormat") String imageFormat) throws IOException { + byte[] pdfBytes = file.getBytes(); + //returns bytes for image + byte[] result = PdfUtils.convertFromPdf(pdfBytes, imageFormat.toLowerCase()); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.parseMediaType(getMediaType(imageFormat))); + headers.setCacheControl("must-revalidate, post-check=0, pre-check=0"); + ResponseEntity response = new ResponseEntity<>(result, headers, HttpStatus.OK); + return response; + } + + private String getMediaType(String imageFormat) { + if(imageFormat.equalsIgnoreCase("PNG")) + return "image/png"; + else if(imageFormat.equalsIgnoreCase("JPEG") || imageFormat.equalsIgnoreCase("JPG")) + return "image/jpeg"; + else if(imageFormat.equalsIgnoreCase("GIF")) + return "image/gif"; + else + return "application/octet-stream"; + } + }