1
0
mirror of https://github.com/Stirling-Tools/Stirling-PDF.git synced 2024-09-28 15:50:08 +02:00
This commit is contained in:
Anthony Stirling 2023-07-16 00:36:58 +01:00
parent 9af1b0cfdc
commit 29aabdfba8
5 changed files with 302 additions and 249 deletions

View File

@ -22,68 +22,79 @@ import stirling.software.SPDF.utils.PdfUtils;
import stirling.software.SPDF.utils.ProcessExecutor; import stirling.software.SPDF.utils.ProcessExecutor;
import stirling.software.SPDF.utils.WebResponseUtils; import stirling.software.SPDF.utils.WebResponseUtils;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
@RestController @RestController
@Tag(name = "Filter", description = "Filter APIs") @Tag(name = "Filter", description = "Filter APIs")
public class FilterController { public class FilterController {
@PostMapping(consumes = "multipart/form-data", value = "/contains-text") @PostMapping(consumes = "multipart/form-data", value = "/filter-contains-text")
@Operation(summary = "Checks if a PDF contains set text, returns true if does", description = "Input:PDF Output:Boolean Type:SISO") @Operation(summary = "Checks if a PDF contains set text, returns true if does", description = "Input:PDF Output:Boolean Type:SISO")
public Boolean containsText( public ResponseEntity<byte[]> containsText(
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file to be converted to a PDF/A file", required = true) MultipartFile inputFile, @RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file to be converted to a PDF/A file", required = true) MultipartFile inputFile,
@Parameter(description = "The text to check for", required = true) String text, @Parameter(description = "The text to check for", required = true) String text,
@Parameter(description = "The page number to check for text on accepts 'All', ranges like '1-4'", required = false) String pageNumber) @Parameter(description = "The page number to check for text on accepts 'All', ranges like '1-4'", required = false) String pageNumber)
throws IOException, InterruptedException { throws IOException, InterruptedException {
PDDocument pdfDocument = PDDocument.load(inputFile.getInputStream()); PDDocument pdfDocument = PDDocument.load(inputFile.getInputStream());
return PdfUtils.hasText(pdfDocument, pageNumber); if (PdfUtils.hasText(pdfDocument, pageNumber, text))
return WebResponseUtils.pdfDocToWebResponse(pdfDocument, inputFile.getOriginalFilename());
return null;
} }
//TODO // TODO
@PostMapping(consumes = "multipart/form-data", value = "/contains-image") @PostMapping(consumes = "multipart/form-data", value = "/filter-contains-image")
@Operation(summary = "Checks if a PDF contains an image", description = "Input:PDF Output:Boolean Type:SISO") @Operation(summary = "Checks if a PDF contains an image", description = "Input:PDF Output:Boolean Type:SISO")
public Boolean containsImage( public ResponseEntity<byte[]> containsImage(
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file to be converted to a PDF/A file", required = true) MultipartFile inputFile, @RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file to be converted to a PDF/A file", required = true) MultipartFile inputFile,
@Parameter(description = "The page number to check for image on accepts 'All', ranges like '1-4'", required = false) String pageNumber) @Parameter(description = "The page number to check for image on accepts 'All', ranges like '1-4'", required = false) String pageNumber)
throws IOException, InterruptedException { throws IOException, InterruptedException {
PDDocument pdfDocument = PDDocument.load(inputFile.getInputStream()); PDDocument pdfDocument = PDDocument.load(inputFile.getInputStream());
return PdfUtils.hasImagesOnPage(null); if (PdfUtils.hasImages(pdfDocument, pageNumber))
return WebResponseUtils.pdfDocToWebResponse(pdfDocument, inputFile.getOriginalFilename());
return null;
} }
@PostMapping(consumes = "multipart/form-data", value = "/page-count") @PostMapping(consumes = "multipart/form-data", value = "/filter-page-count")
@Operation(summary = "Checks if a PDF is greater, less or equal to a setPageCount", description = "Input:PDF Output:Boolean Type:SISO") @Operation(summary = "Checks if a PDF is greater, less or equal to a setPageCount", description = "Input:PDF Output:Boolean Type:SISO")
public Boolean pageCount( public ResponseEntity<byte[]> pageCount(
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file", required = true) MultipartFile inputFile, @RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file", required = true) MultipartFile inputFile,
@Parameter(description = "Page Count", required = true) String pageCount, @Parameter(description = "Page Count", required = true) String pageCount,
@Parameter(description = "Comparison type", @Parameter(description = "Comparison type", schema = @Schema(description = "The comparison type, accepts Greater, Equal, Less than", allowableValues = {
schema = @Schema(description = "The comparison type, accepts Greater, Equal, Less than", "Greater", "Equal", "Less" })) String comparator)
allowableValues = {"Greater", "Equal", "Less"})) String comparator)
throws IOException, InterruptedException { throws IOException, InterruptedException {
// Load the PDF // Load the PDF
PDDocument document = PDDocument.load(inputFile.getInputStream()); PDDocument document = PDDocument.load(inputFile.getInputStream());
int actualPageCount = document.getNumberOfPages(); int actualPageCount = document.getNumberOfPages();
boolean valid = false;
// Perform the comparison // Perform the comparison
switch (comparator) { switch (comparator) {
case "Greater": case "Greater":
return actualPageCount > Integer.parseInt(pageCount); valid = actualPageCount > Integer.parseInt(pageCount);
break;
case "Equal": case "Equal":
return actualPageCount == Integer.parseInt(pageCount); valid = actualPageCount == Integer.parseInt(pageCount);
break;
case "Less": case "Less":
return actualPageCount < Integer.parseInt(pageCount); valid = actualPageCount < Integer.parseInt(pageCount);
break;
default: default:
throw new IllegalArgumentException("Invalid comparator: " + comparator); throw new IllegalArgumentException("Invalid comparator: " + comparator);
} }
if (valid)
return WebResponseUtils.multiPartFileToWebResponse(inputFile);
return null;
} }
@PostMapping(consumes = "multipart/form-data", value = "/page-size") @PostMapping(consumes = "multipart/form-data", value = "/filter-page-size")
@Operation(summary = "Checks if a PDF is of a certain size", description = "Input:PDF Output:Boolean Type:SISO") @Operation(summary = "Checks if a PDF is of a certain size", description = "Input:PDF Output:Boolean Type:SISO")
public Boolean pageSize( public ResponseEntity<byte[]> pageSize(
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file", required = true) MultipartFile inputFile, @RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file", required = true) MultipartFile inputFile,
@Parameter(description = "Standard Page Size", required = true) String standardPageSize, @Parameter(description = "Standard Page Size", required = true) String standardPageSize,
@Parameter(description = "Comparison type", @Parameter(description = "Comparison type", schema = @Schema(description = "The comparison type, accepts Greater, Equal, Less than", allowableValues = {
schema = @Schema(description = "The comparison type, accepts Greater, Equal, Less than", "Greater", "Equal", "Less" })) String comparator)
allowableValues = {"Greater", "Equal", "Less"})) String comparator) throws IOException, InterruptedException {
throws IOException, InterruptedException {
// Load the PDF // Load the PDF
PDDocument document = PDDocument.load(inputFile.getInputStream()); PDDocument document = PDDocument.load(inputFile.getInputStream());
@ -97,75 +108,95 @@ public class FilterController {
PDRectangle standardSize = PdfUtils.textToPageSize(standardPageSize); PDRectangle standardSize = PdfUtils.textToPageSize(standardPageSize);
float standardArea = standardSize.getWidth() * standardSize.getHeight(); float standardArea = standardSize.getWidth() * standardSize.getHeight();
boolean valid = false;
// Perform the comparison // Perform the comparison
switch (comparator) { switch (comparator) {
case "Greater": case "Greater":
return actualArea > standardArea; valid = actualArea > standardArea;
break;
case "Equal": case "Equal":
return actualArea == standardArea; valid = actualArea == standardArea;
break;
case "Less": case "Less":
return actualArea < standardArea; valid = actualArea < standardArea;
break;
default: default:
throw new IllegalArgumentException("Invalid comparator: " + comparator); throw new IllegalArgumentException("Invalid comparator: " + comparator);
} }
}
@PostMapping(consumes = "multipart/form-data", value = "/file-size") if (valid)
return WebResponseUtils.multiPartFileToWebResponse(inputFile);
return null;
}
@PostMapping(consumes = "multipart/form-data", value = "/filter-file-size")
@Operation(summary = "Checks if a PDF is a set file size", description = "Input:PDF Output:Boolean Type:SISO") @Operation(summary = "Checks if a PDF is a set file size", description = "Input:PDF Output:Boolean Type:SISO")
public Boolean fileSize( public ResponseEntity<byte[]> fileSize(
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file", required = true) MultipartFile inputFile, @RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file", required = true) MultipartFile inputFile,
@Parameter(description = "File Size", required = true) String fileSize, @Parameter(description = "File Size", required = true) String fileSize,
@Parameter(description = "Comparison type", @Parameter(description = "Comparison type", schema = @Schema(description = "The comparison type, accepts Greater, Equal, Less than", allowableValues = {
schema = @Schema(description = "The comparison type, accepts Greater, Equal, Less than", "Greater", "Equal", "Less" })) String comparator)
allowableValues = {"Greater", "Equal", "Less"})) String comparator) throws IOException, InterruptedException {
throws IOException, InterruptedException {
// Get the file size // Get the file size
long actualFileSize = inputFile.getSize(); long actualFileSize = inputFile.getSize();
boolean valid = false;
// Perform the comparison // Perform the comparison
switch (comparator) { switch (comparator) {
case "Greater": case "Greater":
return actualFileSize > Long.parseLong(fileSize); valid = actualFileSize > Long.parseLong(fileSize);
break;
case "Equal": case "Equal":
return actualFileSize == Long.parseLong(fileSize); valid = actualFileSize == Long.parseLong(fileSize);
break;
case "Less": case "Less":
return actualFileSize < Long.parseLong(fileSize); valid = actualFileSize < Long.parseLong(fileSize);
break;
default: default:
throw new IllegalArgumentException("Invalid comparator: " + comparator); throw new IllegalArgumentException("Invalid comparator: " + comparator);
} }
if (valid)
return WebResponseUtils.multiPartFileToWebResponse(inputFile);
return null;
} }
@PostMapping(consumes = "multipart/form-data", value = "/filter-page-rotation")
@PostMapping(consumes = "multipart/form-data", value = "/page-rotation")
@Operation(summary = "Checks if a PDF is of a certain rotation", description = "Input:PDF Output:Boolean Type:SISO") @Operation(summary = "Checks if a PDF is of a certain rotation", description = "Input:PDF Output:Boolean Type:SISO")
public Boolean pageRotation( public ResponseEntity<byte[]> pageRotation(
@RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file", required = true) MultipartFile inputFile, @RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file", required = true) MultipartFile inputFile,
@Parameter(description = "Rotation in degrees", required = true) int rotation, @Parameter(description = "Rotation in degrees", required = true) int rotation,
@Parameter(description = "Comparison type", @Parameter(description = "Comparison type", schema = @Schema(description = "The comparison type, accepts Greater, Equal, Less than", allowableValues = {
schema = @Schema(description = "The comparison type, accepts Greater, Equal, Less than", "Greater", "Equal", "Less" })) String comparator)
allowableValues = {"Greater", "Equal", "Less"})) String comparator) throws IOException, InterruptedException {
throws IOException, InterruptedException {
// Load the PDF // Load the PDF
PDDocument document = PDDocument.load(inputFile.getInputStream()); PDDocument document = PDDocument.load(inputFile.getInputStream());
// Get the rotation of the first page // Get the rotation of the first page
PDPage firstPage = document.getPage(0); PDPage firstPage = document.getPage(0);
int actualRotation = firstPage.getRotation(); int actualRotation = firstPage.getRotation();
boolean valid = false;
// Perform the comparison // Perform the comparison
switch (comparator) { switch (comparator) {
case "Greater": case "Greater":
return actualRotation > rotation; valid = actualRotation > rotation;
break;
case "Equal": case "Equal":
return actualRotation == rotation; valid = actualRotation == rotation;
break;
case "Less": case "Less":
return actualRotation < rotation; valid = actualRotation < rotation;
break;
default: default:
throw new IllegalArgumentException("Invalid comparator: " + comparator); throw new IllegalArgumentException("Invalid comparator: " + comparator);
} }
if (valid)
return WebResponseUtils.multiPartFileToWebResponse(inputFile);
return null;
} }
} }

