mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2024-12-21 19:08:24 +01:00
commit
81f031a2ca
@ -1,48 +0,0 @@
|
||||
package stirling.software.SPDF.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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")
|
||||
public ResponseEntity<byte[]> 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<byte[]> 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";
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
package stirling.software.SPDF.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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 ToPDFController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ToPDFController.class);
|
||||
|
||||
@GetMapping("/convert-to-pdf")
|
||||
public String convertToPdfForm() {
|
||||
return "convert-to-pdf";
|
||||
}
|
||||
|
||||
@PostMapping("/convert-to-pdf")
|
||||
public ResponseEntity<byte[]> convertToPdf(@RequestParam("fileInput") MultipartFile file) throws IOException {
|
||||
// Convert the file to PDF and get the resulting bytes
|
||||
byte[] bytes = PdfUtils.convertToPdf(file.getInputStream());
|
||||
logger.info("File {} successfully converted to pdf", file.getOriginalFilename());
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_PDF);
|
||||
String filename = "converted.pdf";
|
||||
headers.setContentDispositionFormData(filename, filename);
|
||||
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
|
||||
ResponseEntity<byte[]> response = new ResponseEntity<>(bytes, headers, HttpStatus.OK);
|
||||
return response;
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<th:block th:insert="~{common :: head}"></th:block>
|
||||
<title>S-PDF ConvertFromPDF</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div th:insert="~{navbar.html :: navbar}"></div>
|
||||
<br>
|
||||
<br>
|
||||
<div class="container">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-6">
|
||||
<h2>PDF to img</h2>
|
||||
<form method="post" enctype="multipart/form-data"
|
||||
th:action="@{/convert-from-pdf}">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="fileInput"
|
||||
name="fileInput" required> <label
|
||||
class="custom-file-label" for="fileInput">Choose PDF</label>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Image Format</label> <select class="form-control"
|
||||
name="imageFormat">
|
||||
<option value="png">PNG</option>
|
||||
<option value="jpg">JPEG</option>
|
||||
<option value="gif">GIF</option>
|
||||
</select>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Convert</button>
|
||||
</form>
|
||||
<th:block th:insert="~{common :: filelist}"></th:block>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div th:insert="~{footer.html :: footer}"></div>
|
||||
</body>
|
||||
</html>
|
@ -1,33 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<th:block th:insert="~{common :: head}"></th:block>
|
||||
<title>S-PDF ConvertToPDF</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div th:insert="~{navbar.html :: navbar}"></div>
|
||||
<br>
|
||||
<br>
|
||||
<div class="container">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-6">
|
||||
<h2>Image to PDF</h2>
|
||||
|
||||
<form method="post" enctype="multipart/form-data"
|
||||
th:action="@{/convert-to-pdf}">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="fileInput"
|
||||
name="fileInput" required> <label
|
||||
class="custom-file-label" for="fileInput">Choose Image</label>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Convert</button>
|
||||
|
||||
</form>
|
||||
<th:block th:insert="~{common :: filelist}"></th:block>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div th:insert="~{footer.html :: footer}"></div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user