updated unit tests
added datamodel support
This commit is contained in:
@ -63,5 +63,9 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -33,6 +33,10 @@ public class PDFTemplate {
|
||||
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();
|
||||
try {
|
||||
|
||||
@ -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<Object> header;
|
||||
private List<Text> header;
|
||||
@Expose
|
||||
private List<Integer> colSizes = null;
|
||||
|
||||
@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);
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,24 +1,38 @@
|
||||
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));
|
||||
@ -37,13 +51,42 @@ public class PDFDocumentTest {
|
||||
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);
|
||||
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);
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user