updated unit tests

added datamodel support
This commit is contained in:
jomu
2016-05-05 22:22:16 +00:00
parent 238d46c118
commit 77e783f77a
5 changed files with 103 additions and 49 deletions

View File

@ -63,5 +63,9 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -33,6 +33,10 @@ public class PDFTemplate {
this.dataModel = new ConcurrentHashMap<>(); this.dataModel = new ConcurrentHashMap<>();
} }
public void addToDatamodel (String key, Object value) {
this.dataModel.put (key, value);
}
public void create(String filenName) throws ConfigurationException, IOException { public void create(String filenName) throws ConfigurationException, IOException {
Writer out = new StringWriter(); Writer out = new StringWriter();
try { try {

View File

@ -2,9 +2,11 @@ package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle; 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 { public class TableContent extends Content {
@Expose @Expose
Font headerFont; private final Font headerFont;
@Expose @Expose
List<Object> header; private List<Text> header;
@Expose
private List<Integer> colSizes = null;
@Expose @Expose
List<List<Object>> data; private List<List<Text>> data = null;
public TableContent(PDFDocument doc, int x, int y) {
public TableContent(PDFDocument doc, Font hf, int x, int y) {
super(doc, x, y); super(doc, x, y);
this.headerFont = hf;
}
public void setHeader (List<String> headers, List<Integer> 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<String> values) {
if (data == null) {
data = new ArrayList<>();
}
values.stream().forEach((cellText) -> {
header.add (new Text (cellText));
});
} }
@Override @Override
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException { protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
cos.beginText(); 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 *** */ /* *** getter / setter *** */
@ -36,8 +74,4 @@ public class TableContent extends Content {
return headerFont; return headerFont;
} }
public void setHeaderFont(Font headerFont) {
this.headerFont = headerFont;
}
} }

View File

@ -1,24 +1,38 @@
package de.muehlencord.shared.pdf; package de.muehlencord.shared.pdf;
import com.google.gson.Gson; 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.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
/** /**
* *
* @author jomu * @author jomu
*/ */
@FixMethodOrder
public class PDFDocumentTest { public class PDFDocumentTest {
private static Gson gson; private static Gson gson;
private String jsonString = null;
@BeforeClass @BeforeClass
public static void setUpClass() { public static void setUpClass() {
gson = GsonUtil.getGsonInstance(); gson = GsonUtil.getGsonInstance();
} }
@Test @Test
public void testToJson() { public void testToJson() throws FileNotFoundException, IOException, ConfigurationException {
System.out.println("testToJson"); System.out.println("testToJson");
PDFDocument doc = new PDFDocument(); PDFDocument doc = new PDFDocument();
doc.addFont("bold", new Font("Helvetica-Bold", 12, 2)); doc.addFont("bold", new Font("Helvetica-Bold", 12, 2));
@ -37,13 +51,42 @@ public class PDFDocumentTest {
doc.addContent(invoiceInfoInformation); doc.addContent(invoiceInfoInformation);
TextContent informationContent = new TextContent(doc, 400, 662); TextContent informationContent = new TextContent(doc, 400, 662);
informationContent.addLine ("Anzeigenabrechnung", "bold"); informationContent.addLine("Anzeigenabrechnung", "bold");
informationContent.addLine ("Veronika Mühlencord", "helv12"); informationContent.addLine("Veronika Mühlencord", "helv12");
informationContent.addLine ("Telefon: 05257/940154", "helv12"); informationContent.addLine("Telefon: 05257/940154", "helv12");
informationContent.addLine ("Telefax: 05257/940156", "helv12"); informationContent.addLine("Telefax: 05257/940156", "helv12");
doc.addContent (informationContent); informationContent.addLine();
informationContent.addLine("Hövelhof, den ${invoiceDate?date}", "bold");
doc.addContent(informationContent);
System.out.println(gson.toJson(doc));
TableContent informationContent2 = new TableContent (doc, doc.getStandardFont(), 400, 500);
List<String> headers = new ArrayList<>();
headers.add("Kunden-Nr");
headers.add("${customer.customerNumber}");
List<Integer> colSize = new ArrayList<>();
colSize.add (100);
colSize.add (100);
informationContent2.setHeader(headers, colSize);
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");
} }
} }

View File

@ -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");
}
}