From 734d76a3b5557fc6ffa90121e51e290239cb5d57 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Tue, 6 Feb 2024 00:00:49 +0000 Subject: [PATCH 1/8] test --- .../software/SPDF/LibreOfficeListener.java | 3 +- .../software/SPDF/SPdfApplication.java | 3 +- .../api/MultiPageLayoutController.java | 5 +- .../controller/api/PdfOverlayController.java | 5 +- .../api/RearrangePagesPDFController.java | 9 +- .../controller/api/RotationController.java | 6 +- .../controller/api/ScalePagesController.java | 5 +- .../controller/api/SplitPDFController.java | 6 +- .../api/SplitPdfBySectionsController.java | 6 +- .../api/SplitPdfBySizeController.java | 6 +- .../ConvertBookToPDFController.java | 2 +- .../api/converters/ConvertHtmlToPDF.java | 2 +- .../converters/ConvertImgPDFController.java | 6 +- .../api/converters/ConvertMarkdownToPdf.java | 2 +- .../converters/ConvertOfficeController.java | 5 +- .../ConvertPDFToBookController.java | 5 +- .../api/converters/ConvertPDFToPDFA.java | 6 +- .../api/filters/FilterController.java | 2 +- .../api/misc/AutoRenameController.java | 5 +- .../api/misc/AutoSplitPdfController.java | 6 +- .../api/misc/BlankPageController.java | 101 +++++++++--------- .../api/misc/CompressController.java | 6 +- .../api/misc/ExtractImagesController.java | 6 +- .../api/misc/FakeScanControllerWIP.java | 6 +- .../api/misc/MetadataController.java | 6 +- .../controller/api/misc/OCRController.java | 10 +- .../api/misc/OverlayImageController.java | 6 +- .../api/misc/PageNumbersController.java | 5 +- .../controller/api/misc/RepairController.java | 6 +- .../controller/api/misc/ShowJavascript.java | 5 +- .../controller/api/misc/StampController.java | 85 ++++++++------- .../api/pipeline/PipelineProcessor.java | 5 +- .../api/security/CertSignController.java | 6 +- .../api/security/PasswordController.java | 12 ++- .../api/security/RedactController.java | 5 +- .../api/security/SanitizeController.java | 5 +- .../api/security/WatermarkController.java | 6 +- .../SPDF/model/api/misc/AddStampRequest.java | 4 +- .../software/SPDF/utils/FileToPdf.java | 6 +- .../software/SPDF/utils/GeneralUtils.java | 17 ++- .../software/SPDF/utils/PDFToFile.java | 3 +- .../software/SPDF/utils/PdfUtils.java | 3 +- .../software/SPDF/utils/ProcessExecutor.java | 13 ++- .../software/SPDF/utils/WebResponseUtils.java | 3 +- src/main/resources/static/js/game.js | 19 +++- 45 files changed, 268 insertions(+), 176 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/LibreOfficeListener.java b/src/main/java/stirling/software/SPDF/LibreOfficeListener.java index d8196dcd..96c4d270 100644 --- a/src/main/java/stirling/software/SPDF/LibreOfficeListener.java +++ b/src/main/java/stirling/software/SPDF/LibreOfficeListener.java @@ -1,12 +1,13 @@ package stirling.software.SPDF; -import io.github.pixee.security.SystemCommand; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import io.github.pixee.security.SystemCommand; + public class LibreOfficeListener { private static final long ACTIVITY_TIMEOUT = 20 * 60 * 1000; // 20 minutes diff --git a/src/main/java/stirling/software/SPDF/SPdfApplication.java b/src/main/java/stirling/software/SPDF/SPdfApplication.java index f37dfb6e..62cbfe10 100644 --- a/src/main/java/stirling/software/SPDF/SPdfApplication.java +++ b/src/main/java/stirling/software/SPDF/SPdfApplication.java @@ -1,6 +1,5 @@ package stirling.software.SPDF; -import io.github.pixee.security.SystemCommand; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Collections; @@ -11,6 +10,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.env.Environment; import org.springframework.scheduling.annotation.EnableScheduling; +import io.github.pixee.security.SystemCommand; + import jakarta.annotation.PostConstruct; import stirling.software.SPDF.config.ConfigInitializer; import stirling.software.SPDF.utils.GeneralUtils; diff --git a/src/main/java/stirling/software/SPDF/controller/api/MultiPageLayoutController.java b/src/main/java/stirling/software/SPDF/controller/api/MultiPageLayoutController.java index b4949a58..adc5424d 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/MultiPageLayoutController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/MultiPageLayoutController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api; -import io.github.pixee.security.Filenames; import java.awt.Color; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -22,6 +21,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -137,6 +137,7 @@ public class MultiPageLayoutController { byte[] result = baos.toByteArray(); return WebResponseUtils.bytesToWebResponse( result, - Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_layoutChanged.pdf"); + Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + + "_layoutChanged.pdf"); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.java b/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.java index f9938a39..a6148d4a 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api; -import io.github.pixee.security.Filenames; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -21,6 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -77,7 +77,8 @@ public class PdfOverlayController { overlay.overlay(overlayGuide).save(outputStream); byte[] data = outputStream.toByteArray(); String outputFilename = - Filenames.toSimpleFileName(baseFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + Filenames.toSimpleFileName(baseFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + "_overlayed.pdf"; // Remove file extension and append .pdf return WebResponseUtils.bytesToWebResponse( diff --git a/src/main/java/stirling/software/SPDF/controller/api/RearrangePagesPDFController.java b/src/main/java/stirling/software/SPDF/controller/api/RearrangePagesPDFController.java index 3074f9f8..88bdcd91 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/RearrangePagesPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/RearrangePagesPDFController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api; -import io.github.pixee.security.Filenames; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -17,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -58,7 +58,9 @@ public class RearrangePagesPDFController { } return WebResponseUtils.pdfDocToWebResponse( document, - Filenames.toSimpleFileName(pdfFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_removed_pages.pdf"); + Filenames.toSimpleFileName(pdfFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_removed_pages.pdf"); } private List removeFirst(int totalPages) { @@ -212,7 +214,8 @@ public class RearrangePagesPDFController { return WebResponseUtils.pdfDocToWebResponse( document, - Filenames.toSimpleFileName(pdfFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + Filenames.toSimpleFileName(pdfFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + "_rearranged.pdf"); } catch (IOException e) { logger.error("Failed rearranging documents", e); diff --git a/src/main/java/stirling/software/SPDF/controller/api/RotationController.java b/src/main/java/stirling/software/SPDF/controller/api/RotationController.java index 609e20f8..76f508d3 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/RotationController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/RotationController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api; -import io.github.pixee.security.Filenames; import java.io.IOException; import org.apache.pdfbox.Loader; @@ -16,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -50,6 +50,8 @@ public class RotationController { return WebResponseUtils.pdfDocToWebResponse( document, - Filenames.toSimpleFileName(pdfFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_rotated.pdf"); + Filenames.toSimpleFileName(pdfFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_rotated.pdf"); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/ScalePagesController.java b/src/main/java/stirling/software/SPDF/controller/api/ScalePagesController.java index 03994321..91cd012d 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/ScalePagesController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/ScalePagesController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api; -import io.github.pixee.security.Filenames; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.HashMap; @@ -23,6 +22,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -113,6 +113,7 @@ public class ScalePagesController { return WebResponseUtils.bytesToWebResponse( baos.toByteArray(), - Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_scaled.pdf"); + Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + + "_scaled.pdf"); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java b/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java index c218788a..44c87464 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api; -import io.github.pixee.security.Filenames; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Files; @@ -24,6 +23,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -84,7 +84,9 @@ public class SplitPDFController { Path zipFile = Files.createTempFile("split_documents", ".zip"); - String filename = Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", ""); + String filename = + Filenames.toSimpleFileName(file.getOriginalFilename()) + .replaceFirst("[.][^.]+$", ""); try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipFile))) { // loop through the split documents and write them to the zip file for (int i = 0; i < splitDocumentsBoas.size(); i++) { diff --git a/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java b/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java index 2951a73e..3deb798e 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySectionsController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api; -import io.github.pixee.security.Filenames; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Files; @@ -27,6 +26,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -65,7 +65,9 @@ public class SplitPdfBySectionsController { sourceDocument.close(); Path zipFile = Files.createTempFile("split_documents", ".zip"); - String filename = Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", ""); + String filename = + Filenames.toSimpleFileName(file.getOriginalFilename()) + .replaceFirst("[.][^.]+$", ""); byte[] data; try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipFile))) { diff --git a/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySizeController.java b/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySizeController.java index eb2cfa28..45d2dd38 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySizeController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/SplitPdfBySizeController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api; -import io.github.pixee.security.Filenames; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Files; @@ -21,6 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -121,7 +121,9 @@ public class SplitPdfBySizeController { sourceDocument.close(); Path zipFile = Files.createTempFile("split_documents", ".zip"); - String filename = Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", ""); + String filename = + Filenames.toSimpleFileName(file.getOriginalFilename()) + .replaceFirst("[.][^.]+$", ""); byte[] data; try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipFile))) { diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertBookToPDFController.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertBookToPDFController.java index a266e871..3cbc783c 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertBookToPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertBookToPDFController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.converters; -import io.github.pixee.security.Filenames; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.ResponseEntity; @@ -10,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java index 2cf79992..189eebdf 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.converters; -import io.github.pixee.security.Filenames; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.ResponseEntity; @@ -10,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java index c131d883..86a70472 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.converters; -import io.github.pixee.security.Filenames; import java.io.IOException; import java.net.URLConnection; @@ -19,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -57,7 +57,9 @@ public class ConvertImgPDFController { // returns bytes for image boolean singleImage = "single".equals(singleOrMultiple); byte[] result = null; - String filename = Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", ""); + String filename = + Filenames.toSimpleFileName(file.getOriginalFilename()) + .replaceFirst("[.][^.]+$", ""); try { result = PdfUtils.convertFromPdf( diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java index 39710ea0..050dd0fe 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.converters; -import io.github.pixee.security.Filenames; import java.util.List; import java.util.Map; @@ -20,6 +19,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertOfficeController.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertOfficeController.java index ce3d9fe9..c6740b5f 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertOfficeController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertOfficeController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.converters; -import io.github.pixee.security.Filenames; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -17,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -90,7 +90,8 @@ public class ConvertOfficeController { byte[] pdfByteArray = convertToPdf(inputFile); return WebResponseUtils.bytesToWebResponse( pdfByteArray, - Filenames.toSimpleFileName(inputFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + "_convertedToPDF.pdf"); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToBookController.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToBookController.java index 9711fc5e..c9171305 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToBookController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToBookController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.converters; -import io.github.pixee.security.Filenames; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -16,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -93,7 +93,8 @@ public class ConvertPDFToBookController { } String outputFilename = - Filenames.toSimpleFileName(fileInput.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + Filenames.toSimpleFileName(fileInput.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + "." + outputFormat; // Remove file extension and append .pdf diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToPDFA.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToPDFA.java index 5de8ee9a..41498413 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToPDFA.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToPDFA.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.converters; -import io.github.pixee.security.Filenames; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -13,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -64,7 +64,9 @@ public class ConvertPDFToPDFA { // Return the optimized PDF as a response String outputFilename = - Filenames.toSimpleFileName(inputFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_PDFA.pdf"; + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_PDFA.pdf"; return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/filters/FilterController.java b/src/main/java/stirling/software/SPDF/controller/api/filters/FilterController.java index 6326cf19..9beaf413 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/filters/FilterController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/filters/FilterController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.filters; -import io.github.pixee.security.Filenames; import java.io.IOException; import org.apache.pdfbox.Loader; @@ -14,6 +13,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/AutoRenameController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/AutoRenameController.java index 9000658c..2dc6c8a3 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/AutoRenameController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/AutoRenameController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.io.IOException; import java.util.ArrayList; import java.util.Comparator; @@ -19,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -134,7 +134,8 @@ public class AutoRenameController { return WebResponseUtils.pdfDocToWebResponse(document, header + ".pdf"); } else { logger.info("File has no good title to be found"); - return WebResponseUtils.pdfDocToWebResponse(document, Filenames.toSimpleFileName(file.getOriginalFilename())); + return WebResponseUtils.pdfDocToWebResponse( + document, Filenames.toSimpleFileName(file.getOriginalFilename())); } } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfController.java index da162d85..32f39c04 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.awt.image.DataBufferInt; @@ -32,6 +31,7 @@ import com.google.zxing.PlanarYUVLuminanceSource; import com.google.zxing.Result; import com.google.zxing.common.HybridBinarizer; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -98,7 +98,9 @@ public class AutoSplitPdfController { document.close(); Path zipFile = Files.createTempFile("split_documents", ".zip"); - String filename = Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", ""); + String filename = + Filenames.toSimpleFileName(file.getOriginalFilename()) + .replaceFirst("[.][^.]+$", ""); byte[] data; try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipFile))) { diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java index 34664a3b..a813ba79 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/BlankPageController.java @@ -1,25 +1,21 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.awt.image.BufferedImage; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; -import javax.imageio.ImageIO; - import org.apache.pdfbox.Loader; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageTree; import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.text.PDFTextStripper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ModelAttribute; @@ -28,12 +24,12 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import stirling.software.SPDF.model.api.misc.RemoveBlankPagesRequest; import stirling.software.SPDF.utils.PdfUtils; -import stirling.software.SPDF.utils.ProcessExecutor; import stirling.software.SPDF.utils.WebResponseUtils; @RestController @@ -41,6 +37,8 @@ import stirling.software.SPDF.utils.WebResponseUtils; @Tag(name = "Misc", description = "Miscellaneous APIs") public class BlankPageController { + private static final Logger logger = LoggerFactory.getLogger(BlankPageController.class); + @PostMapping(consumes = "multipart/form-data", value = "/remove-blanks") @Operation( summary = "Remove blank pages from a PDF file", @@ -63,63 +61,35 @@ public class BlankPageController { PDFRenderer pdfRenderer = new PDFRenderer(document); for (PDPage page : pages) { - System.out.println("checking page " + pageIndex); + logger.info("checking page " + pageIndex); textStripper.setStartPage(pageIndex + 1); textStripper.setEndPage(pageIndex + 1); String pageText = textStripper.getText(document); boolean hasText = !pageText.trim().isEmpty(); + + Boolean blank = false; if (hasText) { - pagesToKeepIndex.add(pageIndex); - System.out.println("page " + pageIndex + " has text"); + logger.info("page " + pageIndex + " has text, not blank"); + blank = false; } else { boolean hasImages = PdfUtils.hasImagesOnPage(page); if (hasImages) { - System.out.println("page " + pageIndex + " has image"); - - Path tempFile = Files.createTempFile("image_", ".png"); - + logger.info("page " + pageIndex + " has image, running blank detection"); // Render image and save as temp file - BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 300); - ImageIO.write(image, "png", tempFile.toFile()); - - List command = - new ArrayList<>( - Arrays.asList( - "python", - System.getProperty("user.dir") - + "/scripts/detect-blank-pages.py", - tempFile.toString(), - "--threshold", - String.valueOf(threshold), - "--white_percent", - String.valueOf(whitePercent))); - - Boolean blank = false; - // Run CLI command - try { - ProcessExecutor.getInstance(ProcessExecutor.Processes.PYTHON_OPENCV) - .runCommandWithOutputHandling(command); - } catch (IOException e) { - // From detect-blank-pages.py - // Return code 1: The image is considered blank. - // Return code 0: The image is not considered blank. - // Since the process returned with a failure code, it should be blank. - blank = true; - } - - if (blank) { - System.out.println("Skipping, Image was blank for page #" + pageIndex); - } else { - System.out.println( - "page " + pageIndex + " has image which is not blank"); - pagesToKeepIndex.add(pageIndex); - } + BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 30); + blank = isBlankImage(image, threshold, whitePercent, threshold); } } + + if (blank) { + logger.info("Skipping, Image was blank for page #" + pageIndex); + } else { + logger.info("page " + pageIndex + " has image which is not blank"); + pagesToKeepIndex.add(pageIndex); + } + pageIndex++; } - System.out.print("pagesToKeep=" + pagesToKeepIndex.size()); - // Remove pages not present in pagesToKeepIndex List pageIndices = IntStream.range(0, pages.getCount()).boxed().collect(Collectors.toList()); @@ -132,7 +102,8 @@ public class BlankPageController { return WebResponseUtils.pdfDocToWebResponse( document, - Filenames.toSimpleFileName(inputFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + "_blanksRemoved.pdf"); } catch (IOException e) { e.printStackTrace(); @@ -141,4 +112,30 @@ public class BlankPageController { if (document != null) document.close(); } } + + public static boolean isBlankImage( + BufferedImage image, int threshold, double whitePercent, int blurSize) { + if (image == null) { + logger.info("Error: Image is null"); + return false; + } + + // Convert to binary image based on the threshold + int whitePixels = 0; + int totalPixels = image.getWidth() * image.getHeight(); + + for (int i = 0; i < image.getHeight(); i++) { + for (int j = 0; j < image.getWidth(); j++) { + int color = image.getRGB(j, i) & 0xFF; + if (color >= 255 - threshold) { + whitePixels++; + } + } + } + + double whitePixelPercentage = (whitePixels / (double) totalPixels) * 100; + logger.info(String.format("Page has white pixel percent of %.2f%%", whitePixelPercentage)); + + return whitePixelPercentage >= whitePercent; + } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/CompressController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/CompressController.java index c1b4a996..82a2c72f 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/CompressController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/CompressController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; @@ -30,6 +29,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -265,7 +265,9 @@ public class CompressController { // Return the optimized PDF as a response String outputFilename = - Filenames.toSimpleFileName(inputFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_Optimized.pdf"; + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_Optimized.pdf"; return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java index cb9071d8..3931e2df 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; @@ -30,6 +29,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -67,7 +67,9 @@ public class ExtractImagesController { zos.setLevel(Deflater.BEST_COMPRESSION); int imageIndex = 1; - String filename = Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", ""); + String filename = + Filenames.toSimpleFileName(file.getOriginalFilename()) + .replaceFirst("[.][^.]+$", ""); int pageNum = 0; Set processedImages = new HashSet<>(); // Iterate over each page diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/FakeScanControllerWIP.java b/src/main/java/stirling/software/SPDF/controller/api/misc/FakeScanControllerWIP.java index 8fb4af43..789bf17e 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/FakeScanControllerWIP.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/FakeScanControllerWIP.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.awt.Color; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; @@ -34,6 +33,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Hidden; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -142,7 +142,9 @@ public class FakeScanControllerWIP { // Return the optimized PDF as a response String outputFilename = - Filenames.toSimpleFileName(inputFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_scanned.pdf"; + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_scanned.pdf"; return WebResponseUtils.boasToWebResponse(baos, outputFilename); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java index 8e4ae664..fec07380 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -19,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -165,6 +165,8 @@ public class MetadataController { document.setDocumentInformation(info); return WebResponseUtils.pdfDocToWebResponse( document, - Filenames.toSimpleFileName(pdfFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_metadata.pdf"); + Filenames.toSimpleFileName(pdfFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_metadata.pdf"); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java index 143ae1da..542e3c6d 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -25,6 +24,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -183,12 +183,16 @@ public class OCRController { // Return the OCR processed PDF as a response String outputFilename = - Filenames.toSimpleFileName(inputFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_OCR.pdf"; + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_OCR.pdf"; if (sidecar != null && sidecar) { // Create a zip file containing both the PDF and the text file String outputZipFilename = - Filenames.toSimpleFileName(inputFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_OCR.zip"; + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_OCR.zip"; Path tempZipFile = Files.createTempFile("output_", ".zip"); try (ZipOutputStream zipOut = diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/OverlayImageController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/OverlayImageController.java index 01da177d..c3fad457 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/OverlayImageController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/OverlayImageController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.io.IOException; import org.slf4j.Logger; @@ -13,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -45,7 +45,9 @@ public class OverlayImageController { return WebResponseUtils.bytesToWebResponse( result, - Filenames.toSimpleFileName(pdfFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_overlayed.pdf"); + Filenames.toSimpleFileName(pdfFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_overlayed.pdf"); } catch (IOException e) { logger.error("Failed to add image to PDF", e); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/PageNumbersController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/PageNumbersController.java index cd99e9d0..337a9f48 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/PageNumbersController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/PageNumbersController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; @@ -22,6 +21,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -146,7 +146,8 @@ public class PageNumbersController { return WebResponseUtils.bytesToWebResponse( baos.toByteArray(), - Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_numbersAdded.pdf", + Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + + "_numbersAdded.pdf", MediaType.APPLICATION_PDF); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/RepairController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/RepairController.java index caaaed5c..85d4560c 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/RepairController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/RepairController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -16,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -66,7 +66,9 @@ public class RepairController { // Return the optimized PDF as a response String outputFilename = - Filenames.toSimpleFileName(inputFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_repaired.pdf"; + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_repaired.pdf"; return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java b/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java index 0903a6e1..6accfb7a 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.nio.charset.StandardCharsets; import java.util.Map; @@ -17,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -55,7 +55,8 @@ public class ShowJavascript { script += "// File: " - + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + + Filenames.toSimpleFileName( + inputFile.getOriginalFilename()) + ", Script: " + name + "\n" diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java index 52e8d970..4779b077 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.misc; -import io.github.pixee.security.Filenames; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; @@ -8,6 +7,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.util.List; import javax.imageio.ImageIO; @@ -33,6 +33,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -52,6 +53,7 @@ public class StampController { public ResponseEntity addStamp(@ModelAttribute AddStampRequest request) throws IOException, Exception { MultipartFile pdfFile = request.getFileInput(); + List pageNumbers = request.getPageNumbersList(); String watermarkType = request.getStampType(); String watermarkText = request.getStampText(); MultipartFile watermarkImage = request.getStampImage(); @@ -87,49 +89,58 @@ public class StampController { // Load the input PDF PDDocument document = Loader.loadPDF(pdfFile.getBytes()); - for (PDPage page : document.getPages()) { - PDPageContentStream contentStream = - new PDPageContentStream( - document, page, PDPageContentStream.AppendMode.APPEND, true, true); + for (int pageIndex : pageNumbers) { + // Convert 1-based index to 0-based index required by + // document.getPages().get(index) + int zeroBasedIndex = pageIndex - 1; + // Check if the zeroBasedIndex is within the range of the document's pages + if (zeroBasedIndex >= 0 && zeroBasedIndex < document.getNumberOfPages()) { + PDPage page = document.getPage(zeroBasedIndex); + PDPageContentStream contentStream = + new PDPageContentStream( + document, page, PDPageContentStream.AppendMode.APPEND, true, true); - PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); - graphicsState.setNonStrokingAlphaConstant(opacity); - contentStream.setGraphicsStateParameters(graphicsState); + PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); + graphicsState.setNonStrokingAlphaConstant(opacity); + contentStream.setGraphicsStateParameters(graphicsState); - if ("text".equalsIgnoreCase(watermarkType)) { - addTextStamp( - contentStream, - watermarkText, - document, - page, - rotation, - position, - fontSize, - alphabet, - overrideX, - overrideY, - marginFactor, - customColor); - } else if ("image".equalsIgnoreCase(watermarkType)) { - addImageStamp( - contentStream, - watermarkImage, - document, - page, - rotation, - position, - fontSize, - overrideX, - overrideY, - marginFactor); + if ("text".equalsIgnoreCase(watermarkType)) { + addTextStamp( + contentStream, + watermarkText, + document, + page, + rotation, + position, + fontSize, + alphabet, + overrideX, + overrideY, + marginFactor, + customColor); + } else if ("image".equalsIgnoreCase(watermarkType)) { + addImageStamp( + contentStream, + watermarkImage, + document, + page, + rotation, + position, + fontSize, + overrideX, + overrideY, + marginFactor); + } + + contentStream.close(); } - - contentStream.close(); } return WebResponseUtils.pdfDocToWebResponse( document, - Filenames.toSimpleFileName(pdfFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_watermarked.pdf"); + Filenames.toSimpleFileName(pdfFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_stamped.pdf"); } private void addTextStamp( diff --git a/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java b/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java index c4e98bdb..c327cdd8 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java +++ b/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java @@ -1,7 +1,5 @@ package stirling.software.SPDF.controller.api.pipeline; -import io.github.pixee.security.Filenames; -import io.github.pixee.security.ZipSecurity; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -38,6 +36,9 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; +import io.github.pixee.security.ZipSecurity; + import jakarta.servlet.ServletContext; import stirling.software.SPDF.SPdfApplication; import stirling.software.SPDF.model.PipelineConfig; diff --git a/src/main/java/stirling/software/SPDF/controller/api/security/CertSignController.java b/src/main/java/stirling/software/SPDF/controller/api/security/CertSignController.java index 658f68f3..d066bd9a 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/security/CertSignController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/security/CertSignController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.security; -import io.github.pixee.security.Filenames; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -43,6 +42,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -124,7 +124,9 @@ public class CertSignController { ByteArrayOutputStream baos = new ByteArrayOutputStream(); sign(pdf.getBytes(), baos, createSignature, name, location, reason); return WebResponseUtils.boasToWebResponse( - baos, Filenames.toSimpleFileName(pdf.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_signed.pdf"); + baos, + Filenames.toSimpleFileName(pdf.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + + "_signed.pdf"); } private static void sign( diff --git a/src/main/java/stirling/software/SPDF/controller/api/security/PasswordController.java b/src/main/java/stirling/software/SPDF/controller/api/security/PasswordController.java index 328b753c..84c44933 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/security/PasswordController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/security/PasswordController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.security; -import io.github.pixee.security.Filenames; import java.io.IOException; import org.apache.pdfbox.Loader; @@ -16,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -44,7 +44,8 @@ public class PasswordController { document.setAllSecurityToBeRemoved(true); return WebResponseUtils.pdfDocToWebResponse( document, - Filenames.toSimpleFileName(fileInput.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + Filenames.toSimpleFileName(fileInput.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + "_password_removed.pdf"); } @@ -89,10 +90,13 @@ public class PasswordController { if ("".equals(ownerPassword) && "".equals(password)) return WebResponseUtils.pdfDocToWebResponse( document, - Filenames.toSimpleFileName(fileInput.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + Filenames.toSimpleFileName(fileInput.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + "_permissions.pdf"); return WebResponseUtils.pdfDocToWebResponse( document, - Filenames.toSimpleFileName(fileInput.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_passworded.pdf"); + Filenames.toSimpleFileName(fileInput.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_passworded.pdf"); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/security/RedactController.java b/src/main/java/stirling/software/SPDF/controller/api/security/RedactController.java index 82ff54dd..0f482647 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/security/RedactController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/security/RedactController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.security; -import io.github.pixee.security.Filenames; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; @@ -26,6 +25,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -105,7 +105,8 @@ public class RedactController { byte[] pdfContent = baos.toByteArray(); return WebResponseUtils.bytesToWebResponse( pdfContent, - Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_redacted.pdf"); + Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + + "_redacted.pdf"); } private void redactFoundText( diff --git a/src/main/java/stirling/software/SPDF/controller/api/security/SanitizeController.java b/src/main/java/stirling/software/SPDF/controller/api/security/SanitizeController.java index bbcfeeaa..6dfbf044 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/security/SanitizeController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/security/SanitizeController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.security; -import io.github.pixee.security.Filenames; import java.io.IOException; import org.apache.pdfbox.Loader; @@ -29,6 +28,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -77,7 +77,8 @@ public class SanitizeController { return WebResponseUtils.pdfDocToWebResponse( document, - Filenames.toSimpleFileName(inputFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + "_sanitized.pdf"); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java b/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java index 91a8a265..9506822c 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.controller.api.security; -import io.github.pixee.security.Filenames; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; @@ -32,6 +31,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -106,7 +106,9 @@ public class WatermarkController { return WebResponseUtils.pdfDocToWebResponse( document, - Filenames.toSimpleFileName(pdfFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + "_watermarked.pdf"); + Filenames.toSimpleFileName(pdfFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + "_watermarked.pdf"); } private void addTextWatermark( diff --git a/src/main/java/stirling/software/SPDF/model/api/misc/AddStampRequest.java b/src/main/java/stirling/software/SPDF/model/api/misc/AddStampRequest.java index 82296acc..f4c449b0 100644 --- a/src/main/java/stirling/software/SPDF/model/api/misc/AddStampRequest.java +++ b/src/main/java/stirling/software/SPDF/model/api/misc/AddStampRequest.java @@ -6,11 +6,11 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import lombok.EqualsAndHashCode; -import stirling.software.SPDF.model.api.PDFFile; +import stirling.software.SPDF.model.api.PDFWithPageNums; @Data @EqualsAndHashCode(callSuper = true) -public class AddStampRequest extends PDFFile { +public class AddStampRequest extends PDFWithPageNums { @Schema( description = "The stamp type (text or image)", diff --git a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java index 09ce430c..90f7dbc3 100644 --- a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java +++ b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.utils; -import io.github.pixee.security.ZipSecurity; import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -13,6 +12,8 @@ import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; +import io.github.pixee.security.ZipSecurity; + import stirling.software.SPDF.model.api.converters.HTMLToPdfRequest; import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult; @@ -145,7 +146,8 @@ public class FileToPdf { private static Path unzipAndGetMainHtml(byte[] fileBytes) throws IOException { Path tempDirectory = Files.createTempDirectory("unzipped_"); - try (ZipInputStream zipIn = ZipSecurity.createHardenedInputStream(new ByteArrayInputStream(fileBytes))) { + try (ZipInputStream zipIn = + ZipSecurity.createHardenedInputStream(new ByteArrayInputStream(fileBytes))) { ZipEntry entry = zipIn.getNextEntry(); while (entry != null) { Path filePath = tempDirectory.resolve(entry.getName()); diff --git a/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java b/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java index 1737b27e..4ef5964c 100644 --- a/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java @@ -1,13 +1,10 @@ package stirling.software.SPDF.utils; -import io.github.pixee.security.HostValidator; -import io.github.pixee.security.Urls; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; -import java.net.URL; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; @@ -15,10 +12,14 @@ import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.HostValidator; +import io.github.pixee.security.Urls; + public class GeneralUtils { public static File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException { @@ -59,7 +60,8 @@ public class GeneralUtils { public static boolean isValidURL(String urlStr) { try { - Urls.create(urlStr, Urls.HTTP_PROTOCOLS, HostValidator.DENY_COMMON_INFRASTRUCTURE_TARGETS); + Urls.create( + urlStr, Urls.HTTP_PROTOCOLS, HostValidator.DENY_COMMON_INFRASTRUCTURE_TARGETS); return true; } catch (MalformedURLException e) { return false; @@ -114,6 +116,13 @@ public class GeneralUtils { } public static List parsePageString(String pageOrder, int totalPages) { + if (pageOrder == null || pageOrder.isEmpty()) { + return Collections.singletonList(1); + } + if (pageOrder.matches("\\d+")) { + // Convert the single number string to an integer and return it in a list + return Collections.singletonList(Integer.parseInt(pageOrder)); + } return parsePageList(pageOrder.split(","), totalPages); } diff --git a/src/main/java/stirling/software/SPDF/utils/PDFToFile.java b/src/main/java/stirling/software/SPDF/utils/PDFToFile.java index 7afe6a49..43683be8 100644 --- a/src/main/java/stirling/software/SPDF/utils/PDFToFile.java +++ b/src/main/java/stirling/software/SPDF/utils/PDFToFile.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.utils; -import io.github.pixee.security.Filenames; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; @@ -21,6 +20,8 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; + import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult; public class PDFToFile { diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 748f8aad..77814340 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.utils; -import io.github.pixee.security.Filenames; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; @@ -38,6 +37,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; + public class PdfUtils { private static final Logger logger = LoggerFactory.getLogger(PdfUtils.class); diff --git a/src/main/java/stirling/software/SPDF/utils/ProcessExecutor.java b/src/main/java/stirling/software/SPDF/utils/ProcessExecutor.java index f75367a7..2c81dce5 100644 --- a/src/main/java/stirling/software/SPDF/utils/ProcessExecutor.java +++ b/src/main/java/stirling/software/SPDF/utils/ProcessExecutor.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.utils; -import io.github.pixee.security.BoundedLineReader; import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -17,6 +16,8 @@ import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import io.github.pixee.security.BoundedLineReader; + public class ProcessExecutor { private static final Logger logger = LoggerFactory.getLogger(ProcessExecutor.class); @@ -110,7 +111,10 @@ public class ProcessExecutor { process.getErrorStream(), StandardCharsets.UTF_8))) { String line; - while ((line = BoundedLineReader.readLine(errorReader, 5_000_000)) != null) { + while ((line = + BoundedLineReader.readLine( + errorReader, 5_000_000)) + != null) { errorLines.add(line); if (liveUpdates) logger.info(line); } @@ -131,7 +135,10 @@ public class ProcessExecutor { process.getInputStream(), StandardCharsets.UTF_8))) { String line; - while ((line = BoundedLineReader.readLine(outputReader, 5_000_000)) != null) { + while ((line = + BoundedLineReader.readLine( + outputReader, 5_000_000)) + != null) { outputLines.add(line); if (liveUpdates) logger.info(line); } diff --git a/src/main/java/stirling/software/SPDF/utils/WebResponseUtils.java b/src/main/java/stirling/software/SPDF/utils/WebResponseUtils.java index a85720e7..d148e65b 100644 --- a/src/main/java/stirling/software/SPDF/utils/WebResponseUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/WebResponseUtils.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.utils; -import io.github.pixee.security.Filenames; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URLEncoder; @@ -13,6 +12,8 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.Filenames; + public class WebResponseUtils { public static ResponseEntity boasToWebResponse( diff --git a/src/main/resources/static/js/game.js b/src/main/resources/static/js/game.js index ffad304b..9067cf97 100644 --- a/src/main/resources/static/js/game.js +++ b/src/main/resources/static/js/game.js @@ -14,13 +14,16 @@ function initializeGame() { const highScoreElement = document.getElementById('high-score'); let pdfSize = gameContainer.clientWidth * 0.0625; // 5% of container width - let projectileWidth = gameContainer.clientWidth * 0.00625; // 0.5% of container width + let projectileWidth = gameContainer.clientWidth * 0.00625;// 0.00625; // 0.5% of container width let projectileHeight = gameContainer.clientHeight * 0.01667; // 1% of container height let paused = false; + const fireRate = 200; // Time between shots in milliseconds let lastProjectileTime = 0; let lives = 3; + + let highScore = localStorage.getItem('highScore') ? parseInt(localStorage.getItem('highScore')) : 0; updateHighScore(); @@ -31,7 +34,7 @@ function initializeGame() { const projectiles = []; let score = 0; let level = 1; - let pdfSpeed = 1; + let pdfSpeed = 0.5; let gameOver = false; function handleKeys() { @@ -119,7 +122,7 @@ function initializeGame() { for (let pdfIndex = 0; pdfIndex < pdfs.length; pdfIndex++) { const pdf = pdfs[pdfIndex]; - const pdfY = parseInt(pdf.style.top) + pdfSpeed; + const pdfY = parseFloat(pdf.style.top) + pdfSpeed; if (pdfY + 50 > gameContainer.clientHeight) { gameContainer.removeChild(pdf); pdfs.splice(pdfIndex, 1); @@ -218,7 +221,7 @@ function initializeGame() { if (newLevel > level) { level = newLevel; levelElement.textContent = 'Level: ' + level; - pdfSpeed += 1; + pdfSpeed += 0.2; } } @@ -249,6 +252,10 @@ function initializeGame() { let spawnPdfTimeout; + const BASE_SPAWN_INTERVAL_MS = 1250; // milliseconds before a new enemy spawns + const LEVEL_INCREASE_FACTOR_MS = 0; // milliseconds to decrease the spawn interval per level + const MAX_SPAWN_RATE_REDUCTION_MS = 800; // Max milliseconds from the base spawn interval + function spawnPdfInterval() { console.log("spawnPdfInterval"); if (gameOver || paused) { @@ -258,7 +265,9 @@ function initializeGame() { } console.log("spawnPdfInterval 3"); spawnPdf(); - spawnPdfTimeout = setTimeout(spawnPdfInterval, 1000 - level * 50); + let spawnRateReduction = Math.min(level * LEVEL_INCREASE_FACTOR_MS, MAX_SPAWN_RATE_REDUCTION_MS); + let spawnRate = BASE_SPAWN_INTERVAL_MS - spawnRateReduction; + spawnPdfTimeout = setTimeout(spawnPdfInterval, spawnRate); } updatePlayerPosition(); From 15ad46fe1cb5cb14f83d4a9624db6f224550db9c Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Fri, 9 Feb 2024 23:24:25 +0000 Subject: [PATCH 2/8] book htmk --- .../SPDF/config/PostStartupProcesses.java | 27 +---- .../api/converters/ConvertPDFToOffice.java | 20 +++- .../software/SPDF/utils/FileToPdf.java | 104 ++++-------------- .../templates/convert/html-to-pdf.html | 59 +--------- .../templates/convert/pdf-to-text.html | 1 + 5 files changed, 45 insertions(+), 166 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/config/PostStartupProcesses.java b/src/main/java/stirling/software/SPDF/config/PostStartupProcesses.java index 862e5f9e..e7b684ff 100644 --- a/src/main/java/stirling/software/SPDF/config/PostStartupProcesses.java +++ b/src/main/java/stirling/software/SPDF/config/PostStartupProcesses.java @@ -41,32 +41,13 @@ public class PostStartupProcesses { // Checking for DOCKER_INSTALL_BOOK_FORMATS environment variable if (bookFormatsInstalled) { List tmpList = new ArrayList<>(); - // Set up the timezone configuration commands - tmpList.addAll( - Arrays.asList( - "sh", - "-c", - "echo 'tzdata tzdata/Areas select Europe' | debconf-set-selections; " - + "echo 'tzdata tzdata/Zones/Europe select Berlin' | debconf-set-selections")); - commands.add(tmpList); - // Install calibre with DEBIAN_FRONTEND set to noninteractive tmpList = new ArrayList<>(); - tmpList.addAll( - Arrays.asList( - "sh", - "-c", - "DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends calibre")); + tmpList.addAll(Arrays.asList("whoami")); commands.add(tmpList); - } - // Checking for DOCKER_INSTALL_HTML_FORMATS environment variable - if (htmlFormatsInstalled) { - List tmpList = new ArrayList<>(); - // Add -y flag for automatic yes to prompts and --no-install-recommends to reduce size - tmpList.addAll( - Arrays.asList( - "apt-get", "install", "wkhtmltopdf", "-y", "--no-install-recommends")); + tmpList = new ArrayList<>(); + tmpList.addAll(Arrays.asList("id")); commands.add(tmpList); } @@ -74,8 +55,6 @@ public class PostStartupProcesses { // Run the command if (runningInDocker) { List tmpList = new ArrayList<>(); - tmpList.addAll(Arrays.asList("apt-get", "update")); - commands.add(0, tmpList); for (List list : commands) { ProcessExecutorResult returnCode = diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToOffice.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToOffice.java index 74b292b5..94e33982 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToOffice.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToOffice.java @@ -2,13 +2,18 @@ package stirling.software.SPDF.controller.api.converters; import java.io.IOException; +import org.apache.pdfbox.Loader; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import org.apache.pdfbox.text.PDFTextStripper; +import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -17,6 +22,7 @@ import stirling.software.SPDF.model.api.converters.PdfToPresentationRequest; import stirling.software.SPDF.model.api.converters.PdfToTextOrRTFRequest; import stirling.software.SPDF.model.api.converters.PdfToWordRequest; import stirling.software.SPDF.utils.PDFToFile; +import stirling.software.SPDF.utils.WebResponseUtils; @RestController @RequestMapping("/api/v1/convert") @@ -59,9 +65,17 @@ public class ConvertPDFToOffice { throws IOException, InterruptedException { MultipartFile inputFile = request.getFileInput(); String outputFormat = request.getOutputFormat(); - - PDFToFile pdfToFile = new PDFToFile(); - return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "writer_pdf_import"); + if ("txt".equals(request.getOutputFormat())) { + try (PDDocument document = Loader.loadPDF(inputFile.getBytes())) { + PDFTextStripper stripper = new PDFTextStripper(); + String text = stripper.getText(document); + return WebResponseUtils.bytesToWebResponse(text.getBytes(), Filenames.toSimpleFileName(inputFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") + + ".txt" , MediaType.TEXT_PLAIN); + } + } else { + PDFToFile pdfToFile = new PDFToFile(); + return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "writer_pdf_import"); + } } @PostMapping(consumes = "multipart/form-data", value = "/pdf/word") diff --git a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java index 90f7dbc3..84f2596a 100644 --- a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java +++ b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java @@ -1,8 +1,9 @@ package stirling.software.SPDF.utils; import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -34,95 +35,40 @@ public class FileToPdf { tempInputFile = Files.createTempFile("input_", ".html"); Files.write(tempInputFile, fileBytes); } else { - tempInputFile = unzipAndGetMainHtml(fileBytes); + tempInputFile = Files.createTempFile("input_", ".zip"); + Files.write(tempInputFile, fileBytes); } List command = new ArrayList<>(); if (!htmlFormatsInstalled) { command.add("weasyprint"); - } else { - command.add("wkhtmltopdf"); - command.add("--enable-local-file-access"); - command.add("--load-error-handling"); - command.add("ignore"); - command.add("--load-media-error-handling"); - command.add("ignore"); - command.add("--zoom"); - command.add(String.valueOf(request.getZoom())); + command.add(tempInputFile.toString()); + command.add(tempOutputFile.toString()); + + } else { + command.add("ebook-convert"); + command.add(tempInputFile.toString()); + command.add(tempOutputFile.toString()); + command.add("--paper-size"); + command.add("a4"); - // if custom zoom add zoom style direct to html - // https://github.com/wkhtmltopdf/wkhtmltopdf/issues/4900 if (request.getZoom() != 1.0) { - String htmlContent = new String(Files.readAllBytes(tempInputFile)); - - String zoomStyle = ""; - // Check for tag, add style tag to associated tag - if (htmlContent.contains("")) { - htmlContent = htmlContent.replace("", "" + zoomStyle); - } else if (htmlContent.contains("")) { - // If no tag, but tag exists - htmlContent = htmlContent.replace("", "" + zoomStyle); - } else { - // If neither nor tags exist - htmlContent = zoomStyle + htmlContent; + // Create a temporary CSS file + File tempCssFile = File.createTempFile("customStyle", ".css"); + try (FileWriter writer = new FileWriter(tempCssFile)) { + // Write the CSS rule to the file + writer.write("body { zoom: " + request.getZoom() + "; }"); } - // rewrite new html to file - Files.write(tempInputFile, htmlContent.getBytes(StandardCharsets.UTF_8)); - } - - if (request.getPageWidth() != null) { - command.add("--page-width"); - command.add(request.getPageWidth() + "cm"); - } - - if (request.getPageHeight() != null) { - command.add("--page-height"); - command.add(request.getPageHeight() + "cm"); - } - - if (request.getMarginTop() != null) { - command.add("--margin-top"); - command.add(request.getMarginTop() + "mm"); - } - - // Repeat similar pattern for marginBottom, marginLeft, marginRight - - if ("Yes".equalsIgnoreCase(request.getPrintBackground())) { - command.add("--background"); - } else { - command.add("--no-background"); - } - - if ("Yes".equalsIgnoreCase(request.getDefaultHeader())) { - command.add("--default-header"); - } - - if ("print".equalsIgnoreCase(request.getCssMediaType())) { - command.add("--print-media-type"); - } else if ("screen".equalsIgnoreCase(request.getCssMediaType())) { - command.add("--no-print-media-type"); + command.add("--extra-css"); + command.add(tempCssFile.getAbsolutePath()); } } - command.add(tempInputFile.toString()); - command.add(tempOutputFile.toString()); ProcessExecutorResult returnCode; - if (fileName.endsWith(".zip")) { - if (htmlFormatsInstalled) { - // command.add(1, "--allow"); - // command.add(2, tempInputFile.getParent().toString()); - } - returnCode = - ProcessExecutor.getInstance(ProcessExecutor.Processes.WEASYPRINT) - .runCommandWithOutputHandling( - command, tempInputFile.getParent().toFile()); - } else { - - returnCode = - ProcessExecutor.getInstance(ProcessExecutor.Processes.WEASYPRINT) - .runCommandWithOutputHandling(command); - } + returnCode = + ProcessExecutor.getInstance(ProcessExecutor.Processes.WEASYPRINT) + .runCommandWithOutputHandling(command); pdfBytes = Files.readAllBytes(tempOutputFile); } catch (IOException e) { @@ -135,10 +81,6 @@ public class FileToPdf { // Clean up temporary files Files.delete(tempOutputFile); Files.delete(tempInputFile); - - if (fileName.endsWith(".zip")) { - GeneralUtils.deleteDirectory(tempInputFile.getParent()); - } } return pdfBytes; diff --git a/src/main/resources/templates/convert/html-to-pdf.html b/src/main/resources/templates/convert/html-to-pdf.html index cbb22b99..534bed5f 100644 --- a/src/main/resources/templates/convert/html-to-pdf.html +++ b/src/main/resources/templates/convert/html-to-pdf.html @@ -19,64 +19,7 @@ - -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- - -
- - -
- - +
diff --git a/src/main/resources/templates/convert/pdf-to-text.html b/src/main/resources/templates/convert/pdf-to-text.html index 75b71d11..de649025 100644 --- a/src/main/resources/templates/convert/pdf-to-text.html +++ b/src/main/resources/templates/convert/pdf-to-text.html @@ -19,6 +19,7 @@
From 22343e507d061ee02d86e6d917bc49cd9451cbfd Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Fri, 9 Feb 2024 23:45:18 +0000 Subject: [PATCH 3/8] fixes --- Dockerfile | 33 ++++++++- Dockerfile-lite | 17 +++-- DockerfileBase | 28 -------- .../api/converters/ConvertPDFToOffice.java | 20 +++--- .../controller/api/misc/ShowJavascript.java | 4 +- .../controller/api/misc/StampController.java | 68 +++++++++---------- 6 files changed, 86 insertions(+), 84 deletions(-) delete mode 100644 DockerfileBase diff --git a/Dockerfile b/Dockerfile index 63bbc6b0..0479b5f9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,32 @@ -# Use the base image -FROM frooodle/stirling-pdf-base:version8 +# Main stage +FROM alpine:3.19.0 + +# JDK for app +RUN echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/main" | tee -a /etc/apk/repositories && \ + echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/community" | tee -a /etc/apk/repositories && \ + echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/testing" | tee -a /etc/apk/repositories && \ + apk add --no-cache \ + ca-certificates \ + tzdata \ + tini \ + bash \ + curl \ + openjdk17-jre \ +# Doc conversion + libreoffice@testing \ +# OCR MY PDF (unpaper for descew and other advanced featues) + ocrmypdf \ + tesseract-ocr-data-eng \ +# CV + py3-opencv \ +# python3/pip + python3 && \ + wget https://bootstrap.pypa.io/get-pip.py -qO - | python3 - --break-system-packages --no-cache-dir --upgrade && \ +# uno unoconv and HTML + pip install --break-system-packages --no-cache-dir --upgrade unoconv WeasyPrint && \ + mv /usr/share/tessdata /usr/share/tessdata-original + + ARG VERSION_TAG @@ -24,7 +51,7 @@ COPY build/libs/*.jar app.jar ## useradd -u $PUID -g stirlingpdfgroup -s /bin/sh stirlingpdfuser && \ ## mkdir -p $HOME && chown stirlingpdfuser:stirlingpdfgroup $HOME && \ # Set up necessary directories and permissions -RUN mkdir /configs /logs /customFiles /pipeline/watchedFolders /pipeline/finishedFolders && \ +RUN mkdir -p /configs /logs /customFiles /pipeline/watchedFolders /pipeline/finishedFolders && \ ##&& \ ## chown -R stirlingpdfuser:stirlingpdfgroup /scripts /usr/share/fonts/opentype/noto /usr/share/tesseract-ocr /configs /customFiles && \ ## chown -R stirlingpdfuser:stirlingpdfgroup /usr/share/tesseract-ocr-original && \ diff --git a/Dockerfile-lite b/Dockerfile-lite index 979d97de..5de060e6 100644 --- a/Dockerfile-lite +++ b/Dockerfile-lite @@ -20,17 +20,19 @@ COPY src/main/resources/static/fonts/*.ttf /usr/share/fonts/opentype/noto COPY src/main/resources/static/fonts/*.otf /usr/share/fonts/opentype/noto COPY build/libs/*.jar app.jar -RUN apk add --no-cache \ +RUN echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/main" | tee -a /etc/apk/repositories && \ + echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/community" | tee -a /etc/apk/repositories && \ + echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/testing" | tee -a /etc/apk/repositories && \ + apk add --no-cache \ ca-certificates \ tzdata \ tini \ bash \ curl \ - openjdk17-jre && \ + openjdk17-jre \ # Doc conversion - apk add --no-cache libreoffice --repository http://dl-cdn.alpinelinux.org/alpine/edge/community && \ + libreoffice@testing \ # python and pip - apk add --no-cache \ python3 && \ wget https://bootstrap.pypa.io/get-pip.py -qO - | python3 - --break-system-packages --no-cache-dir --upgrade && \ # uno unoconv and HTML @@ -40,14 +42,11 @@ RUN apk add --no-cache \ # useradd -u $PUID -g stirlingpdfgroup -s /bin/sh stirlingpdfuser && \ # mkdir -p $HOME && chown stirlingpdfuser:stirlingpdfgroup $HOME # Set up necessary directories and permissions - mkdir /configs /logs /customFiles /pipeline/watchedFolders /pipeline/finishedFolders && \ + mkdir -p /configs /logs /customFiles /pipeline/watchedFolders /pipeline/finishedFolders && \ # chown -R stirlingpdfuser:stirlingpdfgroup /usr/share/fonts/opentype/noto /configs /customFiles # Set font cache and permissions fc-cache -f -v && \ - chmod +x /scripts/*.sh && \ - echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/main" | tee -a /etc/apk/repositories && \ - echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/community" | tee -a /etc/apk/repositories && \ - echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/testing" | tee -a /etc/apk/repositories + chmod +x /scripts/*.sh # chown stirlingpdfuser:stirlingpdfgroup /app.jar # Set environment variables diff --git a/DockerfileBase b/DockerfileBase deleted file mode 100644 index 44e1a582..00000000 --- a/DockerfileBase +++ /dev/null @@ -1,28 +0,0 @@ -# Main stage -FROM alpine:3.19.0 - -# JDK for app -RUN echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/main" | tee -a /etc/apk/repositories && \ - echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/community" | tee -a /etc/apk/repositories && \ - echo "@testing https://dl-cdn.alpinelinux.org/alpine/edge/testing" | tee -a /etc/apk/repositories && \ - apk add --no-cache \ - ca-certificates \ - tzdata \ - tini \ - bash \ - curl \ - openjdk17-jre \ -# Doc conversion - libreoffice@testing \ -# OCR MY PDF (unpaper for descew and other advanced featues) - ocrmypdf \ - tesseract-ocr-data-eng \ -# CV - py3-opencv \ -# python3/pip - python3 && \ - wget https://bootstrap.pypa.io/get-pip.py -qO - | python3 - --break-system-packages --no-cache-dir --upgrade && \ -# uno unoconv and HTML - pip install --break-system-packages --no-cache-dir --upgrade unoconv WeasyPrint && \ - mv /usr/share/tessdata /usr/share/tessdata-original - diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToOffice.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToOffice.java index 94e33982..798c5f44 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToOffice.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToOffice.java @@ -4,6 +4,7 @@ import java.io.IOException; import org.apache.pdfbox.Loader; import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.text.PDFTextStripper; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ModelAttribute; @@ -11,7 +12,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import org.apache.pdfbox.text.PDFTextStripper; import io.github.pixee.security.Filenames; import io.swagger.v3.oas.annotations.Operation; @@ -65,16 +65,20 @@ public class ConvertPDFToOffice { throws IOException, InterruptedException { MultipartFile inputFile = request.getFileInput(); String outputFormat = request.getOutputFormat(); - if ("txt".equals(request.getOutputFormat())) { - try (PDDocument document = Loader.loadPDF(inputFile.getBytes())) { + if ("txt".equals(request.getOutputFormat())) { + try (PDDocument document = Loader.loadPDF(inputFile.getBytes())) { PDFTextStripper stripper = new PDFTextStripper(); - String text = stripper.getText(document); - return WebResponseUtils.bytesToWebResponse(text.getBytes(), Filenames.toSimpleFileName(inputFile.getOriginalFilename()).replaceFirst("[.][^.]+$", "") - + ".txt" , MediaType.TEXT_PLAIN); + String text = stripper.getText(document); + return WebResponseUtils.bytesToWebResponse( + text.getBytes(), + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + .replaceFirst("[.][^.]+$", "") + + ".txt", + MediaType.TEXT_PLAIN); } } else { - PDFToFile pdfToFile = new PDFToFile(); - return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "writer_pdf_import"); + PDFToFile pdfToFile = new PDFToFile(); + return pdfToFile.processPdfToOfficeFormat(inputFile, outputFormat, "writer_pdf_import"); } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java b/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java index e1355c30..a1f4f59c 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java @@ -68,7 +68,9 @@ public class ShowJavascript { if (script.isEmpty()) { script = - "PDF '" + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + "' does not contain Javascript"; + "PDF '" + + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + + "' does not contain Javascript"; } return WebResponseUtils.bytesToWebResponse( diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java index cfde30c1..9e53292e 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java @@ -97,46 +97,44 @@ public class StampController { PDRectangle pageSize = page.getMediaBox(); float margin = marginFactor * (pageSize.getWidth() + pageSize.getHeight()) / 2; - PDPageContentStream contentStream = new PDPageContentStream( document, page, PDPageContentStream.AppendMode.APPEND, true, true); + PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); + graphicsState.setNonStrokingAlphaConstant(opacity); + contentStream.setGraphicsStateParameters(graphicsState); - PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); - graphicsState.setNonStrokingAlphaConstant(opacity); - contentStream.setGraphicsStateParameters(graphicsState); - - if ("text".equalsIgnoreCase(watermarkType)) { - addTextStamp( - contentStream, - watermarkText, - document, - page, - rotation, - position, - fontSize, - alphabet, - overrideX, - overrideY, - margin, - customColor); - } else if ("image".equalsIgnoreCase(watermarkType)) { - addImageStamp( - contentStream, - watermarkImage, - document, - page, - rotation, - position, - fontSize, - overrideX, - overrideY, - margin); - } - - contentStream.close(); - } + if ("text".equalsIgnoreCase(watermarkType)) { + addTextStamp( + contentStream, + watermarkText, + document, + page, + rotation, + position, + fontSize, + alphabet, + overrideX, + overrideY, + margin, + customColor); + } else if ("image".equalsIgnoreCase(watermarkType)) { + addImageStamp( + contentStream, + watermarkImage, + document, + page, + rotation, + position, + fontSize, + overrideX, + overrideY, + margin); + } + + contentStream.close(); + } } return WebResponseUtils.pdfDocToWebResponse( document, From 96e399a617122e64a1fcd23e9957d6c4038aa744 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sat, 10 Feb 2024 00:00:07 +0000 Subject: [PATCH 4/8] changing html and book labels --- .../software/SPDF/config/AppConfig.java | 11 ++---- .../SPDF/config/EndpointConfiguration.java | 10 +++--- .../SPDF/config/PostStartupProcesses.java | 16 +++------ .../ConvertBookToPDFController.java | 10 +++--- .../ConvertPDFToBookController.java | 10 +++--- .../web/ConverterWebController.java | 4 +-- .../SPDF/model/ApplicationProperties.java | 25 ++++--------- .../api/converters/HTMLToPdfRequest.java | 35 ------------------- src/main/resources/settings.yml.template | 3 +- 9 files changed, 32 insertions(+), 92 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/config/AppConfig.java b/src/main/java/stirling/software/SPDF/config/AppConfig.java index c1a646ad..65e98b7f 100644 --- a/src/main/java/stirling/software/SPDF/config/AppConfig.java +++ b/src/main/java/stirling/software/SPDF/config/AppConfig.java @@ -77,16 +77,11 @@ public class AppConfig { return Files.exists(Paths.get("/.dockerenv")); } - @Bean(name = "bookFormatsInstalled") - public boolean bookFormatsInstalled() { - return applicationProperties.getSystem().getCustomApplications().isInstallBookFormats(); - } - - @Bean(name = "htmlFormatsInstalled") - public boolean htmlFormatsInstalled() { + @Bean(name = "bookAndHtmlFormatsInstalled") + public boolean bookAndHtmlFormatsInstalled() { return applicationProperties .getSystem() .getCustomApplications() - .isInstallAdvancedHtmlToPDF(); + .isInstallBookAndHtmlFormats(); } } diff --git a/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java b/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java index f1e328f9..f82c189f 100644 --- a/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java +++ b/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java @@ -16,7 +16,7 @@ import org.springframework.stereotype.Service; import stirling.software.SPDF.model.ApplicationProperties; @Service -@DependsOn({"bookFormatsInstalled"}) +@DependsOn({"bookAndHtmlFormatsInstalled"}) public class EndpointConfiguration { private static final Logger logger = LoggerFactory.getLogger(EndpointConfiguration.class); private Map endpointStatuses = new ConcurrentHashMap<>(); @@ -24,14 +24,14 @@ public class EndpointConfiguration { private final ApplicationProperties applicationProperties; - private boolean bookFormatsInstalled; + private boolean bookAndHtmlFormatsInstalled; @Autowired public EndpointConfiguration( ApplicationProperties applicationProperties, - @Qualifier("bookFormatsInstalled") boolean bookFormatsInstalled) { + @Qualifier("bookAndHtmlFormatsInstalled") boolean bookAndHtmlFormatsInstalled) { this.applicationProperties = applicationProperties; - this.bookFormatsInstalled = bookFormatsInstalled; + this.bookAndHtmlFormatsInstalled = bookAndHtmlFormatsInstalled; init(); processEnvironmentConfigs(); } @@ -229,7 +229,7 @@ public class EndpointConfiguration { private void processEnvironmentConfigs() { List endpointsToRemove = applicationProperties.getEndpoints().getToRemove(); List groupsToRemove = applicationProperties.getEndpoints().getGroupsToRemove(); - if (!bookFormatsInstalled) { + if (!bookAndHtmlFormatsInstalled) { groupsToRemove.add("Calibre"); } if (endpointsToRemove != null) { diff --git a/src/main/java/stirling/software/SPDF/config/PostStartupProcesses.java b/src/main/java/stirling/software/SPDF/config/PostStartupProcesses.java index e7b684ff..2041cf95 100644 --- a/src/main/java/stirling/software/SPDF/config/PostStartupProcesses.java +++ b/src/main/java/stirling/software/SPDF/config/PostStartupProcesses.java @@ -26,12 +26,8 @@ public class PostStartupProcesses { private boolean runningInDocker; @Autowired - @Qualifier("bookFormatsInstalled") - private boolean bookFormatsInstalled; - - @Autowired - @Qualifier("htmlFormatsInstalled") - private boolean htmlFormatsInstalled; + @Qualifier("bookAndHtmlFormatsInstalled") + private boolean bookAndHtmlFormatsInstalled; private static final Logger logger = LoggerFactory.getLogger(PostStartupProcesses.class); @@ -39,15 +35,11 @@ public class PostStartupProcesses { public void runInstallCommandBasedOnEnvironment() throws IOException, InterruptedException { List> commands = new ArrayList<>(); // Checking for DOCKER_INSTALL_BOOK_FORMATS environment variable - if (bookFormatsInstalled) { + if (bookAndHtmlFormatsInstalled) { List tmpList = new ArrayList<>(); tmpList = new ArrayList<>(); - tmpList.addAll(Arrays.asList("whoami")); - commands.add(tmpList); - - tmpList = new ArrayList<>(); - tmpList.addAll(Arrays.asList("id")); + tmpList.addAll(Arrays.asList("apk add --no-cache calibre")); commands.add(tmpList); } diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertBookToPDFController.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertBookToPDFController.java index 3cbc783c..05784a15 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertBookToPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertBookToPDFController.java @@ -23,21 +23,21 @@ import stirling.software.SPDF.utils.WebResponseUtils; public class ConvertBookToPDFController { @Autowired - @Qualifier("bookFormatsInstalled") - private boolean bookFormatsInstalled; + @Qualifier("bookAndHtmlFormatsInstalled") + private boolean bookAndHtmlFormatsInstalled; @PostMapping(consumes = "multipart/form-data", value = "/book/pdf") @Operation( summary = "Convert a BOOK/comic (*.epub | *.mobi | *.azw3 | *.fb2 | *.txt | *.docx) to PDF", description = - "(Requires bookFormatsInstalled flag and Calibre installed) This endpoint takes an BOOK/comic (*.epub | *.mobi | *.azw3 | *.fb2 | *.txt | *.docx) input and converts it to PDF format.") + "(Requires bookAndHtmlFormatsInstalled flag and Calibre installed) This endpoint takes an BOOK/comic (*.epub | *.mobi | *.azw3 | *.fb2 | *.txt | *.docx) input and converts it to PDF format.") public ResponseEntity HtmlToPdf(@ModelAttribute GeneralFile request) throws Exception { MultipartFile fileInput = request.getFileInput(); - if (!bookFormatsInstalled) { + if (!bookAndHtmlFormatsInstalled) { throw new IllegalArgumentException( - "bookFormatsInstalled flag is False, this functionality is not avaiable"); + "bookAndHtmlFormatsInstalled flag is False, this functionality is not avaiable"); } if (fileInput == null) { diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToBookController.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToBookController.java index c9171305..28793d48 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToBookController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPDFToBookController.java @@ -30,22 +30,22 @@ import stirling.software.SPDF.utils.WebResponseUtils; public class ConvertPDFToBookController { @Autowired - @Qualifier("bookFormatsInstalled") - private boolean bookFormatsInstalled; + @Qualifier("bookAndHtmlFormatsInstalled") + private boolean bookAndHtmlFormatsInstalled; @PostMapping(consumes = "multipart/form-data", value = "/pdf/book") @Operation( summary = "Convert a PDF to a Book/comic (*.epub | *.mobi | *.azw3 | *.fb2 | *.txt | *.docx .. (others to include by chatgpt) to PDF", description = - "(Requires bookFormatsInstalled flag and Calibre installed) This endpoint Convert a PDF to a Book/comic (*.epub | *.mobi | *.azw3 | *.fb2 | *.txt | *.docx .. (others to include by chatgpt) to PDF") + "(Requires bookAndHtmlFormatsInstalled flag and Calibre installed) This endpoint Convert a PDF to a Book/comic (*.epub | *.mobi | *.azw3 | *.fb2 | *.txt | *.docx .. (others to include by chatgpt) to PDF") public ResponseEntity HtmlToPdf(@ModelAttribute PdfToBookRequest request) throws Exception { MultipartFile fileInput = request.getFileInput(); - if (!bookFormatsInstalled) { + if (!bookAndHtmlFormatsInstalled) { throw new IllegalArgumentException( - "bookFormatsInstalled flag is False, this functionality is not avaiable"); + "bookAndHtmlFormatsInstalled flag is False, this functionality is not avaiable"); } if (fileInput == null) { diff --git a/src/main/java/stirling/software/SPDF/controller/web/ConverterWebController.java b/src/main/java/stirling/software/SPDF/controller/web/ConverterWebController.java index 55ebcb91..23270df5 100644 --- a/src/main/java/stirling/software/SPDF/controller/web/ConverterWebController.java +++ b/src/main/java/stirling/software/SPDF/controller/web/ConverterWebController.java @@ -13,7 +13,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; @Tag(name = "Convert", description = "Convert APIs") public class ConverterWebController { - @ConditionalOnExpression("#{bookFormatsInstalled}") + @ConditionalOnExpression("#{bookAndHtmlFormatsInstalled}") @GetMapping("/book-to-pdf") @Hidden public String convertBookToPdfForm(Model model) { @@ -21,7 +21,7 @@ public class ConverterWebController { return "convert/book-to-pdf"; } - @ConditionalOnExpression("#{bookFormatsInstalled}") + @ConditionalOnExpression("#{bookAndHtmlFormatsInstalled}") @GetMapping("/pdf-to-book") @Hidden public String convertPdfToBookForm(Model model) { diff --git a/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java b/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java index 3258d8b1..34c60bbe 100644 --- a/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java +++ b/src/main/java/stirling/software/SPDF/model/ApplicationProperties.java @@ -290,31 +290,20 @@ public class ApplicationProperties { } public static class CustomApplications { - private boolean installBookFormats; - private boolean installAdvancedHtmlToPDF; + private boolean installBookAndHtmlFormats; - public boolean isInstallBookFormats() { - return installBookFormats; + public boolean isInstallBookAndHtmlFormats() { + return installBookAndHtmlFormats; } - public void setInstallBookFormats(boolean installBookFormats) { - this.installBookFormats = installBookFormats; - } - - public boolean isInstallAdvancedHtmlToPDF() { - return installAdvancedHtmlToPDF; - } - - public void setInstallAdvancedHtmlToPDF(boolean installAdvancedHtmlToPDF) { - this.installAdvancedHtmlToPDF = installAdvancedHtmlToPDF; + public void setInstallBookAndHtmlFormats(boolean installBookAndHtmlFormats) { + this.installBookAndHtmlFormats = installBookAndHtmlFormats; } @Override public String toString() { - return "CustomApplications [installBookFormats=" - + installBookFormats - + ", installAdvancedHtmlToPDF=" - + installAdvancedHtmlToPDF + return "CustomApplications [installBookAndHtmlFormats=" + + installBookAndHtmlFormats + "]"; } } diff --git a/src/main/java/stirling/software/SPDF/model/api/converters/HTMLToPdfRequest.java b/src/main/java/stirling/software/SPDF/model/api/converters/HTMLToPdfRequest.java index c778c36f..5f5ae051 100644 --- a/src/main/java/stirling/software/SPDF/model/api/converters/HTMLToPdfRequest.java +++ b/src/main/java/stirling/software/SPDF/model/api/converters/HTMLToPdfRequest.java @@ -14,39 +14,4 @@ public class HTMLToPdfRequest extends PDFFile { description = "Zoom level for displaying the website. Default is '1'.", defaultValue = "1") private float zoom; - - @Schema(description = "Width of the page in centimeters.") - private Float pageWidth; - - @Schema(description = "Height of the page in centimeters.") - private Float pageHeight; - - @Schema(description = "Top margin of the page in millimeters.") - private Float marginTop; - - @Schema(description = "Bottom margin of the page in millimeters.") - private Float marginBottom; - - @Schema(description = "Left margin of the page in millimeters.") - private Float marginLeft; - - @Schema(description = "Right margin of the page in millimeters.") - private Float marginRight; - - @Schema( - description = "Enable or disable rendering of website background.", - allowableValues = {"Yes", "No"}) - private String printBackground; - - @Schema( - description = - "Enable or disable the default header. The default header includes the name of the page on the left and the page number on the right.", - allowableValues = {"Yes", "No"}) - private String defaultHeader; - - @Schema( - description = "Change the CSS media type of the page. Defaults to 'print'.", - allowableValues = {"none", "print", "screen"}, - defaultValue = "print") - private String cssMediaType; } diff --git a/src/main/resources/settings.yml.template b/src/main/resources/settings.yml.template index 00c5998e..368a6a43 100644 --- a/src/main/resources/settings.yml.template +++ b/src/main/resources/settings.yml.template @@ -14,8 +14,7 @@ system: googlevisibility: false # 'true' to allow Google visibility (via robots.txt), 'false' to disallow enableAlphaFunctionality: false # Set to enable functionality which might need more testing before it fully goes live (This feature might make no changes) customApplications: - installBookFormats: false # Installs Calibre for book format conversion (For non docker it must be manually downloaded but will need to be true to show in UI) - installAdvancedHtmlToPDF: false # DO NOT USE EXTERNALLY, NOT SAFE! Install wkHtmlToPDF (For non docker it must be manually downloaded but will need to be true to show in UI) + installBookAndHtmlFormats: false # Installs Calibre for book format conversion (For non docker it must be manually downloaded but will need to be true to show in UI) #ui: # appName: exampleAppName # Application's visible name From 68c094166603bd0a5c54ff3706fc4bb2ca68bdf9 Mon Sep 17 00:00:00 2001 From: "pixeebot[bot]" <23113631+pixeebot@users.noreply.github.com> Date: Sat, 10 Feb 2024 00:08:18 +0000 Subject: [PATCH 5/8] Sanitized user-provided file names in HTTP multipart uploads --- .../software/SPDF/controller/api/misc/ShowJavascript.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java b/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java index a1f4f59c..0a93bf1d 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/ShowJavascript.java @@ -75,7 +75,7 @@ public class ShowJavascript { return WebResponseUtils.bytesToWebResponse( script.getBytes(StandardCharsets.UTF_8), - inputFile.getOriginalFilename() + ".js"); + Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + ".js"); } } } From 23672cd18d968fac55a9ccb4b1fb63a4cc0d9697 Mon Sep 17 00:00:00 2001 From: "pixeebot[bot]" <23113631+pixeebot@users.noreply.github.com> Date: Sat, 10 Feb 2024 00:08:18 +0000 Subject: [PATCH 6/8] Modernize and secure temp file creation --- src/main/java/stirling/software/SPDF/utils/FileToPdf.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java index 84f2596a..da408a23 100644 --- a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java +++ b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java @@ -54,7 +54,7 @@ public class FileToPdf { if (request.getZoom() != 1.0) { // Create a temporary CSS file - File tempCssFile = File.createTempFile("customStyle", ".css"); + File tempCssFile = Files.createTempFile("customStyle", ".css").toFile(); try (FileWriter writer = new FileWriter(tempCssFile)) { // Write the CSS rule to the file writer.write("body { zoom: " + request.getZoom() + "; }"); From 5a52e3d6dde1a6f6d82a344edd5ed8fe3f09547d Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sat, 10 Feb 2024 00:08:54 +0000 Subject: [PATCH 7/8] other changes --- .../SPDF/controller/api/converters/ConvertHtmlToPDF.java | 6 +++--- .../SPDF/controller/api/converters/ConvertWebsiteToPDF.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java index 189eebdf..9945ad2b 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java @@ -23,8 +23,8 @@ import stirling.software.SPDF.utils.WebResponseUtils; public class ConvertHtmlToPDF { @Autowired - @Qualifier("htmlFormatsInstalled") - private boolean htmlFormatsInstalled; + @Qualifier("installBookAndHtmlFormats") + private boolean installBookAndHtmlFormats; @PostMapping(consumes = "multipart/form-data", value = "/html/pdf") @Operation( @@ -47,7 +47,7 @@ public class ConvertHtmlToPDF { } byte[] pdfBytes = FileToPdf.convertHtmlToPdf( - request, fileInput.getBytes(), originalFilename, htmlFormatsInstalled); + request, fileInput.getBytes(), originalFilename, installBookAndHtmlFormats); String outputFilename = originalFilename.replaceFirst("[.][^.]+$", "") diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPDF.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPDF.java index a6cd439b..7fb4eeec 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPDF.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPDF.java @@ -29,8 +29,8 @@ import stirling.software.SPDF.utils.WebResponseUtils; public class ConvertWebsiteToPDF { @Autowired - @Qualifier("htmlFormatsInstalled") - private boolean htmlFormatsInstalled; + @Qualifier("installBookAndHtmlFormats") + private boolean installBookAndHtmlFormats; @PostMapping(consumes = "multipart/form-data", value = "/url/pdf") @Operation( @@ -53,7 +53,7 @@ public class ConvertWebsiteToPDF { // Prepare the OCRmyPDF command List command = new ArrayList<>(); - if (!htmlFormatsInstalled) { + if (!installBookAndHtmlFormats) { command.add("weasyprint"); } else { command.add("wkhtmltopdf"); From 6f72096953e932022c1f3803db72733ffdd399ce Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sat, 10 Feb 2024 00:21:00 +0000 Subject: [PATCH 8/8] more fixes --- .../SPDF/controller/api/converters/ConvertHtmlToPDF.java | 9 ++++++--- .../controller/api/converters/ConvertMarkdownToPdf.java | 9 ++++++--- .../controller/api/converters/ConvertWebsiteToPDF.java | 6 +++--- src/main/resources/settings.yml.template | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java index 9945ad2b..c7cfc196 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertHtmlToPDF.java @@ -23,8 +23,8 @@ import stirling.software.SPDF.utils.WebResponseUtils; public class ConvertHtmlToPDF { @Autowired - @Qualifier("installBookAndHtmlFormats") - private boolean installBookAndHtmlFormats; + @Qualifier("bookAndHtmlFormatsInstalled") + private boolean bookAndHtmlFormatsInstalled; @PostMapping(consumes = "multipart/form-data", value = "/html/pdf") @Operation( @@ -47,7 +47,10 @@ public class ConvertHtmlToPDF { } byte[] pdfBytes = FileToPdf.convertHtmlToPdf( - request, fileInput.getBytes(), originalFilename, installBookAndHtmlFormats); + request, + fileInput.getBytes(), + originalFilename, + bookAndHtmlFormatsInstalled); String outputFilename = originalFilename.replaceFirst("[.][^.]+$", "") diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java index 050dd0fe..9cefe1ff 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java @@ -33,8 +33,8 @@ import stirling.software.SPDF.utils.WebResponseUtils; public class ConvertMarkdownToPdf { @Autowired - @Qualifier("htmlFormatsInstalled") - private boolean htmlFormatsInstalled; + @Qualifier("bookAndHtmlFormatsInstalled") + private boolean bookAndHtmlFormatsInstalled; @PostMapping(consumes = "multipart/form-data", value = "/markdown/pdf") @Operation( @@ -69,7 +69,10 @@ public class ConvertMarkdownToPdf { byte[] pdfBytes = FileToPdf.convertHtmlToPdf( - null, htmlContent.getBytes(), "converted.html", htmlFormatsInstalled); + null, + htmlContent.getBytes(), + "converted.html", + bookAndHtmlFormatsInstalled); String outputFilename = originalFilename.replaceFirst("[.][^.]+$", "") diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPDF.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPDF.java index 7fb4eeec..c2d25973 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPDF.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPDF.java @@ -29,8 +29,8 @@ import stirling.software.SPDF.utils.WebResponseUtils; public class ConvertWebsiteToPDF { @Autowired - @Qualifier("installBookAndHtmlFormats") - private boolean installBookAndHtmlFormats; + @Qualifier("bookAndHtmlFormatsInstalled") + private boolean bookAndHtmlFormatsInstalled; @PostMapping(consumes = "multipart/form-data", value = "/url/pdf") @Operation( @@ -53,7 +53,7 @@ public class ConvertWebsiteToPDF { // Prepare the OCRmyPDF command List command = new ArrayList<>(); - if (!installBookAndHtmlFormats) { + if (!bookAndHtmlFormatsInstalled) { command.add("weasyprint"); } else { command.add("wkhtmltopdf"); diff --git a/src/main/resources/settings.yml.template b/src/main/resources/settings.yml.template index 368a6a43..52a84bf4 100644 --- a/src/main/resources/settings.yml.template +++ b/src/main/resources/settings.yml.template @@ -14,7 +14,7 @@ system: googlevisibility: false # 'true' to allow Google visibility (via robots.txt), 'false' to disallow enableAlphaFunctionality: false # Set to enable functionality which might need more testing before it fully goes live (This feature might make no changes) customApplications: - installBookAndHtmlFormats: false # Installs Calibre for book format conversion (For non docker it must be manually downloaded but will need to be true to show in UI) + bookAndHtmlFormatsInstalled: false # Installs Calibre for book format conversion (For non docker it must be manually downloaded but will need to be true to show in UI) #ui: # appName: exampleAppName # Application's visible name