diff --git a/commons/commons-api/src/main/java/fr/gouv/vitamui/commons/api/FileExtensions.java b/commons/commons-api/src/main/java/fr/gouv/vitamui/commons/api/FileExtensions.java index 2f343a2c621dc20ce2745c83b1414fc829068c0e..b2faa55279ff8ab888455c472f93500d862094bd 100644 --- a/commons/commons-api/src/main/java/fr/gouv/vitamui/commons/api/FileExtensions.java +++ b/commons/commons-api/src/main/java/fr/gouv/vitamui/commons/api/FileExtensions.java @@ -53,7 +53,10 @@ public class FileExtensions { public static final String JSON = ".json"; + public static final String PNG = ".png"; + private FileExtensions() { // do nothing } + } diff --git a/commons/commons-utils/pom.xml b/commons/commons-utils/pom.xml index cec29363db13682a61df059e900bf01e959ff6da..ca29c392535126509eba4004a5406b8b0c291b07 100644 --- a/commons/commons-utils/pom.xml +++ b/commons/commons-utils/pom.xml @@ -114,5 +114,10 @@ <artifactId>powermock-module-junit4</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.pdfbox</groupId> + <artifactId>pdfbox</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/commons/commons-utils/src/main/java/fr/gouv/vitamui/commons/utils/PdfFileGenerator.java b/commons/commons-utils/src/main/java/fr/gouv/vitamui/commons/utils/PdfFileGenerator.java index a463897816ea1e52b148b68770fc1d7c21e6efc8..deec705683e8d0b4e6058cf5f03f6eb2869229e3 100644 --- a/commons/commons-utils/src/main/java/fr/gouv/vitamui/commons/utils/PdfFileGenerator.java +++ b/commons/commons-utils/src/main/java/fr/gouv/vitamui/commons/utils/PdfFileGenerator.java @@ -53,9 +53,10 @@ public class PdfFileGenerator { /** * Method allowing to fill an odt template and generate a pdf file from it. - * @param odtTemplateInputStream the odt template to fill and convert to pdf /!\ the owner is reponsible for closing the stream. - * @param pdfOutputStream the generated pdf file /!\ the owner is reponsible for closing the stream. - * @param dataMap the object containing the data to add to the template. + * + * @param odtTemplateInputStream the odt template to fill and convert to pdf /!\ the owner is responsible for closing the stream. + * @param pdfOutputStream the generated pdf file /!\ the owner is responsible for closing the stream. + * @param dataMap the object containing the data to add to the template. * @throws Exception */ public static void createPdf(final InputStream odtTemplateInputStream, final OutputStream pdfOutputStream, final Map<String, Object> dataMap) @@ -66,26 +67,39 @@ public class PdfFileGenerator { /** * Method allowing to fill an odt template with dynamic and static data and generate a pdf file from it. + * * @param odtTemplateInputStream the odt template to fill and convert to pdf /!\ the owner is reponsible for closing the stream. - * @param pdfOutputStream the generated pdf file /!\ the owner is reponsible for closing the stream. - * @param dataMap the object containing the data to add to the template. - * @param dynamicFields the dynamic fields to add to the report to enable the creation of the pdf. + * @param pdfOutputStream the generated pdf file /!\ the owner is reponsible for closing the stream. + * @param dataMap the object containing the data to add to the template. + * @param dynamicFields the dynamic fields to add to the report to enable the creation of the pdf. * @throws Exception */ public static void createPdfWithDynamicInfo(final InputStream odtTemplateInputStream, final OutputStream pdfOutputStream, final Map<String, Object> dataMap, final String... dynamicFields) throws Exception { - final IXDocReport xdocGenerator = createXDocReport(odtTemplateInputStream); - final FieldsMetadata dynamicFieldsMetadata = new FieldsMetadata(); - for (final String dynamicField : dynamicFields) { - dynamicFieldsMetadata.addFieldAsList(dynamicField); - } - xdocGenerator.setFieldsMetadata(dynamicFieldsMetadata); + final IXDocReport xdocGenerator = generateXDocReport(odtTemplateInputStream, dynamicFields, new String[]{}); + createPdfDocument(xdocGenerator, dataMap, pdfOutputStream); + } + + /** + * Method allowing to fill an odt template with images, dynamic and static data and generate a pdf file from it. + * + * @param odtTemplateInputStream the odt template to fill and convert to pdf /!\ the owner is responsible for closing the stream. + * @param pdfOutputStream the generated pdf file /!\ the owner is responsible for closing the stream. + * @param dataMap the object containing the data to add to the template. + * @param dynamicFields the dynamic fields to add to the report to enable the creation of the pdf. + * @param imageFields the image fields to add to the report to enable the creation of the pdf. + * @throws Exception + */ + public static void createPdfWithMetadata(final InputStream odtTemplateInputStream, final OutputStream pdfOutputStream, final Map<String, Object> dataMap, + final String[] dynamicFields, final String[] imageFields) throws Exception { + final IXDocReport xdocGenerator = generateXDocReport(odtTemplateInputStream, dynamicFields, imageFields); createPdfDocument(xdocGenerator, dataMap, pdfOutputStream); } /** * Method allowing to get the report to generate a pdf file. - * @param odtTemplateInputStream the odt template to fill and convert to pdf /!\ the owner is reponsible for closing the stream. + * + * @param odtTemplateInputStream the odt template to fill and convert to pdf /!\ the owner is responsible for closing the stream. * @return the XDocReport. * @throws Exception */ @@ -108,4 +122,18 @@ public class PdfFileGenerator { xdocGenerator.convert(context, options, pdfOutputStream); } + private static IXDocReport generateXDocReport(final InputStream odtTemplateInputStream, final String[] dynamicFields, final String[] imageFields) + throws Exception { + final IXDocReport xdocGenerator = createXDocReport(odtTemplateInputStream); + final FieldsMetadata fieldsMetadata = new FieldsMetadata(); + for (final String dynamicField : dynamicFields) { + fieldsMetadata.addFieldAsList(dynamicField); + } + for (final String imageField : imageFields) { + fieldsMetadata.addFieldAsImage(imageField); + } + xdocGenerator.setFieldsMetadata(fieldsMetadata); + return xdocGenerator; + } + } diff --git a/commons/commons-utils/src/test/java/fr/gouv/vitamui/commons/utils/PdfFileGeneratorTest.java b/commons/commons-utils/src/test/java/fr/gouv/vitamui/commons/utils/PdfFileGeneratorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..82fc6c60e2022033d41c9cc2ae0ad5d3fe4a354c --- /dev/null +++ b/commons/commons-utils/src/test/java/fr/gouv/vitamui/commons/utils/PdfFileGeneratorTest.java @@ -0,0 +1,79 @@ +package fr.gouv.vitamui.commons.utils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.text.PDFTextStripper; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class PdfFileGeneratorTest { + + public static final String GENERATED_PDF_NAME = "generated-pdf.pdf"; + + private static final String TEST_DIRECTORY = "src/test/resources/"; + + private static final String TMP_DIRECTORY = "/tmp/dlab/test/PdfFileGeneratorTest/"; + + @BeforeClass + public static void setUp() throws IOException { + if (!new File(TMP_DIRECTORY).exists()) { + Files.createDirectories(Paths.get(TMP_DIRECTORY)); + } + } + + @Test + public void testCreatePdfWithDynamicInfo() throws Exception { + // The template contains "${data},${dynamic.field}" + try (final InputStream templateInput = new FileInputStream(new File(TEST_DIRECTORY + "template-dynamic-info.odt")); + final FileOutputStream pdfOutput = new FileOutputStream(new File(TMP_DIRECTORY + GENERATED_PDF_NAME))) { + final Map<String, Object> dataMap = new HashMap<>(); + final String[] dynamicFields = {"dynamic.field"}; + dataMap.put("data", "value1"); + dataMap.put("dynamic.field", "value2"); + + PdfFileGenerator.createPdfWithDynamicInfo(templateInput, pdfOutput, dataMap, dynamicFields); + } + + try (final PDDocument document = PDDocument.load(new File(TMP_DIRECTORY + GENERATED_PDF_NAME))) { + final String content = new PDFTextStripper().getText(document); + final String[] results = content.split(","); + Assert.assertEquals("value1", results[0].trim()); + Assert.assertEquals("value2", results[1].trim()); + } + } + + @Test + public void testCreatePdfWithMetadata() throws Exception { + // The template contains "${data},${dynamic.field},${imageField}" + try (final InputStream templateInput = new FileInputStream(new File(TEST_DIRECTORY + "template-metadata.odt")); + final FileOutputStream pdfOutput = new FileOutputStream(new File(TMP_DIRECTORY + GENERATED_PDF_NAME))) { + final Map<String, Object> dataMap = new HashMap<>(); + final String[] dynamicFields = {"dynamic.field"}; + final String[] imageFields = {"imageField"}; + dataMap.put("data", "value1"); + dataMap.put("dynamic.field", "value2"); + dataMap.put("imageField", "value3"); + + PdfFileGenerator.createPdfWithMetadata(templateInput, pdfOutput, dataMap, dynamicFields, imageFields); + } + + try (final PDDocument document = PDDocument.load(new File(TMP_DIRECTORY + GENERATED_PDF_NAME))) { + final String content = new PDFTextStripper().getText(document); + final String[] results = content.split(","); + Assert.assertEquals("value1", results[0].trim()); + Assert.assertEquals("value2", results[1].trim()); + Assert.assertEquals("value3", results[2].trim()); + } + } + +} diff --git a/commons/commons-utils/src/test/resources/template-dynamic-info.odt b/commons/commons-utils/src/test/resources/template-dynamic-info.odt new file mode 100644 index 0000000000000000000000000000000000000000..95124f09ea33130cefc2675814f57bf382b0664e Binary files /dev/null and b/commons/commons-utils/src/test/resources/template-dynamic-info.odt differ diff --git a/commons/commons-utils/src/test/resources/template-metadata.odt b/commons/commons-utils/src/test/resources/template-metadata.odt new file mode 100644 index 0000000000000000000000000000000000000000..e7032e8a8caddfa97bfd6199519360557e0ccf9f Binary files /dev/null and b/commons/commons-utils/src/test/resources/template-metadata.odt differ diff --git a/pom.xml b/pom.xml index 36d2f5e4ef0079f2135d2c6207d7748ad9bba47d..48717ec00dcb0676ea8b2272238b070a64f280fa 100644 --- a/pom.xml +++ b/pom.xml @@ -144,6 +144,7 @@ <maven.deploy.plugin.version>2.8.2</maven.deploy.plugin.version> <maven.exec.plugin.version>1.6.0</maven.exec.plugin.version> <maven.failsafe.plugin.version>2.21.0</maven.failsafe.plugin.version> + <maven.filtering.plugin.version>1.3</maven.filtering.plugin.version> <maven.findbugs.plugin.version>3.0.4</maven.findbugs.plugin.version> <maven.frontend.plugin.version>1.6</maven.frontend.plugin.version> <maven.jacoco.plugin.version>0.8.0</maven.jacoco.plugin.version> @@ -821,6 +822,12 @@ <version>${jsonassert.version}</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.pdfbox</groupId> + <artifactId>pdfbox</artifactId> + <version>${apache.pdfbox.version}</version> + <scope>test</scope> + </dependency> <!-- CUCUMBER SERENITY --> <dependency> @@ -1009,6 +1016,13 @@ <nonFilteredFileExtension>pem</nonFilteredFileExtension> </nonFilteredFileExtensions> </configuration> + <dependencies> + <dependency> + <groupId>org.apache.maven.shared</groupId> + <artifactId>maven-filtering</artifactId> + <version>${maven.filtering.plugin.version}</version> + </dependency> + </dependencies> </plugin> </plugins> <pluginManagement>