View File

@ -292,12 +292,18 @@ public class PipelineController {
ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.POST, entity, byte[].class); ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.POST, entity, byte[].class);
// If the operation is filter and the response body is null or empty, skip this file
if (operation.startsWith("filter-") && (response.getBody() == null || response.getBody().length == 0)) {
logger.info("Skipping file due to failing {}", operation);
continue;
}
if (!response.getStatusCode().equals(HttpStatus.OK)) { if (!response.getStatusCode().equals(HttpStatus.OK)) {
logPrintStream.println("Error: " + response.getBody()); logPrintStream.println("Error: " + response.getBody());
hasErrors = true; hasErrors = true;
continue; continue;
} }
// Define filename // Define filename
String filename; String filename;

View File

@ -1,107 +1,113 @@
package stirling.software.SPDF.utils; package stirling.software.SPDF.utils;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class GeneralUtils { public class GeneralUtils {
public static Long convertSizeToBytes(String sizeStr) { public static Long convertSizeToBytes(String sizeStr) {
if (sizeStr == null) { if (sizeStr == null) {
return null; return null;
} }
sizeStr = sizeStr.trim().toUpperCase(); sizeStr = sizeStr.trim().toUpperCase();
try { try {
if (sizeStr.endsWith("KB")) { if (sizeStr.endsWith("KB")) {
return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2)) * 1024); return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2)) * 1024);
} else if (sizeStr.endsWith("MB")) { } else if (sizeStr.endsWith("MB")) {
return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2)) * 1024 * 1024); return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2)) * 1024 * 1024);
} else if (sizeStr.endsWith("GB")) { } else if (sizeStr.endsWith("GB")) {
return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2)) * 1024 * 1024 * 1024); return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2)) * 1024 * 1024 * 1024);
} else if (sizeStr.endsWith("B")) { } else if (sizeStr.endsWith("B")) {
return Long.parseLong(sizeStr.substring(0, sizeStr.length() - 1)); return Long.parseLong(sizeStr.substring(0, sizeStr.length() - 1));
} else { } else {
// Input string does not have a valid format, handle this case // Input string does not have a valid format, handle this case
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// The numeric part of the input string cannot be parsed, handle this case // The numeric part of the input string cannot be parsed, handle this case
} }
return null; return null;
} }
public static List<Integer> parsePageList(String[] pageOrderArr, int totalPages) { public static List<Integer> parsePageList(String[] pageOrderArr, int totalPages) {
List<Integer> newPageOrder = new ArrayList<>(); List<Integer> newPageOrder = new ArrayList<>();
// loop through the page order array // loop through the page order array
for (String element : pageOrderArr) { for (String element : pageOrderArr) {
// check if the element contains a range of pages if (element.equalsIgnoreCase("all")) {
if (element.matches("\\d*n\\+?-?\\d*|\\d*\\+?n")) { for (int i = 0; i < totalPages; i++) {
// Handle page order as a function newPageOrder.add(i);
int coefficient = 0; }
int constant = 0; // As all pages are already added, no need to check further
boolean coefficientExists = false; break;
boolean constantExists = false; }
else if (element.matches("\\d*n\\+?-?\\d*|\\d*\\+?n")) {
if (element.contains("n")) { // Handle page order as a function
String[] parts = element.split("n"); int coefficient = 0;
if (!parts[0].equals("") && parts[0] != null) { int constant = 0;
coefficient = Integer.parseInt(parts[0]); boolean coefficientExists = false;
coefficientExists = true; boolean constantExists = false;
}
if (parts.length > 1 && !parts[1].equals("") && parts[1] != null) { if (element.contains("n")) {
constant = Integer.parseInt(parts[1]); String[] parts = element.split("n");
constantExists = true; if (!parts[0].equals("") && parts[0] != null) {
} coefficient = Integer.parseInt(parts[0]);
} else if (element.contains("+")) { coefficientExists = true;
constant = Integer.parseInt(element.replace("+", "")); }
constantExists = true; if (parts.length > 1 && !parts[1].equals("") && parts[1] != null) {
} constant = Integer.parseInt(parts[1]);
constantExists = true;
for (int i = 1; i <= totalPages; i++) { }
int pageNum = coefficientExists ? coefficient * i : i; } else if (element.contains("+")) {
pageNum += constantExists ? constant : 0; constant = Integer.parseInt(element.replace("+", ""));
constantExists = true;
if (pageNum <= totalPages && pageNum > 0) { }
newPageOrder.add(pageNum - 1);
} for (int i = 1; i <= totalPages; i++) {
} int pageNum = coefficientExists ? coefficient * i : i;
} else if (element.contains("-")) { pageNum += constantExists ? constant : 0;
// split the range into start and end page
String[] range = element.split("-"); if (pageNum <= totalPages && pageNum > 0) {
int start = Integer.parseInt(range[0]); newPageOrder.add(pageNum - 1);
int end = Integer.parseInt(range[1]); }
// check if the end page is greater than total pages }
if (end > totalPages) { } else if (element.contains("-")) {
end = totalPages; // split the range into start and end page
} String[] range = element.split("-");
// loop through the range of pages int start = Integer.parseInt(range[0]);
for (int j = start; j <= end; j++) { int end = Integer.parseInt(range[1]);
// print the current index // check if the end page is greater than total pages
newPageOrder.add(j - 1); if (end > totalPages) {
} end = totalPages;
} else { }
// if the element is a single page // loop through the range of pages
newPageOrder.add(Integer.parseInt(element) - 1); for (int j = start; j <= end; j++) {
} // print the current index
} newPageOrder.add(j - 1);
}
return newPageOrder; } else {
} // if the element is a single page
public static boolean createDir(String path) { newPageOrder.add(Integer.parseInt(element) - 1);
Path folder = Paths.get(path); }
if (!Files.exists(folder)) { }
try {
Files.createDirectories(folder); return newPageOrder;
} catch (IOException e) { }
e.printStackTrace(); public static boolean createDir(String path) {
return false; Path folder = Paths.get(path);
} if (!Files.exists(folder)) {
} try {
return true; Files.createDirectories(folder);
} } catch (IOException e) {
} e.printStackTrace();
return false;
}
}
return true;
}
}

