From 6793fd5bc7fcf9a046c75b618e435137f9b67bd4 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Thu, 1 Jun 2023 21:37:33 +0100 Subject: [PATCH] thirs party js folder --- .../controller/api/ScalePagesController.java | 67 ++++++++++++++----- src/main/resources/messages_en_GB.properties | 2 +- .../js/{ => thirdParty}/bootstrap.min.js | 0 .../js/{ => thirdParty}/interact.min.js | 0 .../static/js/{ => thirdParty}/jquery.min.js | 0 .../static/js/{ => thirdParty}/jszip.min.js | 0 .../static/js/{ => thirdParty}/pdf-lib.min.js | 0 .../static/js/{ => thirdParty}/popper.min.js | 0 .../{ => thirdParty}/signature_pad.umd.min.js | 0 .../resources/templates/fragments/common.html | 10 +-- .../resources/templates/other/add-image.html | 2 +- .../templates/other/scale-pages.html | 28 +++++++- src/main/resources/templates/sign.html | 4 +- 13 files changed, 87 insertions(+), 26 deletions(-) rename src/main/resources/static/js/{ => thirdParty}/bootstrap.min.js (100%) rename src/main/resources/static/js/{ => thirdParty}/interact.min.js (100%) rename src/main/resources/static/js/{ => thirdParty}/jquery.min.js (100%) rename src/main/resources/static/js/{ => thirdParty}/jszip.min.js (100%) rename src/main/resources/static/js/{ => thirdParty}/pdf-lib.min.js (100%) rename src/main/resources/static/js/{ => thirdParty}/popper.min.js (100%) rename src/main/resources/static/js/{ => thirdParty}/signature_pad.umd.min.js (100%) 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 a5f874b2..508d4fc3 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/ScalePagesController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/ScalePagesController.java @@ -26,7 +26,8 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Schema; import stirling.software.SPDF.utils.WebResponseUtils; - +import java.util.HashMap; +import java.util.Map; @RestController public class ScalePagesController { @@ -34,15 +35,49 @@ public class ScalePagesController { @PostMapping(value = "/scale-pages", consumes = "multipart/form-data") @Operation(summary = "Change the size of a PDF page/document", description = "This operation takes an input PDF file and the size to scale the pages to in the output PDF file.") - public ResponseEntity mergeMultiplePagesIntoOne( - @Parameter(description = "The input PDF file", required = true) @RequestParam("fileInput") MultipartFile file, - @Parameter(description = "The scale of pages in the output PDF. Acceptable values are A4.", required = true, schema = @Schema(type = "String", allowableValues = { "A4" })) @RequestParam("pageSize") String targetPageSize, - @Parameter(description = "The scale of the content on the pages of the output PDF. Acceptable values are floats.", required = true, schema = @Schema(type = "float")) @RequestParam("scaleFactor") float scaleFactor) - throws IOException { + public ResponseEntity scalePages( + @Parameter(description = "The input PDF file", required = true) @RequestParam("fileInput") MultipartFile file, + @Parameter(description = "The scale of pages in the output PDF. Acceptable values are A0-A10, B0-B9, LETTER, TABLOID, LEDGER, LEGAL, EXECUTIVE.", required = true, schema = @Schema(type = "String", allowableValues = { "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "LETTER", "TABLOID", "LEDGER", "LEGAL", "EXECUTIVE" })) @RequestParam("pageSize") String targetPageSize, + @Parameter(description = "The scale of the content on the pages of the output PDF. Acceptable values are floats.", required = true, schema = @Schema(type = "float")) @RequestParam("scaleFactor") float scaleFactor) + throws IOException { - if (!targetPageSize.equals("A4")) { - throw new IllegalArgumentException("pageSize must be A4"); - } + Map sizeMap = new HashMap<>(); + // Add A0 - A10 + sizeMap.put("A0", PageSize.A0); + sizeMap.put("A1", PageSize.A1); + sizeMap.put("A2", PageSize.A2); + sizeMap.put("A3", PageSize.A3); + sizeMap.put("A4", PageSize.A4); + sizeMap.put("A5", PageSize.A5); + sizeMap.put("A6", PageSize.A6); + sizeMap.put("A7", PageSize.A7); + sizeMap.put("A8", PageSize.A8); + sizeMap.put("A9", PageSize.A9); + sizeMap.put("A10", PageSize.A10); + // Add B0 - B9 + sizeMap.put("B0", PageSize.B0); + sizeMap.put("B1", PageSize.B1); + sizeMap.put("B2", PageSize.B2); + sizeMap.put("B3", PageSize.B3); + sizeMap.put("B4", PageSize.B4); + sizeMap.put("B5", PageSize.B5); + sizeMap.put("B6", PageSize.B6); + sizeMap.put("B7", PageSize.B7); + sizeMap.put("B8", PageSize.B8); + sizeMap.put("B9", PageSize.B9); + // Add other sizes + sizeMap.put("LETTER", PageSize.LETTER); + sizeMap.put("TABLOID", PageSize.TABLOID); + sizeMap.put("LEDGER", PageSize.LEDGER); + sizeMap.put("LEGAL", PageSize.LEGAL); + sizeMap.put("EXECUTIVE", PageSize.EXECUTIVE); + + if (!sizeMap.containsKey(targetPageSize)) { + throw new IllegalArgumentException("Invalid pageSize. It must be one of the following: A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10"); + } + + PageSize pageSize = sizeMap.get(targetPageSize); + byte[] bytes = file.getBytes(); PdfReader reader = new PdfReader(new ByteArrayInputStream(bytes)); @@ -51,9 +86,9 @@ public class ScalePagesController { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = new PdfWriter(baos); PdfDocument outputPdf = new PdfDocument(writer); - - PageSize pageSize = new PageSize(PageSize.A4); // TODO: This (and all other PageSize.A4) need to be dynamically changed in response to targetPageSize - + + + int totalPages = pdfDoc.getNumberOfPages(); for (int i = 1; i <= totalPages; i++) { @@ -62,14 +97,14 @@ public class ScalePagesController { // Get the page and calculate scaling factors Rectangle rect = pdfDoc.getPage(i).getPageSize(); - float scaleWidth = PageSize.A4.getWidth() / rect.getWidth(); - float scaleHeight = PageSize.A4.getHeight() / rect.getHeight(); + float scaleWidth = pageSize.getWidth() / rect.getWidth(); + float scaleHeight = pageSize.getHeight() / rect.getHeight(); float scale = Math.min(scaleWidth, scaleHeight) * scaleFactor; System.out.println("Scale: " + scale); PdfFormXObject formXObject = pdfDoc.getPage(i).copyAsFormXObject(outputPdf); - float x = (PageSize.A4.getWidth() - rect.getWidth() * scale) / 2; // Center Page - float y = (PageSize.A4.getHeight() - rect.getHeight() * scale) / 2; + float x = (pageSize.getWidth() - rect.getWidth() * scale) / 2; // Center Page + float y = (pageSize.getHeight() - rect.getHeight() * scale) / 2; // Save the graphics state, apply the transformations, add the object, and then // restore the graphics state diff --git a/src/main/resources/messages_en_GB.properties b/src/main/resources/messages_en_GB.properties index b8c634f2..5eee2506 100644 --- a/src/main/resources/messages_en_GB.properties +++ b/src/main/resources/messages_en_GB.properties @@ -132,7 +132,7 @@ home.pageLayout.title=Multi-Page Layout home.pageLayout.desc=Merge multiple pages of a PDF document into a single page home.scalePages.title=Adjust page-scale -home.scalePages.desc=Change the size of the pages of a PDF document +home.scalePages.desc=Change the size of page contents while maintaining a set page-size error.pdfPassword=The PDF Document is passworded and either the password was not provided or was incorrect diff --git a/src/main/resources/static/js/bootstrap.min.js b/src/main/resources/static/js/thirdParty/bootstrap.min.js similarity index 100% rename from src/main/resources/static/js/bootstrap.min.js rename to src/main/resources/static/js/thirdParty/bootstrap.min.js diff --git a/src/main/resources/static/js/interact.min.js b/src/main/resources/static/js/thirdParty/interact.min.js similarity index 100% rename from src/main/resources/static/js/interact.min.js rename to src/main/resources/static/js/thirdParty/interact.min.js diff --git a/src/main/resources/static/js/jquery.min.js b/src/main/resources/static/js/thirdParty/jquery.min.js similarity index 100% rename from src/main/resources/static/js/jquery.min.js rename to src/main/resources/static/js/thirdParty/jquery.min.js diff --git a/src/main/resources/static/js/jszip.min.js b/src/main/resources/static/js/thirdParty/jszip.min.js similarity index 100% rename from src/main/resources/static/js/jszip.min.js rename to src/main/resources/static/js/thirdParty/jszip.min.js diff --git a/src/main/resources/static/js/pdf-lib.min.js b/src/main/resources/static/js/thirdParty/pdf-lib.min.js similarity index 100% rename from src/main/resources/static/js/pdf-lib.min.js rename to src/main/resources/static/js/thirdParty/pdf-lib.min.js diff --git a/src/main/resources/static/js/popper.min.js b/src/main/resources/static/js/thirdParty/popper.min.js similarity index 100% rename from src/main/resources/static/js/popper.min.js rename to src/main/resources/static/js/thirdParty/popper.min.js diff --git a/src/main/resources/static/js/signature_pad.umd.min.js b/src/main/resources/static/js/thirdParty/signature_pad.umd.min.js similarity index 100% rename from src/main/resources/static/js/signature_pad.umd.min.js rename to src/main/resources/static/js/thirdParty/signature_pad.umd.min.js diff --git a/src/main/resources/templates/fragments/common.html b/src/main/resources/templates/fragments/common.html index c7183ee3..3b654499 100644 --- a/src/main/resources/templates/fragments/common.html +++ b/src/main/resources/templates/fragments/common.html @@ -8,14 +8,14 @@ - + - + - - + + @@ -23,7 +23,7 @@ - + diff --git a/src/main/resources/templates/other/add-image.html b/src/main/resources/templates/other/add-image.html index 5e800481..1e9717c9 100644 --- a/src/main/resources/templates/other/add-image.html +++ b/src/main/resources/templates/other/add-image.html @@ -3,7 +3,7 @@ - + diff --git a/src/main/resources/templates/other/scale-pages.html b/src/main/resources/templates/other/scale-pages.html index b2acd961..b2030a4d 100644 --- a/src/main/resources/templates/other/scale-pages.html +++ b/src/main/resources/templates/other/scale-pages.html @@ -19,7 +19,33 @@
diff --git a/src/main/resources/templates/sign.html b/src/main/resources/templates/sign.html index 33ba5914..f2a9f698 100644 --- a/src/main/resources/templates/sign.html +++ b/src/main/resources/templates/sign.html @@ -3,8 +3,8 @@ - - + +