From ac5273244c42fe5ccf7e80ce3e637f3554ac6d33 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sun, 5 May 2024 13:33:17 +0100 Subject: [PATCH] flatten (#1167) --- .../SPDF/config/ConfigInitializer.java | 14 ++-- .../config/security/InitialSecuritySetup.java | 2 +- .../api/misc/FlattenController.java | 84 +++++++++++++++++++ .../SPDF/model/api/misc/FlattenRequest.java | 17 ++++ src/main/resources/messages_ar_AR.properties | 1 + src/main/resources/messages_bg_BG.properties | 1 + src/main/resources/messages_ca_CA.properties | 1 + src/main/resources/messages_de_DE.properties | 1 + src/main/resources/messages_el_GR.properties | 1 + src/main/resources/messages_en_GB.properties | 3 +- src/main/resources/messages_en_US.properties | 1 + src/main/resources/messages_es_ES.properties | 1 + src/main/resources/messages_eu_ES.properties | 1 + src/main/resources/messages_fr_FR.properties | 1 + src/main/resources/messages_hi_IN.properties | 1 + src/main/resources/messages_hu_HU.properties | 1 + src/main/resources/messages_id_ID.properties | 1 + src/main/resources/messages_it_IT.properties | 1 + src/main/resources/messages_ja_JP.properties | 1 + src/main/resources/messages_ko_KR.properties | 1 + src/main/resources/messages_nl_NL.properties | 1 + src/main/resources/messages_pl_PL.properties | 1 + src/main/resources/messages_pt_BR.properties | 1 + src/main/resources/messages_pt_PT.properties | 1 + src/main/resources/messages_ro_RO.properties | 1 + src/main/resources/messages_ru_RU.properties | 1 + .../resources/messages_sr_LATN_RS.properties | 1 + src/main/resources/messages_sv_SE.properties | 1 + src/main/resources/messages_tr_TR.properties | 1 + src/main/resources/messages_uk_UA.properties | 1 + src/main/resources/messages_zh_CN.properties | 1 + src/main/resources/messages_zh_TW.properties | 1 + .../resources/templates/misc/flatten.html | 36 ++------ 33 files changed, 145 insertions(+), 38 deletions(-) create mode 100644 src/main/java/stirling/software/SPDF/controller/api/misc/FlattenController.java create mode 100644 src/main/java/stirling/software/SPDF/model/api/misc/FlattenRequest.java diff --git a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java index b31f44f8..394baeb1 100644 --- a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java +++ b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java @@ -13,8 +13,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; @@ -76,6 +74,7 @@ public class ConfigInitializer Files.createFile(customSettingsPath); } } + private static Map extractEntries(List lines) { Map entries = new HashMap<>(); StringBuilder currentEntry = new StringBuilder(); @@ -121,8 +120,10 @@ public class ConfigInitializer return entries; } - - private static List mergeConfigs(List templateLines, Map templateEntries, Map userEntries) { + private static List mergeConfigs( + List templateLines, + Map templateEntries, + Map userEntries) { List mergedLines = new ArrayList<>(); Set handledKeys = new HashSet<>(); @@ -141,7 +142,8 @@ public class ConfigInitializer blockIndent = indentLevel; } - if (userEntries.containsKey(currentBlockKey) && !handledKeys.contains(currentBlockKey)) { + if (userEntries.containsKey(currentBlockKey) + && !handledKeys.contains(currentBlockKey)) { mergedLines.add(userEntries.get(currentBlockKey)); handledKeys.add(currentBlockKey); } else if (!handledKeys.contains(currentBlockKey)) { @@ -152,8 +154,6 @@ public class ConfigInitializer return mergedLines; } - - private static List cleanInvalidYamlEntries(List lines) { List cleanedLines = new ArrayList<>(); for (int i = 0; i < lines.size(); i++) { diff --git a/src/main/java/stirling/software/SPDF/config/security/InitialSecuritySetup.java b/src/main/java/stirling/software/SPDF/config/security/InitialSecuritySetup.java index b472622b..b483ba10 100644 --- a/src/main/java/stirling/software/SPDF/config/security/InitialSecuritySetup.java +++ b/src/main/java/stirling/software/SPDF/config/security/InitialSecuritySetup.java @@ -85,6 +85,7 @@ public class InitialSecuritySetup { // Write back to the file Files.write(path, lines); } + private boolean isValidUUID(String uuid) { if (uuid == null) { return false; @@ -96,5 +97,4 @@ public class InitialSecuritySetup { return false; } } - } diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/FlattenController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/FlattenController.java new file mode 100644 index 00000000..bdc0a478 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/FlattenController.java @@ -0,0 +1,84 @@ +package stirling.software.SPDF.controller.api.misc; + +import java.awt.image.BufferedImage; +import java.io.IOException; + +import org.apache.pdfbox.Loader; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.PDPageContentStream; +import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory; +import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; +import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; +import org.apache.pdfbox.rendering.ImageType; +import org.apache.pdfbox.rendering.PDFRenderer; +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 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.PdfMetadata; +import stirling.software.SPDF.model.api.misc.FlattenRequest; +import stirling.software.SPDF.utils.PdfUtils; +import stirling.software.SPDF.utils.WebResponseUtils; + +@RestController +@RequestMapping("/api/v1/misc") +@Tag(name = "Misc", description = "Miscellaneous APIs") +public class FlattenController { + + @PostMapping(consumes = "multipart/form-data", value = "/flatten") + @Operation( + summary = "Flatten PDF form fields or full page", + description = + "Flattening just PDF form fields or converting each page to images to make text unselectable. Input: PDF, Output: PDF. Type: SISO") + public ResponseEntity flatten(@ModelAttribute FlattenRequest request) throws Exception { + MultipartFile file = request.getFileInput(); + + PDDocument document = Loader.loadPDF(file.getBytes()); + PdfMetadata metadata = PdfUtils.extractMetadataFromPdf(document); + Boolean flattenOnlyForms = request.getFlattenOnlyForms(); + + if (Boolean.TRUE.equals(flattenOnlyForms)) { + PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); + if (acroForm != null) { + acroForm.flatten(); + } + return WebResponseUtils.pdfDocToWebResponse( + document, Filenames.toSimpleFileName(file.getOriginalFilename())); + } else { + // flatten whole page aka convert each page to image and readd it (making text + // unselectable) + PDFRenderer pdfRenderer = new PDFRenderer(document); + PDDocument newDocument = new PDDocument(); + int numPages = document.getNumberOfPages(); + for (int i = 0; i < numPages; i++) { + try { + BufferedImage image = pdfRenderer.renderImageWithDPI(i, 300, ImageType.RGB); + PDPage page = new PDPage(); + page.setMediaBox(document.getPage(i).getMediaBox()); + newDocument.addPage(page); + try (PDPageContentStream contentStream = + new PDPageContentStream(newDocument, page)) { + PDImageXObject pdImage = JPEGFactory.createFromImage(newDocument, image); + float pageWidth = page.getMediaBox().getWidth(); + float pageHeight = page.getMediaBox().getHeight(); + + contentStream.drawImage(pdImage, 0, 0, pageWidth, pageHeight); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + PdfUtils.setMetadataToPdf(newDocument, metadata); + return WebResponseUtils.pdfDocToWebResponse( + newDocument, Filenames.toSimpleFileName(file.getOriginalFilename())); + } + } +} diff --git a/src/main/java/stirling/software/SPDF/model/api/misc/FlattenRequest.java b/src/main/java/stirling/software/SPDF/model/api/misc/FlattenRequest.java new file mode 100644 index 00000000..c87d1974 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/model/api/misc/FlattenRequest.java @@ -0,0 +1,17 @@ +package stirling.software.SPDF.model.api.misc; + +import io.swagger.v3.oas.annotations.media.Schema; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import stirling.software.SPDF.model.api.PDFFile; + +@Data +@EqualsAndHashCode(callSuper = true) +public class FlattenRequest extends PDFFile { + + @Schema( + description = + "True to flatten only the forms, false to flatten full PDF (Convert page to image)") + private Boolean flattenOnlyForms; +} diff --git a/src/main/resources/messages_ar_AR.properties b/src/main/resources/messages_ar_AR.properties index c1102346..c6119110 100644 --- a/src/main/resources/messages_ar_AR.properties +++ b/src/main/resources/messages_ar_AR.properties @@ -694,6 +694,7 @@ repair.submit=الإصلاح #flatten flatten.title=تسطيح flatten.header=تسوية ملفات PDF +flatten.flattenOnlyForms=Flatten only forms flatten.submit=تسطيح diff --git a/src/main/resources/messages_bg_BG.properties b/src/main/resources/messages_bg_BG.properties index 865522f6..ca196110 100644 --- a/src/main/resources/messages_bg_BG.properties +++ b/src/main/resources/messages_bg_BG.properties @@ -694,6 +694,7 @@ repair.submit=Поправи #flatten flatten.title=Изравнете flatten.header=Изравнете PDF-и +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Изравнете diff --git a/src/main/resources/messages_ca_CA.properties b/src/main/resources/messages_ca_CA.properties index c2e2e476..41f4f662 100644 --- a/src/main/resources/messages_ca_CA.properties +++ b/src/main/resources/messages_ca_CA.properties @@ -694,6 +694,7 @@ repair.submit=Reparar #flatten flatten.title=Aplanar flatten.header=Aplana els PDF +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Aplanar diff --git a/src/main/resources/messages_de_DE.properties b/src/main/resources/messages_de_DE.properties index a007f314..6cbd3a29 100644 --- a/src/main/resources/messages_de_DE.properties +++ b/src/main/resources/messages_de_DE.properties @@ -694,6 +694,7 @@ repair.submit=Reparieren #flatten flatten.title=Abflachen flatten.header=PDFs reduzieren +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Abflachen diff --git a/src/main/resources/messages_el_GR.properties b/src/main/resources/messages_el_GR.properties index 08e5f329..e4d2537c 100644 --- a/src/main/resources/messages_el_GR.properties +++ b/src/main/resources/messages_el_GR.properties @@ -694,6 +694,7 @@ repair.submit=Επιδιόρθωση #flatten flatten.title=Flatten flatten.header=Flatten PDFs +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Flatten diff --git a/src/main/resources/messages_en_GB.properties b/src/main/resources/messages_en_GB.properties index ed3779cc..6189f56b 100644 --- a/src/main/resources/messages_en_GB.properties +++ b/src/main/resources/messages_en_GB.properties @@ -693,7 +693,8 @@ repair.submit=Repair #flatten flatten.title=Flatten -flatten.header=Flatten PDFs +flatten.header=Flatten PDF +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Flatten diff --git a/src/main/resources/messages_en_US.properties b/src/main/resources/messages_en_US.properties index df24e3a2..2ecaa2e8 100644 --- a/src/main/resources/messages_en_US.properties +++ b/src/main/resources/messages_en_US.properties @@ -694,6 +694,7 @@ repair.submit=Repair #flatten flatten.title=Flatten flatten.header=Flatten PDFs +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Flatten diff --git a/src/main/resources/messages_es_ES.properties b/src/main/resources/messages_es_ES.properties index 96008bed..275bf1d1 100644 --- a/src/main/resources/messages_es_ES.properties +++ b/src/main/resources/messages_es_ES.properties @@ -694,6 +694,7 @@ repair.submit=Reparar #flatten flatten.title=Aplanar flatten.header=Acoplar archivos PDF +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Aplanar diff --git a/src/main/resources/messages_eu_ES.properties b/src/main/resources/messages_eu_ES.properties index 5df9bc3d..c9337e03 100644 --- a/src/main/resources/messages_eu_ES.properties +++ b/src/main/resources/messages_eu_ES.properties @@ -694,6 +694,7 @@ repair.submit=Konpondu #flatten flatten.title=Lautu flatten.header=Akoplatu PDF fitxategiak +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Lautu diff --git a/src/main/resources/messages_fr_FR.properties b/src/main/resources/messages_fr_FR.properties index 6624b01c..06068ffc 100644 --- a/src/main/resources/messages_fr_FR.properties +++ b/src/main/resources/messages_fr_FR.properties @@ -694,6 +694,7 @@ repair.submit=Réparer #flatten flatten.title=Rendre inerte flatten.header=Rendre inerte +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Rendre inerte diff --git a/src/main/resources/messages_hi_IN.properties b/src/main/resources/messages_hi_IN.properties index 355ccb9e..9bcddca1 100644 --- a/src/main/resources/messages_hi_IN.properties +++ b/src/main/resources/messages_hi_IN.properties @@ -694,6 +694,7 @@ repair.submit=मरम्मत #flatten flatten.title=समतल करें flatten.header=पीडीएफ़ समतल करें +flatten.flattenOnlyForms=Flatten only forms flatten.submit=समतल करें diff --git a/src/main/resources/messages_hu_HU.properties b/src/main/resources/messages_hu_HU.properties index 3b2811d4..625e8b23 100644 --- a/src/main/resources/messages_hu_HU.properties +++ b/src/main/resources/messages_hu_HU.properties @@ -694,6 +694,7 @@ repair.submit=Javítás #flatten flatten.title=Kiegyenlítés flatten.header=PDF-ek kiegyenlítése +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Kiegyenlítés diff --git a/src/main/resources/messages_id_ID.properties b/src/main/resources/messages_id_ID.properties index 370b01ad..fd5c3e31 100644 --- a/src/main/resources/messages_id_ID.properties +++ b/src/main/resources/messages_id_ID.properties @@ -694,6 +694,7 @@ repair.submit=Perbaiki #flatten flatten.title=Ratakan flatten.header=Ratakan PDF +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Ratakan diff --git a/src/main/resources/messages_it_IT.properties b/src/main/resources/messages_it_IT.properties index fb1f439e..4a867e83 100644 --- a/src/main/resources/messages_it_IT.properties +++ b/src/main/resources/messages_it_IT.properties @@ -694,6 +694,7 @@ repair.submit=Ripara #flatten flatten.title=Appiattisci flatten.header=Appiattisci PDF +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Appiattisci diff --git a/src/main/resources/messages_ja_JP.properties b/src/main/resources/messages_ja_JP.properties index febd561c..9f981333 100644 --- a/src/main/resources/messages_ja_JP.properties +++ b/src/main/resources/messages_ja_JP.properties @@ -694,6 +694,7 @@ repair.submit=修復 #flatten flatten.title=平坦化 flatten.header=PDFを平坦化する +flatten.flattenOnlyForms=Flatten only forms flatten.submit=平坦化 diff --git a/src/main/resources/messages_ko_KR.properties b/src/main/resources/messages_ko_KR.properties index e8604477..da011ec6 100644 --- a/src/main/resources/messages_ko_KR.properties +++ b/src/main/resources/messages_ko_KR.properties @@ -694,6 +694,7 @@ repair.submit=복구 #flatten flatten.title=평탄화 flatten.header=PDF 문서의 레이어 평탄화 +flatten.flattenOnlyForms=Flatten only forms flatten.submit=평탄화 diff --git a/src/main/resources/messages_nl_NL.properties b/src/main/resources/messages_nl_NL.properties index 6ba6a7b3..cbbf7b43 100644 --- a/src/main/resources/messages_nl_NL.properties +++ b/src/main/resources/messages_nl_NL.properties @@ -694,6 +694,7 @@ repair.submit=Repareren #flatten flatten.title=Afvlakken flatten.header=PDF's afvlakken +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Afvlakken diff --git a/src/main/resources/messages_pl_PL.properties b/src/main/resources/messages_pl_PL.properties index ee96d34f..757647ee 100644 --- a/src/main/resources/messages_pl_PL.properties +++ b/src/main/resources/messages_pl_PL.properties @@ -694,6 +694,7 @@ repair.submit=Napraw #flatten flatten.title=Spłaszcz flatten.header=Spłaszcz dokument(y) PDF +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Spłaszcz diff --git a/src/main/resources/messages_pt_BR.properties b/src/main/resources/messages_pt_BR.properties index 715615e4..97ac151e 100644 --- a/src/main/resources/messages_pt_BR.properties +++ b/src/main/resources/messages_pt_BR.properties @@ -694,6 +694,7 @@ repair.submit=Reparar #flatten flatten.title=Achatar flatten.header=Achatar PDFs +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Achatar diff --git a/src/main/resources/messages_pt_PT.properties b/src/main/resources/messages_pt_PT.properties index 7a5541e2..fcde52ad 100644 --- a/src/main/resources/messages_pt_PT.properties +++ b/src/main/resources/messages_pt_PT.properties @@ -694,6 +694,7 @@ repair.submit=Reparar #flatten flatten.title=Achatar flatten.header=Achatar PDFs +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Achatar diff --git a/src/main/resources/messages_ro_RO.properties b/src/main/resources/messages_ro_RO.properties index 55f5e3d9..1f3daca5 100644 --- a/src/main/resources/messages_ro_RO.properties +++ b/src/main/resources/messages_ro_RO.properties @@ -694,6 +694,7 @@ repair.submit=Repară #flatten flatten.title=Nivelare flatten.header=Nivelează documente PDF +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Nivelează diff --git a/src/main/resources/messages_ru_RU.properties b/src/main/resources/messages_ru_RU.properties index fc7f3965..4517e100 100644 --- a/src/main/resources/messages_ru_RU.properties +++ b/src/main/resources/messages_ru_RU.properties @@ -694,6 +694,7 @@ repair.submit=Ремонт #flatten flatten.title=Сглаживание flatten.header=Сглаживание PDF ов +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Сгладить diff --git a/src/main/resources/messages_sr_LATN_RS.properties b/src/main/resources/messages_sr_LATN_RS.properties index 07c32db3..708dc2d8 100644 --- a/src/main/resources/messages_sr_LATN_RS.properties +++ b/src/main/resources/messages_sr_LATN_RS.properties @@ -694,6 +694,7 @@ repair.submit=Popravi #flatten flatten.title=Ravnanje flatten.header=Ravnanje PDF fajlova +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Ravnanje diff --git a/src/main/resources/messages_sv_SE.properties b/src/main/resources/messages_sv_SE.properties index ac29f553..f621c73c 100644 --- a/src/main/resources/messages_sv_SE.properties +++ b/src/main/resources/messages_sv_SE.properties @@ -694,6 +694,7 @@ repair.submit=Reparera #flatten flatten.title=Platta till flatten.header=Placera PDF-filer +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Platta till diff --git a/src/main/resources/messages_tr_TR.properties b/src/main/resources/messages_tr_TR.properties index c8d3bab6..8484e555 100644 --- a/src/main/resources/messages_tr_TR.properties +++ b/src/main/resources/messages_tr_TR.properties @@ -694,6 +694,7 @@ repair.submit=Onar #flatten flatten.title=Düzleştir flatten.header=PDF'leri Düzleştir +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Düzleştir diff --git a/src/main/resources/messages_uk_UA.properties b/src/main/resources/messages_uk_UA.properties index 6cd0ace0..1ace290c 100644 --- a/src/main/resources/messages_uk_UA.properties +++ b/src/main/resources/messages_uk_UA.properties @@ -694,6 +694,7 @@ repair.submit=Ремонтувати #flatten flatten.title=Згладжування flatten.header=Згладжування PDF +flatten.flattenOnlyForms=Flatten only forms flatten.submit=Згладити diff --git a/src/main/resources/messages_zh_CN.properties b/src/main/resources/messages_zh_CN.properties index 7ea7ed55..ca04fc36 100644 --- a/src/main/resources/messages_zh_CN.properties +++ b/src/main/resources/messages_zh_CN.properties @@ -694,6 +694,7 @@ repair.submit=修复 #flatten flatten.title=展平 flatten.header=展平 PDF +flatten.flattenOnlyForms=Flatten only forms flatten.submit=展平 diff --git a/src/main/resources/messages_zh_TW.properties b/src/main/resources/messages_zh_TW.properties index 2c089b13..8391da55 100644 --- a/src/main/resources/messages_zh_TW.properties +++ b/src/main/resources/messages_zh_TW.properties @@ -694,6 +694,7 @@ repair.submit=修復 #flatten flatten.title=平坦化 flatten.header=PDF 平坦化 +flatten.flattenOnlyForms=Flatten only forms flatten.submit=平坦化 diff --git a/src/main/resources/templates/misc/flatten.html b/src/main/resources/templates/misc/flatten.html index 49710831..bbc06f58 100644 --- a/src/main/resources/templates/misc/flatten.html +++ b/src/main/resources/templates/misc/flatten.html @@ -13,36 +13,14 @@

-
-
-
-
+ +
+
+ + +
+
- -