Number of fixes and making pipline LIVE ! (#907)
Closes #889 and #332 #710 #901 #885
This commit is contained in:
parent
ac620082ec
commit
ae73595335
19 changed files with 359 additions and 220 deletions
|
@ -12,8 +12,7 @@ plugins {
|
||||||
import com.github.jk1.license.render.*
|
import com.github.jk1.license.render.*
|
||||||
|
|
||||||
group = 'stirling.software'
|
group = 'stirling.software'
|
||||||
|
version = '0.22.3'
|
||||||
version = '0.22.2'
|
|
||||||
sourceCompatibility = '17'
|
sourceCompatibility = '17'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
apiVersion: v2
|
apiVersion: v2
|
||||||
appVersion: 0.20.2
|
appVersion: 0.22.3
|
||||||
description: locally hosted web application that allows you to perform various operations on PDF files
|
description: locally hosted web application that allows you to perform various operations on PDF files
|
||||||
home: https://github.com/Stirling-Tools/Stirling-PDF
|
home: https://github.com/Stirling-Tools/Stirling-PDF
|
||||||
keywords:
|
keywords:
|
||||||
|
|
|
@ -130,7 +130,7 @@ public class AutoRenameController {
|
||||||
|
|
||||||
// Sanitize the header string by removing characters not allowed in a filename.
|
// Sanitize the header string by removing characters not allowed in a filename.
|
||||||
if (header != null && header.length() < 255) {
|
if (header != null && header.length() < 255) {
|
||||||
header = header.replaceAll("[/\\\\?%*:|\"<>]", "");
|
header = header.replaceAll("[/\\\\?%*:|\"<>]", "").trim();
|
||||||
return WebResponseUtils.pdfDocToWebResponse(document, header + ".pdf");
|
return WebResponseUtils.pdfDocToWebResponse(document, header + ".pdf");
|
||||||
} else {
|
} else {
|
||||||
logger.info("File has no good title to be found");
|
logger.info("File has no good title to be found");
|
||||||
|
|
|
@ -58,7 +58,7 @@ public class AutoSplitPdfController {
|
||||||
|
|
||||||
PDDocument document = Loader.loadPDF(file.getBytes());
|
PDDocument document = Loader.loadPDF(file.getBytes());
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
|
pdfRenderer.setSubsamplingAllowed(true);
|
||||||
List<PDDocument> splitDocuments = new ArrayList<>();
|
List<PDDocument> splitDocuments = new ArrayList<>();
|
||||||
List<ByteArrayOutputStream> splitDocumentsBoas = new ArrayList<>();
|
List<ByteArrayOutputStream> splitDocumentsBoas = new ArrayList<>();
|
||||||
|
|
||||||
|
|
|
@ -59,7 +59,7 @@ public class BlankPageController {
|
||||||
List<Integer> pagesToKeepIndex = new ArrayList<>();
|
List<Integer> pagesToKeepIndex = new ArrayList<>();
|
||||||
int pageIndex = 0;
|
int pageIndex = 0;
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
|
pdfRenderer.setSubsamplingAllowed(true);
|
||||||
for (PDPage page : pages) {
|
for (PDPage page : pages) {
|
||||||
logger.info("checking page " + pageIndex);
|
logger.info("checking page " + pageIndex);
|
||||||
textStripper.setStartPage(pageIndex + 1);
|
textStripper.setStartPage(pageIndex + 1);
|
||||||
|
|
|
@ -2,9 +2,7 @@ package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -75,194 +73,208 @@ public class CompressController {
|
||||||
long inputFileSize = Files.size(tempInputFile);
|
long inputFileSize = Files.size(tempInputFile);
|
||||||
|
|
||||||
// Prepare the output file path
|
// Prepare the output file path
|
||||||
Path tempOutputFile = Files.createTempFile("output_", ".pdf");
|
|
||||||
|
|
||||||
// Determine initial optimization level based on expected size reduction, only if in
|
|
||||||
// autoMode
|
|
||||||
if (autoMode) {
|
|
||||||
double sizeReductionRatio = expectedOutputSize / (double) inputFileSize;
|
|
||||||
if (sizeReductionRatio > 0.7) {
|
|
||||||
optimizeLevel = 1;
|
|
||||||
} else if (sizeReductionRatio > 0.5) {
|
|
||||||
optimizeLevel = 2;
|
|
||||||
} else if (sizeReductionRatio > 0.35) {
|
|
||||||
optimizeLevel = 3;
|
|
||||||
} else {
|
|
||||||
optimizeLevel = 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean sizeMet = false;
|
|
||||||
while (!sizeMet && optimizeLevel <= 4) {
|
|
||||||
// Prepare the Ghostscript command
|
|
||||||
List<String> command = new ArrayList<>();
|
|
||||||
command.add("gs");
|
|
||||||
command.add("-sDEVICE=pdfwrite");
|
|
||||||
command.add("-dCompatibilityLevel=1.4");
|
|
||||||
|
|
||||||
switch (optimizeLevel) {
|
|
||||||
case 1:
|
|
||||||
command.add("-dPDFSETTINGS=/prepress");
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
command.add("-dPDFSETTINGS=/printer");
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
command.add("-dPDFSETTINGS=/ebook");
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
command.add("-dPDFSETTINGS=/screen");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
command.add("-dPDFSETTINGS=/default");
|
|
||||||
}
|
|
||||||
|
|
||||||
command.add("-dNOPAUSE");
|
|
||||||
command.add("-dQUIET");
|
|
||||||
command.add("-dBATCH");
|
|
||||||
command.add("-sOutputFile=" + tempOutputFile.toString());
|
|
||||||
command.add(tempInputFile.toString());
|
|
||||||
|
|
||||||
ProcessExecutorResult returnCode =
|
|
||||||
ProcessExecutor.getInstance(ProcessExecutor.Processes.GHOSTSCRIPT)
|
|
||||||
.runCommandWithOutputHandling(command);
|
|
||||||
|
|
||||||
// Check if file size is within expected size or not auto mode so instantly finish
|
|
||||||
long outputFileSize = Files.size(tempOutputFile);
|
|
||||||
if (outputFileSize <= expectedOutputSize || !autoMode) {
|
|
||||||
sizeMet = true;
|
|
||||||
} else {
|
|
||||||
// Increase optimization level for next iteration
|
|
||||||
optimizeLevel++;
|
|
||||||
if (autoMode && optimizeLevel > 3) {
|
|
||||||
System.out.println("Skipping level 4 due to bad results in auto mode");
|
|
||||||
sizeMet = true;
|
|
||||||
} else if (optimizeLevel == 5) {
|
|
||||||
|
|
||||||
|
Path tempOutputFile = null;
|
||||||
|
byte[] pdfBytes;
|
||||||
|
try {
|
||||||
|
tempOutputFile = Files.createTempFile("output_", ".pdf");
|
||||||
|
// Determine initial optimization level based on expected size reduction, only if in
|
||||||
|
// autoMode
|
||||||
|
if (autoMode) {
|
||||||
|
double sizeReductionRatio = expectedOutputSize / (double) inputFileSize;
|
||||||
|
if (sizeReductionRatio > 0.7) {
|
||||||
|
optimizeLevel = 1;
|
||||||
|
} else if (sizeReductionRatio > 0.5) {
|
||||||
|
optimizeLevel = 2;
|
||||||
|
} else if (sizeReductionRatio > 0.35) {
|
||||||
|
optimizeLevel = 3;
|
||||||
} else {
|
} else {
|
||||||
System.out.println(
|
optimizeLevel = 3;
|
||||||
"Increasing ghostscript optimisation level to " + optimizeLevel);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (expectedOutputSize != null && autoMode) {
|
boolean sizeMet = false;
|
||||||
long outputFileSize = Files.size(tempOutputFile);
|
while (!sizeMet && optimizeLevel <= 4) {
|
||||||
if (outputFileSize > expectedOutputSize) {
|
// Prepare the Ghostscript command
|
||||||
try (PDDocument doc = Loader.loadPDF(new File(tempOutputFile.toString()))) {
|
List<String> command = new ArrayList<>();
|
||||||
long previousFileSize = 0;
|
command.add("gs");
|
||||||
double scaleFactor = 1.0;
|
command.add("-sDEVICE=pdfwrite");
|
||||||
while (true) {
|
command.add("-dCompatibilityLevel=1.4");
|
||||||
for (PDPage page : doc.getPages()) {
|
|
||||||
PDResources res = page.getResources();
|
|
||||||
|
|
||||||
for (COSName name : res.getXObjectNames()) {
|
switch (optimizeLevel) {
|
||||||
PDXObject xobj = res.getXObject(name);
|
case 1:
|
||||||
if (xobj instanceof PDImageXObject) {
|
command.add("-dPDFSETTINGS=/prepress");
|
||||||
PDImageXObject image = (PDImageXObject) xobj;
|
break;
|
||||||
|
case 2:
|
||||||
|
command.add("-dPDFSETTINGS=/printer");
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
command.add("-dPDFSETTINGS=/ebook");
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
command.add("-dPDFSETTINGS=/screen");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
command.add("-dPDFSETTINGS=/default");
|
||||||
|
}
|
||||||
|
|
||||||
// Get the image in BufferedImage format
|
command.add("-dNOPAUSE");
|
||||||
BufferedImage bufferedImage = image.getImage();
|
command.add("-dQUIET");
|
||||||
|
command.add("-dBATCH");
|
||||||
|
command.add("-sOutputFile=" + tempOutputFile.toString());
|
||||||
|
command.add(tempInputFile.toString());
|
||||||
|
|
||||||
// Calculate the new dimensions
|
ProcessExecutorResult returnCode =
|
||||||
int newWidth = (int) (bufferedImage.getWidth() * scaleFactor);
|
ProcessExecutor.getInstance(ProcessExecutor.Processes.GHOSTSCRIPT)
|
||||||
int newHeight = (int) (bufferedImage.getHeight() * scaleFactor);
|
.runCommandWithOutputHandling(command);
|
||||||
|
|
||||||
// If the new dimensions are zero, skip this iteration
|
// Check if file size is within expected size or not auto mode so instantly finish
|
||||||
if (newWidth == 0 || newHeight == 0) {
|
long outputFileSize = Files.size(tempOutputFile);
|
||||||
continue;
|
if (outputFileSize <= expectedOutputSize || !autoMode) {
|
||||||
|
sizeMet = true;
|
||||||
|
} else {
|
||||||
|
// Increase optimization level for next iteration
|
||||||
|
optimizeLevel++;
|
||||||
|
if (autoMode && optimizeLevel > 4) {
|
||||||
|
System.out.println("Skipping level 5 due to bad results in auto mode");
|
||||||
|
sizeMet = true;
|
||||||
|
} else {
|
||||||
|
System.out.println(
|
||||||
|
"Increasing ghostscript optimisation level to " + optimizeLevel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (expectedOutputSize != null && autoMode) {
|
||||||
|
long outputFileSize = Files.size(tempOutputFile);
|
||||||
|
byte[] fileBytes = Files.readAllBytes(tempOutputFile);
|
||||||
|
if (outputFileSize > expectedOutputSize) {
|
||||||
|
try (PDDocument doc = Loader.loadPDF(fileBytes)) {
|
||||||
|
long previousFileSize = 0;
|
||||||
|
double scaleFactorConst = 0.9f;
|
||||||
|
double scaleFactor = 0.9f;
|
||||||
|
while (true) {
|
||||||
|
for (PDPage page : doc.getPages()) {
|
||||||
|
PDResources res = page.getResources();
|
||||||
|
if (res != null && res.getXObjectNames() != null) {
|
||||||
|
for (COSName name : res.getXObjectNames()) {
|
||||||
|
PDXObject xobj = res.getXObject(name);
|
||||||
|
if (xobj != null && xobj instanceof PDImageXObject) {
|
||||||
|
PDImageXObject image = (PDImageXObject) xobj;
|
||||||
|
|
||||||
|
// Get the image in BufferedImage format
|
||||||
|
BufferedImage bufferedImage = image.getImage();
|
||||||
|
|
||||||
|
// Calculate the new dimensions
|
||||||
|
int newWidth =
|
||||||
|
(int)
|
||||||
|
(bufferedImage.getWidth()
|
||||||
|
* scaleFactorConst);
|
||||||
|
int newHeight =
|
||||||
|
(int)
|
||||||
|
(bufferedImage.getHeight()
|
||||||
|
* scaleFactorConst);
|
||||||
|
|
||||||
|
// If the new dimensions are zero, skip this iteration
|
||||||
|
if (newWidth == 0 || newHeight == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, proceed with the scaling
|
||||||
|
Image scaledImage =
|
||||||
|
bufferedImage.getScaledInstance(
|
||||||
|
newWidth,
|
||||||
|
newHeight,
|
||||||
|
Image.SCALE_SMOOTH);
|
||||||
|
|
||||||
|
// Convert the scaled image back to a BufferedImage
|
||||||
|
BufferedImage scaledBufferedImage =
|
||||||
|
new BufferedImage(
|
||||||
|
newWidth,
|
||||||
|
newHeight,
|
||||||
|
BufferedImage.TYPE_INT_RGB);
|
||||||
|
scaledBufferedImage
|
||||||
|
.getGraphics()
|
||||||
|
.drawImage(scaledImage, 0, 0, null);
|
||||||
|
|
||||||
|
// Compress the scaled image
|
||||||
|
ByteArrayOutputStream compressedImageStream =
|
||||||
|
new ByteArrayOutputStream();
|
||||||
|
ImageIO.write(
|
||||||
|
scaledBufferedImage,
|
||||||
|
"jpeg",
|
||||||
|
compressedImageStream);
|
||||||
|
byte[] imageBytes = compressedImageStream.toByteArray();
|
||||||
|
compressedImageStream.close();
|
||||||
|
|
||||||
|
PDImageXObject compressedImage =
|
||||||
|
PDImageXObject.createFromByteArray(
|
||||||
|
doc,
|
||||||
|
imageBytes,
|
||||||
|
image.getCOSObject().toString());
|
||||||
|
|
||||||
|
// Replace the image in the resources with the
|
||||||
|
// compressed
|
||||||
|
// version
|
||||||
|
res.put(name, compressedImage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, proceed with the scaling
|
|
||||||
Image scaledImage =
|
|
||||||
bufferedImage.getScaledInstance(
|
|
||||||
newWidth, newHeight, Image.SCALE_SMOOTH);
|
|
||||||
|
|
||||||
// Convert the scaled image back to a BufferedImage
|
|
||||||
BufferedImage scaledBufferedImage =
|
|
||||||
new BufferedImage(
|
|
||||||
newWidth,
|
|
||||||
newHeight,
|
|
||||||
BufferedImage.TYPE_INT_RGB);
|
|
||||||
scaledBufferedImage
|
|
||||||
.getGraphics()
|
|
||||||
.drawImage(scaledImage, 0, 0, null);
|
|
||||||
|
|
||||||
// Compress the scaled image
|
|
||||||
ByteArrayOutputStream compressedImageStream =
|
|
||||||
new ByteArrayOutputStream();
|
|
||||||
ImageIO.write(
|
|
||||||
scaledBufferedImage, "jpeg", compressedImageStream);
|
|
||||||
byte[] imageBytes = compressedImageStream.toByteArray();
|
|
||||||
compressedImageStream.close();
|
|
||||||
|
|
||||||
// Convert compressed image back to PDImageXObject
|
|
||||||
ByteArrayInputStream bais =
|
|
||||||
new ByteArrayInputStream(imageBytes);
|
|
||||||
PDImageXObject compressedImage =
|
|
||||||
PDImageXObject.createFromByteArray(
|
|
||||||
doc,
|
|
||||||
imageBytes,
|
|
||||||
image.getCOSObject().toString());
|
|
||||||
|
|
||||||
// Replace the image in the resources with the compressed
|
|
||||||
// version
|
|
||||||
res.put(name, compressedImage);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// save the document to tempOutputFile again
|
// save the document to tempOutputFile again
|
||||||
doc.save(tempOutputFile.toString());
|
doc.save(tempOutputFile.toString());
|
||||||
|
|
||||||
long currentSize = Files.size(tempOutputFile);
|
long currentSize = Files.size(tempOutputFile);
|
||||||
// Check if the overall PDF size is still larger than expectedOutputSize
|
// Check if the overall PDF size is still larger than expectedOutputSize
|
||||||
if (currentSize > expectedOutputSize) {
|
if (currentSize > expectedOutputSize) {
|
||||||
// Log the current file size and scaleFactor
|
// Log the current file size and scaleFactor
|
||||||
|
|
||||||
System.out.println(
|
System.out.println(
|
||||||
"Current file size: "
|
"Current file size: "
|
||||||
+ FileUtils.byteCountToDisplaySize(currentSize));
|
+ FileUtils.byteCountToDisplaySize(currentSize));
|
||||||
System.out.println("Current scale factor: " + scaleFactor);
|
System.out.println("Current scale factor: " + scaleFactor);
|
||||||
|
|
||||||
// The file is still too large, reduce scaleFactor and try again
|
// The file is still too large, reduce scaleFactor and try again
|
||||||
scaleFactor *= 0.9; // reduce scaleFactor by 10%
|
scaleFactor *= 0.9f; // reduce scaleFactor by 10%
|
||||||
// Avoid scaleFactor being too small, causing the image to shrink to 0
|
// Avoid scaleFactor being too small, causing the image to shrink to
|
||||||
if (scaleFactor < 0.2 || previousFileSize == currentSize) {
|
// 0
|
||||||
throw new RuntimeException(
|
if (scaleFactor < 0.2f || previousFileSize == currentSize) {
|
||||||
"Could not reach the desired size without excessively degrading image quality, lowest size recommended is "
|
throw new RuntimeException(
|
||||||
+ FileUtils.byteCountToDisplaySize(currentSize)
|
"Could not reach the desired size without excessively degrading image quality, lowest size recommended is "
|
||||||
+ ", "
|
+ FileUtils.byteCountToDisplaySize(currentSize)
|
||||||
+ currentSize
|
+ ", "
|
||||||
+ " bytes");
|
+ currentSize
|
||||||
|
+ " bytes");
|
||||||
|
}
|
||||||
|
previousFileSize = currentSize;
|
||||||
|
} else {
|
||||||
|
// The file is small enough, break the loop
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
previousFileSize = currentSize;
|
|
||||||
} else {
|
|
||||||
// The file is small enough, break the loop
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read the optimized PDF file
|
||||||
|
pdfBytes = Files.readAllBytes(tempOutputFile);
|
||||||
|
|
||||||
|
// Check if optimized file is larger than the original
|
||||||
|
if (pdfBytes.length > inputFileSize) {
|
||||||
|
// Log the occurrence
|
||||||
|
logger.warn(
|
||||||
|
"Optimized file is larger than the original. Returning the original file instead.");
|
||||||
|
|
||||||
|
// Read the original file again
|
||||||
|
pdfBytes = Files.readAllBytes(tempInputFile);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
// Clean up the temporary files
|
||||||
|
Files.delete(tempInputFile);
|
||||||
|
Files.delete(tempOutputFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the optimized PDF file
|
|
||||||
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
|
||||||
|
|
||||||
// Check if optimized file is larger than the original
|
|
||||||
if (pdfBytes.length > inputFileSize) {
|
|
||||||
// Log the occurrence
|
|
||||||
logger.warn(
|
|
||||||
"Optimized file is larger than the original. Returning the original file instead.");
|
|
||||||
|
|
||||||
// Read the original file again
|
|
||||||
pdfBytes = Files.readAllBytes(tempInputFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up the temporary files
|
|
||||||
Files.delete(tempInputFile);
|
|
||||||
Files.delete(tempOutputFile);
|
|
||||||
|
|
||||||
// Return the optimized PDF as a response
|
// Return the optimized PDF as a response
|
||||||
String outputFilename =
|
String outputFilename =
|
||||||
Filenames.toSimpleFileName(inputFile.getOriginalFilename())
|
Filenames.toSimpleFileName(inputFile.getOriginalFilename())
|
||||||
|
|
|
@ -84,6 +84,7 @@ public class ExtractImageScansController {
|
||||||
// Load PDF document
|
// Load PDF document
|
||||||
try (PDDocument document = Loader.loadPDF(form.getFileInput().getBytes())) {
|
try (PDDocument document = Loader.loadPDF(form.getFileInput().getBytes())) {
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
|
pdfRenderer.setSubsamplingAllowed(true);
|
||||||
int pageCount = document.getNumberOfPages();
|
int pageCount = document.getNumberOfPages();
|
||||||
images = new ArrayList<>();
|
images = new ArrayList<>();
|
||||||
|
|
||||||
|
|
|
@ -60,6 +60,7 @@ public class FakeScanControllerWIP {
|
||||||
|
|
||||||
PDDocument document = Loader.loadPDF(inputFile.getBytes());
|
PDDocument document = Loader.loadPDF(inputFile.getBytes());
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
|
pdfRenderer.setSubsamplingAllowed(true);
|
||||||
for (int page = 0; page < document.getNumberOfPages(); ++page) {
|
for (int page = 0; page < document.getNumberOfPages(); ++page) {
|
||||||
BufferedImage image = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
|
BufferedImage image = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
|
||||||
ImageIO.write(image, "png", new File("scanned-" + (page + 1) + ".png"));
|
ImageIO.write(image, "png", new File("scanned-" + (page + 1) + ".png"));
|
||||||
|
|
|
@ -12,7 +12,6 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
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;
|
||||||
|
@ -50,9 +49,6 @@ public class PipelineController {
|
||||||
@PostMapping("/handleData")
|
@PostMapping("/handleData")
|
||||||
public ResponseEntity<byte[]> handleData(@ModelAttribute HandleDataRequest request)
|
public ResponseEntity<byte[]> handleData(@ModelAttribute HandleDataRequest request)
|
||||||
throws JsonMappingException, JsonProcessingException {
|
throws JsonMappingException, JsonProcessingException {
|
||||||
if (!Boolean.TRUE.equals(applicationProperties.getSystem().getEnableAlphaFunctionality())) {
|
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
MultipartFile[] files = request.getFileInput();
|
MultipartFile[] files = request.getFileInput();
|
||||||
String jsonString = request.getJson();
|
String jsonString = request.getJson();
|
||||||
|
|
|
@ -83,6 +83,7 @@ public class RedactController {
|
||||||
if (convertPDFToImage) {
|
if (convertPDFToImage) {
|
||||||
PDDocument imageDocument = new PDDocument();
|
PDDocument imageDocument = new PDDocument();
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
|
pdfRenderer.setSubsamplingAllowed(true);
|
||||||
for (int page = 0; page < document.getNumberOfPages(); ++page) {
|
for (int page = 0; page < document.getNumberOfPages(); ++page) {
|
||||||
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
|
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
|
||||||
PDPage newPage = new PDPage(new PDRectangle(bim.getWidth(), bim.getHeight()));
|
PDPage newPage = new PDPage(new PDRectangle(bim.getWidth(), bim.getHeight()));
|
||||||
|
|
|
@ -54,33 +54,32 @@ public class SanitizeController {
|
||||||
boolean removeLinks = request.isRemoveLinks();
|
boolean removeLinks = request.isRemoveLinks();
|
||||||
boolean removeFonts = request.isRemoveFonts();
|
boolean removeFonts = request.isRemoveFonts();
|
||||||
|
|
||||||
try (PDDocument document = Loader.loadPDF(inputFile.getBytes())) {
|
PDDocument document = Loader.loadPDF(inputFile.getBytes());
|
||||||
if (removeJavaScript) {
|
if (removeJavaScript) {
|
||||||
sanitizeJavaScript(document);
|
sanitizeJavaScript(document);
|
||||||
}
|
|
||||||
|
|
||||||
if (removeEmbeddedFiles) {
|
|
||||||
sanitizeEmbeddedFiles(document);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (removeMetadata) {
|
|
||||||
sanitizeMetadata(document);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (removeLinks) {
|
|
||||||
sanitizeLinks(document);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (removeFonts) {
|
|
||||||
sanitizeFonts(document);
|
|
||||||
}
|
|
||||||
|
|
||||||
return WebResponseUtils.pdfDocToWebResponse(
|
|
||||||
document,
|
|
||||||
Filenames.toSimpleFileName(inputFile.getOriginalFilename())
|
|
||||||
.replaceFirst("[.][^.]+$", "")
|
|
||||||
+ "_sanitized.pdf");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (removeEmbeddedFiles) {
|
||||||
|
sanitizeEmbeddedFiles(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (removeMetadata) {
|
||||||
|
sanitizeMetadata(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (removeLinks) {
|
||||||
|
sanitizeLinks(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (removeFonts) {
|
||||||
|
sanitizeFonts(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
return WebResponseUtils.pdfDocToWebResponse(
|
||||||
|
document,
|
||||||
|
Filenames.toSimpleFileName(inputFile.getOriginalFilename())
|
||||||
|
.replaceFirst("[.][^.]+$", "")
|
||||||
|
+ "_sanitized.pdf");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sanitizeJavaScript(PDDocument document) throws IOException {
|
private void sanitizeJavaScript(PDDocument document) throws IOException {
|
||||||
|
|
|
@ -136,6 +136,7 @@ public class GeneralUtils {
|
||||||
int offset = oneBased ? 1 : 0;
|
int offset = oneBased ? 1 : 0;
|
||||||
for (String page : pages) {
|
for (String page : pages) {
|
||||||
if ("all".equalsIgnoreCase(page)) {
|
if ("all".equalsIgnoreCase(page)) {
|
||||||
|
|
||||||
for (int i = 0; i < totalPages; i++) {
|
for (int i = 0; i < totalPages; i++) {
|
||||||
result.add(i + offset);
|
result.add(i + offset);
|
||||||
}
|
}
|
||||||
|
|
|
@ -214,6 +214,7 @@ public class PdfUtils {
|
||||||
throws IOException, Exception {
|
throws IOException, Exception {
|
||||||
try (PDDocument document = Loader.loadPDF(inputStream)) {
|
try (PDDocument document = Loader.loadPDF(inputStream)) {
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
|
pdfRenderer.setSubsamplingAllowed(true);
|
||||||
int pageCount = document.getNumberOfPages();
|
int pageCount = document.getNumberOfPages();
|
||||||
|
|
||||||
// Create a ByteArrayOutputStream to save the image(s) to
|
// Create a ByteArrayOutputStream to save the image(s) to
|
||||||
|
|
|
@ -189,7 +189,11 @@ public class ProcessExecutor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (exitCode != 0) {
|
} else if (exitCode != 0) {
|
||||||
throw new IOException("Command process failed with exit code " + exitCode);
|
throw new IOException(
|
||||||
|
"Command process failed with exit code "
|
||||||
|
+ exitCode
|
||||||
|
+ "\nLogs: "
|
||||||
|
+ messages);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
semaphore.release();
|
semaphore.release();
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
fileToPDF.fileTypesList=Microsoft Word: (DOC, DOCX, DOT, DOTX) <br> \
|
|
||||||
Microsoft Excel: (CSV, XLS, XLSX, XLT, XLTX, SLK, DIF) <br> \
|
|
||||||
Microsoft PowerPoint: (PPT, PPTX) <br> \
|
|
||||||
OpenDocument Formats: (ODT, OTT, ODS, OTS, ODP, OTP, ODG, OTG) <br> \
|
|
||||||
Plain Text: (TXT, TEXT, XML) <br> \
|
|
||||||
Rich Text Format: (RTF) <br> \
|
|
||||||
Images: (BMP, GIF, JPEG, PNG, TIF, PBM, PGM, PPM, RAS, XBM, XPM, SVG, SVM, WMF) <br> \
|
|
||||||
HTML: (HTML) <br> \
|
|
||||||
Lotus Word Pro: (LWP) <br> \
|
|
||||||
StarOffice formats: (SDA, SDC, SDD, SDW, STC, STD, STI, STW, SXD, SXG, SXI, SXW) <br> \
|
|
||||||
Other formats: (DBF, FODS, VSD, VOR, VOR3, VOR4, UOP, PCT, PS, PDF)
|
|
|
@ -22,7 +22,17 @@
|
||||||
</form>
|
</form>
|
||||||
<p class="mt-3" th:text="#{fileToPDF.credit}"></p>
|
<p class="mt-3" th:text="#{fileToPDF.credit}"></p>
|
||||||
<p class="mt-3" th:text="#{fileToPDF.supportedFileTypes}"></p>
|
<p class="mt-3" th:text="#{fileToPDF.supportedFileTypes}"></p>
|
||||||
<p th:utext="#{fileToPDF.fileTypesList}"></p>
|
<p>Microsoft Word: (DOC, DOCX, DOT, DOTX)</p>
|
||||||
|
<p>Microsoft Excel: (CSV, XLS, XLSX, XLT, XLTX, SLK, DIF)</p>
|
||||||
|
<p>Microsoft PowerPoint: (PPT, PPTX)</p>
|
||||||
|
<p>OpenDocument Formats: (ODT, OTT, ODS, OTS, ODP, OTP, ODG, OTG)</p>
|
||||||
|
<p>Plain Text: (TXT, TEXT, XML)</p>
|
||||||
|
<p>Rich Text Format: (RTF)</p>
|
||||||
|
<p>Images: (BMP, GIF, JPEG, PNG, TIF, PBM, PGM, PPM, RAS, XBM, XPM, SVG, SVM, WMF)</p>
|
||||||
|
<p>HTML: (HTML)</p>
|
||||||
|
<p>Lotus Word Pro: (LWP)</p>
|
||||||
|
<p>StarOffice: (SDA, SDC, SDD, SDW, STC, STD, STI, STW, SXD, SXG, SXI, SXW)</p>
|
||||||
|
<p>Other: (DBF, FODS, VSD, VOR, VOR3, VOR4, UOP, PCT, PS, PDF)</p>
|
||||||
<a href="https://help.libreoffice.org/latest/en-US/text/shared/guide/supported_formats.html">https://help.libreoffice.org/latest/en-US/text/shared/guide/supported_formats.html</a>
|
<a href="https://help.libreoffice.org/latest/en-US/text/shared/guide/supported_formats.html">https://help.libreoffice.org/latest/en-US/text/shared/guide/supported_formats.html</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -22,8 +22,8 @@
|
||||||
<span class="icon-text" th:text="#{home.multiTool.title}"></span>
|
<span class="icon-text" th:text="#{home.multiTool.title}"></span>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<li th:if="${@enableAlphaFunctionality}" class="nav-item nav-item-separator"></li>
|
<li class="nav-item nav-item-separator"></li>
|
||||||
<li th:if="${@enableAlphaFunctionality}" class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link" href="#" th:href="@{pipeline}" th:classappend="${currentPage}=='pipeline' ? 'active' : ''" th:title="#{home.pipeline.desc}">
|
<a class="nav-link" href="#" th:href="@{pipeline}" th:classappend="${currentPage}=='pipeline' ? 'active' : ''" th:title="#{home.pipeline.desc}">
|
||||||
<img class="icon" src="images/pipeline.svg" alt="icon">
|
<img class="icon" src="images/pipeline.svg" alt="icon">
|
||||||
<span class="icon-text" th:text="#{home.pipeline.title}"></span>
|
<span class="icon-text" th:text="#{home.pipeline.title}"></span>
|
||||||
|
|
|
@ -23,9 +23,8 @@
|
||||||
<br>
|
<br>
|
||||||
<input type="text" id="searchBar" onkeyup="filterCards()" th:placeholder="#{home.searchBar}">
|
<input type="text" id="searchBar" onkeyup="filterCards()" th:placeholder="#{home.searchBar}">
|
||||||
<div class="features-container">
|
<div class="features-container">
|
||||||
<th:block th:if="${@enableAlphaFunctionality}">
|
|
||||||
<div th:replace="~{fragments/card :: card(id='pipeline', cardTitle=#{home.pipeline.title}, cardText=#{home.pipeline.desc}, cardLink='pipeline', svgPath='images/pipeline.svg', tags=#{pipeline.tags})}"></div>
|
<div th:replace="~{fragments/card :: card(id='pipeline', cardTitle=#{home.pipeline.title}, cardText=#{home.pipeline.desc}, cardLink='pipeline', svgPath='images/pipeline.svg', tags=#{pipeline.tags})}"></div>
|
||||||
</th:block>
|
|
||||||
<div th:replace="~{fragments/card :: card(id='view-pdf', cardTitle=#{home.viewPdf.title}, cardText=#{home.viewPdf.desc}, cardLink='view-pdf', svgPath='images/book-opened.svg', tags=#{viewPdf.tags})}"></div>
|
<div th:replace="~{fragments/card :: card(id='view-pdf', cardTitle=#{home.viewPdf.title}, cardText=#{home.viewPdf.desc}, cardLink='view-pdf', svgPath='images/book-opened.svg', tags=#{viewPdf.tags})}"></div>
|
||||||
<div th:replace="~{fragments/card :: card(id='multi-tool', cardTitle=#{home.multiTool.title}, cardText=#{home.multiTool.desc}, cardLink='multi-tool', svgPath='images/tools.svg', tags=#{multiTool.tags})}"></div>
|
<div th:replace="~{fragments/card :: card(id='multi-tool', cardTitle=#{home.multiTool.title}, cardText=#{home.multiTool.desc}, cardLink='multi-tool', svgPath='images/tools.svg', tags=#{multiTool.tags})}"></div>
|
||||||
<div th:replace="~{fragments/card :: card(id='merge-pdfs', cardTitle=#{home.merge.title}, cardText=#{home.merge.desc}, cardLink='merge-pdfs', svgPath='images/union.svg', tags=#{merge.tags})}"></div>
|
<div th:replace="~{fragments/card :: card(id='merge-pdfs', cardTitle=#{home.merge.title}, cardText=#{home.merge.desc}, cardLink='merge-pdfs', svgPath='images/union.svg', tags=#{merge.tags})}"></div>
|
||||||
|
|
126
test2.sh
Normal file
126
test2.sh
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Default values
|
||||||
|
build_type="full"
|
||||||
|
enable_security="false"
|
||||||
|
run_compose="true"
|
||||||
|
|
||||||
|
# Function to parse command line arguments
|
||||||
|
parse_args() {
|
||||||
|
case "$1" in
|
||||||
|
""|-lite|-ultra-lite) build_type="$1";;
|
||||||
|
esac
|
||||||
|
|
||||||
|
case "$2" in
|
||||||
|
true|false) enable_security="$2";;
|
||||||
|
esac
|
||||||
|
|
||||||
|
case "$3" in
|
||||||
|
true|false) run_compose="$3";;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to check the health of the service with a timeout of 80 seconds
|
||||||
|
check_health() {
|
||||||
|
local service_name=$1
|
||||||
|
local compose_file=$2
|
||||||
|
local end=$((SECONDS+80)) # Fixed the timeout to be 80 seconds as per the function comment
|
||||||
|
|
||||||
|
echo -n "Waiting for $service_name to become healthy..."
|
||||||
|
until [ "$(docker inspect --format='{{json .State.Health.Status}}' "$service_name")" == '"healthy"' ] || [ $SECONDS -ge $end ]; do
|
||||||
|
sleep 3
|
||||||
|
echo -n "."
|
||||||
|
if [ $SECONDS -ge $end ]; then
|
||||||
|
echo -e "\n$service_name health check timed out after 80 seconds."
|
||||||
|
echo "Printing logs for $service_name:"
|
||||||
|
docker logs "$service_name"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo -e "\n$service_name is healthy!"
|
||||||
|
echo "Printing logs for $service_name:"
|
||||||
|
docker logs "$service_name"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to build and test a Docker Compose configuration
|
||||||
|
# Function to build and test a Docker Compose configuration
|
||||||
|
# Function to build and test a Docker Compose configuration
|
||||||
|
build_and_test() {
|
||||||
|
local version_tag="alpha"
|
||||||
|
local dockerfile_name="./Dockerfile"
|
||||||
|
local image_base="frooodle/s-pdf"
|
||||||
|
local security_suffix=""
|
||||||
|
local docker_compose_base="./exampleYmlFiles/docker-compose-latest"
|
||||||
|
local compose_suffix=".yml"
|
||||||
|
local service_name_base="Stirling-PDF"
|
||||||
|
|
||||||
|
if [ "$enable_security" == "true" ]; then
|
||||||
|
security_suffix="-Security"
|
||||||
|
docker_compose_base+="-security" # Append to base name for Docker Compose files with security
|
||||||
|
fi
|
||||||
|
|
||||||
|
case "$build_type" in
|
||||||
|
full)
|
||||||
|
dockerfile_name="./Dockerfile"
|
||||||
|
;;
|
||||||
|
lite)
|
||||||
|
dockerfile_name="./Dockerfile-lite"
|
||||||
|
;;
|
||||||
|
ultra-lite)
|
||||||
|
dockerfile_name="./Dockerfile-ultra-lite"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Dynamic image tag and service name based on build type and security
|
||||||
|
local image_tag="${image_base}:latest${build_type}${security_suffix}"
|
||||||
|
local service_name="${service_name_base}${build_type^}${security_suffix}"
|
||||||
|
local compose_file="${docker_compose_base}${build_type}${compose_suffix}"
|
||||||
|
|
||||||
|
# Gradle build with or without security
|
||||||
|
echo "Running ./gradlew clean build with security=$enable_security..."
|
||||||
|
./gradlew clean build
|
||||||
|
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "Gradle build failed, exiting script."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Building Docker image
|
||||||
|
echo "Building Docker image $image_tag with Dockerfile $dockerfile_name..."
|
||||||
|
docker build --build-arg VERSION_TAG=$version_tag -t $image_tag -f $dockerfile_name .
|
||||||
|
|
||||||
|
if [ "$run_compose" == "true" ]; then
|
||||||
|
echo "Running Docker Compose for $build_type with security=$enable_security..."
|
||||||
|
docker-compose -f "$compose_file" up -d
|
||||||
|
|
||||||
|
# Health check using the dynamic service name
|
||||||
|
if ! check_health "$service_name" "$compose_file"; then
|
||||||
|
echo "$service_name health check failed."
|
||||||
|
docker-compose -f "$compose_file" down
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
# If the health check passes, prompt the user to press any key to tear down the service
|
||||||
|
read -n 1 -s -r -p "Health check passed. Press any key to tear down the service."
|
||||||
|
echo "" # Move to a new line
|
||||||
|
|
||||||
|
# Tear down the service
|
||||||
|
docker-compose -f "$compose_file" down
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Tear down the service after the health check passes
|
||||||
|
#docker-compose -f "$compose_file" down
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Main function
|
||||||
|
main() {
|
||||||
|
SECONDS=0
|
||||||
|
parse_args "$@"
|
||||||
|
build_and_test
|
||||||
|
echo "All operations completed in $SECONDS seconds."
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@"
|
Loading…
Reference in a new issue