View File

@ -68,43 +68,37 @@ public class PdfUtils {
} }
} }
public boolean hasImageInFile(PDDocument pdfDocument, String text, String pagesToCheck) throws IOException {
PDFTextStripper textStripper = new PDFTextStripper();
String pdfText = "";
if(pagesToCheck == null || pagesToCheck.equals("all")) {
pdfText = textStripper.getText(pdfDocument);
} else { public static boolean hasImages(PDDocument document, String pagesToCheck) throws IOException {
// remove whitespaces String[] pageOrderArr = pagesToCheck.split(",");
pagesToCheck = pagesToCheck.replaceAll("\\s+", ""); List<Integer> pageList = GeneralUtils.parsePageList(pageOrderArr, document.getNumberOfPages());
String[] splitPoints = pagesToCheck.split(","); for (int pageNumber : pageList) {
for (String splitPoint : splitPoints) { PDPage page = document.getPage(pageNumber);
if (splitPoint.contains("-")) { if (hasImagesOnPage(page)) {
// Handle page ranges return true;
String[] range = splitPoint.split("-");
int startPage = Integer.parseInt(range[0]);
int endPage = Integer.parseInt(range[1]);
for (int i = startPage; i <= endPage; i++) {
textStripper.setStartPage(i);
textStripper.setEndPage(i);
pdfText += textStripper.getText(pdfDocument);
}
} else {
// Handle individual page
int page = Integer.parseInt(splitPoint);
textStripper.setStartPage(page);
textStripper.setEndPage(page);
pdfText += textStripper.getText(pdfDocument);
}
} }
} }
pdfDocument.close(); return false;
return pdfText.contains(text);
} }
public static boolean hasText(PDDocument document, String pageNumbersToCheck, String phrase) throws IOException {
String[] pageOrderArr = pageNumbersToCheck.split(",");
List<Integer> pageList = GeneralUtils.parsePageList(pageOrderArr, document.getNumberOfPages());
for (int pageNumber : pageList) {
PDPage page = document.getPage(pageNumber);
if (hasTextOnPage(page, phrase)) {
return true;
}
}
return false;
}
public static boolean hasImagesOnPage(PDPage page) throws IOException { public static boolean hasImagesOnPage(PDPage page) throws IOException {
ImageFinder imageFinder = new ImageFinder(page); ImageFinder imageFinder = new ImageFinder(page);
@ -113,12 +107,17 @@ public class PdfUtils {
} }
public static boolean hasText(PDDocument document, String phrase) throws IOException {
PDFTextStripper pdfStripper = new PDFTextStripper();
String text = pdfStripper.getText(document);
return text.contains(phrase);
}
public static boolean hasTextOnPage(PDPage page, String phrase) throws IOException {
PDFTextStripper textStripper = new PDFTextStripper();
PDDocument tempDoc = new PDDocument();
tempDoc.addPage(page);
String pageText = textStripper.getText(tempDoc);
tempDoc.close();
return pageText.contains(phrase);
}
public boolean containsTextInFile(PDDocument pdfDocument, String text, String pagesToCheck) throws IOException { public boolean containsTextInFile(PDDocument pdfDocument, String text, String pagesToCheck) throws IOException {
PDFTextStripper textStripper = new PDFTextStripper(); PDFTextStripper textStripper = new PDFTextStripper();

View File

@ -1,50 +1,61 @@
package stirling.software.SPDF.utils; package stirling.software.SPDF.utils;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; 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.multipart.MultipartFile;
public class WebResponseUtils {
public class WebResponseUtils {
public static ResponseEntity<byte[]> boasToWebResponse(ByteArrayOutputStream baos, String docName) throws IOException {
return WebResponseUtils.bytesToWebResponse(baos.toByteArray(), docName); public static ResponseEntity<byte[]> boasToWebResponse(ByteArrayOutputStream baos, String docName) throws IOException {
} return WebResponseUtils.bytesToWebResponse(baos.toByteArray(), docName);
}
public static ResponseEntity<byte[]> boasToWebResponse(ByteArrayOutputStream baos, String docName, MediaType mediaType) throws IOException {
return WebResponseUtils.bytesToWebResponse(baos.toByteArray(), docName, mediaType); public static ResponseEntity<byte[]> boasToWebResponse(ByteArrayOutputStream baos, String docName, MediaType mediaType) throws IOException {
} return WebResponseUtils.bytesToWebResponse(baos.toByteArray(), docName, mediaType);
}
public static ResponseEntity<byte[]> bytesToWebResponse(byte[] bytes, String docName, MediaType mediaType) throws IOException {
// Return the PDF as a response public static ResponseEntity<byte[]> multiPartFileToWebResponse(MultipartFile file) throws IOException {
HttpHeaders headers = new HttpHeaders(); String fileName = file.getOriginalFilename();
headers.setContentType(mediaType); MediaType mediaType = MediaType.parseMediaType(file.getContentType());
headers.setContentLength(bytes.length);
String encodedDocName = URLEncoder.encode(docName, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20"); byte[] bytes = file.getBytes();
headers.setContentDispositionFormData("attachment", encodedDocName);
return new ResponseEntity<>(bytes, headers, HttpStatus.OK); return bytesToWebResponse(bytes, fileName, mediaType);
} }
public static ResponseEntity<byte[]> bytesToWebResponse(byte[] bytes, String docName) throws IOException { public static ResponseEntity<byte[]> bytesToWebResponse(byte[] bytes, String docName, MediaType mediaType) throws IOException {
return bytesToWebResponse(bytes, docName, MediaType.APPLICATION_PDF);
} // Return the PDF as a response
HttpHeaders headers = new HttpHeaders();
public static ResponseEntity<byte[]> pdfDocToWebResponse(PDDocument document, String docName) throws IOException { headers.setContentType(mediaType);
headers.setContentLength(bytes.length);
// Open Byte Array and save document to it String encodedDocName = URLEncoder.encode(docName, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20");
ByteArrayOutputStream baos = new ByteArrayOutputStream(); headers.setContentDispositionFormData("attachment", encodedDocName);
document.save(baos); return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
// Close the document }
document.close();
public static ResponseEntity<byte[]> bytesToWebResponse(byte[] bytes, String docName) throws IOException {
return boasToWebResponse(baos, docName); return bytesToWebResponse(bytes, docName, MediaType.APPLICATION_PDF);
} }
} public static ResponseEntity<byte[]> pdfDocToWebResponse(PDDocument document, String docName) throws IOException {
// Open Byte Array and save document to it
ByteArrayOutputStream baos = new ByteArrayOutputStream();
document.save(baos);
// Close the document
document.close();
return boasToWebResponse(baos, docName);
}
}