diff --git a/pdf/pom.xml b/pdf/pom.xml index ce07676..60cc1ef 100644 --- a/pdf/pom.xml +++ b/pdf/pom.xml @@ -63,5 +63,9 @@ + + commons-io + commons-io + \ No newline at end of file diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/PDFTemplate.java b/pdf/src/main/java/de/muehlencord/shared/pdf/PDFTemplate.java index 1e4778c..a260066 100644 --- a/pdf/src/main/java/de/muehlencord/shared/pdf/PDFTemplate.java +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/PDFTemplate.java @@ -32,6 +32,10 @@ public class PDFTemplate { this.template = template; this.dataModel = new ConcurrentHashMap<>(); } + + public void addToDatamodel (String key, Object value) { + this.dataModel.put (key, value); + } public void create(String filenName) throws ConfigurationException, IOException { Writer out = new StringWriter(); diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/TableContent.java b/pdf/src/main/java/de/muehlencord/shared/pdf/TableContent.java index 09fff8e..745214d 100644 --- a/pdf/src/main/java/de/muehlencord/shared/pdf/TableContent.java +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/TableContent.java @@ -2,9 +2,11 @@ package de.muehlencord.shared.pdf; import com.google.gson.annotations.Expose; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.apache.pdfbox.pdmodel.font.PDFont; /** * @@ -13,22 +15,58 @@ import org.apache.pdfbox.pdmodel.common.PDRectangle; public class TableContent extends Content { @Expose - Font headerFont; + private final Font headerFont; @Expose - List header; + private List header; + @Expose + private List colSizes = null; @Expose - List> data; + private List> data = null; + - public TableContent(PDFDocument doc, int x, int y) { + public TableContent(PDFDocument doc, Font hf, int x, int y) { super(doc, x, y); + this.headerFont = hf; + } + + public void setHeader (List headers, List cols) { + header = new ArrayList<>(); + headers.stream().forEach((cellText) -> { + header.add (new Text (cellText)); + }); + colSizes = new ArrayList<>(); + cols.stream().forEach((col) -> { + colSizes.add (col); + }); + } + + public void addLine (List values) { + if (data == null) { + data = new ArrayList<>(); + } + values.stream().forEach((cellText) -> { + header.add (new Text (cellText)); + }); } @Override protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException { cos.beginText(); - cos.newLineAtOffset(x,y); + + PDFont hFont = document.getFont(headerFont.getFontName()); + + cos.endText(); +// cos.showText("Telefon:"); +// cos.newLineAtOffset(tabSize, 0); +// cos.showText("05257 / 1234567"); +// cos.newLineAtOffset(tabSize * -1, -14); // leading +// cos.showText("Fax:"); +// cos.newLineAtOffset(tabSize, 0); +// cos.showText("05257 / 1234567"); +// cos.newLineAtOffset(tabSize * -1, -14); // leading + } /* *** getter / setter *** */ @@ -36,8 +74,4 @@ public class TableContent extends Content { return headerFont; } - public void setHeaderFont(Font headerFont) { - this.headerFont = headerFont; - } - } diff --git a/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java b/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java index 9dfaf62..17785a9 100644 --- a/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java +++ b/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java @@ -1,29 +1,43 @@ package de.muehlencord.shared.pdf; import com.google.gson.Gson; +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateExceptionHandler; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import org.apache.commons.io.FileUtils; import org.junit.BeforeClass; +import org.junit.FixMethodOrder; import org.junit.Test; /** * * @author jomu */ +@FixMethodOrder public class PDFDocumentTest { private static Gson gson; + private String jsonString = null; + @BeforeClass public static void setUpClass() { gson = GsonUtil.getGsonInstance(); } @Test - public void testToJson() { + public void testToJson() throws FileNotFoundException, IOException, ConfigurationException { System.out.println("testToJson"); PDFDocument doc = new PDFDocument(); doc.addFont("bold", new Font("Helvetica-Bold", 12, 2)); doc.addFont("helv12", new Font("Helvetica", 12, 2)); - + doc.setPaperSize(PaperSize.A4); TextContent addressContent = new TextContent(doc, 40, 692, "Max Mustermann") .addLine("Musterstraße 123") @@ -36,14 +50,43 @@ public class PDFDocumentTest { .addLine("Wir danken für den Auftrag und bitten um Erledigung der folgenden Anzeigenabrechnung"); doc.addContent(invoiceInfoInformation); - TextContent informationContent = new TextContent(doc, 400, 662); - informationContent.addLine ("Anzeigenabrechnung", "bold"); - informationContent.addLine ("Veronika Mühlencord", "helv12"); - informationContent.addLine ("Telefon: 05257/940154", "helv12"); - informationContent.addLine ("Telefax: 05257/940156", "helv12"); - doc.addContent (informationContent); + TextContent informationContent = new TextContent(doc, 400, 662); + informationContent.addLine("Anzeigenabrechnung", "bold"); + informationContent.addLine("Veronika Mühlencord", "helv12"); + informationContent.addLine("Telefon: 05257/940154", "helv12"); + informationContent.addLine("Telefax: 05257/940156", "helv12"); + informationContent.addLine(); + informationContent.addLine("Hövelhof, den ${invoiceDate?date}", "bold"); + doc.addContent(informationContent); + + + TableContent informationContent2 = new TableContent (doc, doc.getStandardFont(), 400, 500); + List headers = new ArrayList<>(); + headers.add("Kunden-Nr"); + headers.add("${customer.customerNumber}"); + List colSize = new ArrayList<>(); + colSize.add (100); + colSize.add (100); + informationContent2.setHeader(headers, colSize); - System.out.println(gson.toJson(doc)); + jsonString = gson.toJson(doc); + System.out.println(jsonString); + + File file = new File("c:/temp/test.ftlh"); + FileUtils.writeStringToFile(file, jsonString, "UTF-8"); + + + // create pdf + Configuration cfg = new Configuration(Configuration.VERSION_2_3_24); + cfg.setDirectoryForTemplateLoading(new File("c:/temp")); + cfg.setDefaultEncoding("UTF-8"); + cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); + cfg.setLogTemplateExceptions(false); + + Template template = cfg.getTemplate("test.ftlh"); + PDFTemplate pdfDoc = new PDFTemplate(template); + pdfDoc.addToDatamodel("invoiceDate", new Date()); + pdfDoc.create("c:/temp/test.pdf"); } } diff --git a/pdf/src/test/java/de/muehlencord/shared/pdf/PDFTemplateTest.java b/pdf/src/test/java/de/muehlencord/shared/pdf/PDFTemplateTest.java deleted file mode 100644 index 660324a..0000000 --- a/pdf/src/test/java/de/muehlencord/shared/pdf/PDFTemplateTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package de.muehlencord.shared.pdf; - -import freemarker.template.Configuration; -import freemarker.template.Template; -import freemarker.template.TemplateExceptionHandler; -import java.io.File; -import java.io.IOException; -import org.junit.Test; - -/** - * - * @author jomu - */ -public class PDFTemplateTest { - - @Test - // @Ignore // TODO get template from test resources - public void testCreate() throws IOException, ConfigurationException { - Configuration cfg = new Configuration(Configuration.VERSION_2_3_24); - cfg.setDirectoryForTemplateLoading(new File("c:/temp")); - cfg.setDefaultEncoding("UTF-8"); - cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); - cfg.setLogTemplateExceptions(false); - - Template template = cfg.getTemplate("test.ftlh"); - - PDFTemplate doc = new PDFTemplate(template); - doc.create ("c:/temp/test.pdf"); - } - -}