diff --git a/src/main/java/stirling/software/SPDF/controller/api/other/RepairController.java b/src/main/java/stirling/software/SPDF/controller/api/other/RepairController.java new file mode 100644 index 00000000..28f87efa --- /dev/null +++ b/src/main/java/stirling/software/SPDF/controller/api/other/RepairController.java @@ -0,0 +1,59 @@ +package stirling.software.SPDF.controller.api.other; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import stirling.software.SPDF.utils.PdfUtils; +import stirling.software.SPDF.utils.ProcessExecutor; + +@RestController +public class RepairController { + + private static final Logger logger = LoggerFactory.getLogger(RepairController.class); + + @PostMapping(consumes = "multipart/form-data", value = "/repair") + public ResponseEntity repairPdf(@RequestPart(required = true, value = "fileInput") MultipartFile inputFile) + throws IOException, InterruptedException { + + // Save the uploaded file to a temporary location + Path tempInputFile = Files.createTempFile("input_", ".pdf"); + inputFile.transferTo(tempInputFile.toFile()); + + // Prepare the output file path + Path tempOutputFile = Files.createTempFile("output_", ".pdf"); + + List command = new ArrayList<>(); + command.add("gs"); + command.add("-o"); + command.add(tempOutputFile.toString()); + command.add("-sDEVICE=pdfwrite"); + command.add(tempInputFile.toString()); + + + int returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.GHOSTSCRIPT).runCommandWithOutputHandling(command); + + // Read the optimized PDF file + byte[] pdfBytes = Files.readAllBytes(tempOutputFile); + + // Clean up the temporary files + Files.delete(tempInputFile); + Files.delete(tempOutputFile); + + // Return the optimized PDF as a response + String outputFilename = inputFile.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_repaired.pdf"; + return PdfUtils.bytesToWebResponse(pdfBytes, outputFilename); + } + +} diff --git a/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java b/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java index 48b1f8e1..8199070c 100644 --- a/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java +++ b/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java @@ -88,5 +88,11 @@ public class OtherWebController { return "other/adjust-contrast"; } + @GetMapping("/repair") + @Hidden + public String repairForm(Model model) { + model.addAttribute("currentPage", "repair"); + return "other/repair"; + } } diff --git a/src/main/resources/messages_en_GB.properties b/src/main/resources/messages_en_GB.properties index 54a12f76..b58f5e96 100644 --- a/src/main/resources/messages_en_GB.properties +++ b/src/main/resources/messages_en_GB.properties @@ -114,6 +114,8 @@ home.sign.desc=Adds signature to PDF by drawing, text or image home.flatten.title=Flatten home.flatten.desc=Remove all interactive elements and forms from a PDF +home.repair.title=Repair +home.repair.desc=Tries to repair a corrupt/broken PDF ScannerImageSplit.selectText.1=Angle Threshold: ScannerImageSplit.selectText.2=Sets the minimum absolute angle required for the image to be rotated (default: 10). diff --git a/src/main/resources/static/images/wrench.svg b/src/main/resources/static/images/wrench.svg new file mode 100644 index 00000000..bef07136 --- /dev/null +++ b/src/main/resources/static/images/wrench.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/main/resources/templates/fragments/navbar.html b/src/main/resources/templates/fragments/navbar.html index 058e786c..0aa1ccf7 100644 --- a/src/main/resources/templates/fragments/navbar.html +++ b/src/main/resources/templates/fragments/navbar.html @@ -289,7 +289,10 @@ function compareVersions(version1, version2) { icon - + + + icon + diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html index 2f49b1b4..6e55502d 100644 --- a/src/main/resources/templates/home.html +++ b/src/main/resources/templates/home.html @@ -111,6 +111,7 @@ filter: invert(0.2) sepia(2) saturate(50) hue-rotate(190deg);
+
diff --git a/src/main/resources/templates/other/repair.html b/src/main/resources/templates/other/repair.html new file mode 100644 index 00000000..8ce329b6 --- /dev/null +++ b/src/main/resources/templates/other/repair.html @@ -0,0 +1,29 @@ + + + + + + + + +
+
+
+

+
+
+
+

+
+
+ +
+
+
+
+
+
+
+ + + \ No newline at end of file