Files
Stirling-PDF/src/main/java/stirling/software/SPDF/controller/FromPDFController.java
T

49 lines
1.8 KiB
Java
Raw Normal View History

2023-01-27 18:23:40 +00:00
package stirling.software.SPDF.controller;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2023-01-28 10:00:32 +00:00
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
2023-01-27 18:23:40 +00:00
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import stirling.software.SPDF.utils.PdfUtils;
@Controller
public class FromPDFController {
private static final Logger logger = LoggerFactory.getLogger(FromPDFController.class);
@PostMapping("/convert-from-pdf")
2023-01-28 10:00:32 +00:00
public ResponseEntity<byte[]> convertToImage(@RequestParam("fileInput") MultipartFile file,
2023-01-27 18:23:40 +00:00
@RequestParam("imageFormat") String imageFormat) throws IOException {
byte[] pdfBytes = file.getBytes();
2023-01-28 10:00:32 +00:00
//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<byte[]> response = new ResponseEntity<>(result, headers, HttpStatus.OK);
return response;
2023-01-27 18:23:40 +00:00
}
2023-01-28 10:00:32 +00:00
private String getMediaType(String imageFormat) {
if(imageFormat.equalsIgnoreCase("PNG"))
return "image/png";
2023-01-29 17:06:53 +00:00
else if(imageFormat.equalsIgnoreCase("JPEG") || imageFormat.equalsIgnoreCase("JPG"))
2023-01-28 10:00:32 +00:00
return "image/jpeg";
else if(imageFormat.equalsIgnoreCase("GIF"))
return "image/gif";
else
return "application/octet-stream";
}
2023-01-27 18:23:40 +00:00
}