started API clean up

This commit is contained in:
jomu
2016-06-15 10:42:27 +00:00
parent a9da425681
commit 1cd84cfa3b
25 changed files with 495 additions and 318 deletions

View File

@ -0,0 +1,14 @@
package de.muehlencord.shared.pdf;
/**
*
* @author joern.muehlencord
*/
public abstract class CellValue {
public abstract int getColSize();
public abstract float getCellPadding();
}

View File

@ -9,7 +9,7 @@ import org.apache.pdfbox.pdmodel.common.PDRectangle;
*
* @author jomu
*/
public abstract class Content {
abstract class Content {
protected PDFDocument document;
protected PDRectangle rect;

View File

@ -4,7 +4,7 @@ package de.muehlencord.shared.pdf;
*
* @author joern.muehlencord
*/
public class Coordinate {
class Coordinate {
final float x;
final float y;

View File

@ -8,10 +8,13 @@ import java.util.List;
*
* @author joern.muehlencord
*/
public class DefaultTableRow extends TableRow {
class DefaultTableRow extends TableRow {
@Expose
private final List<Text> row;
private final List<TextElement> row;
@Expose
private final List<Float> padding;
@Expose
private Boolean isList;
@ -22,50 +25,73 @@ public class DefaultTableRow extends TableRow {
@Expose
private String varName;
public DefaultTableRow() {
protected DefaultTableRow() {
this.row = new ArrayList<>();
this.padding = new ArrayList<>();
this.isList = false;
this.listName = null;
this.varName = null;
}
@Override
protected void addColumn(TextElement element) {
row.add(element);
padding.add(null);
}
public void add(Text text) {
row.add(text);
@Override
protected void addColumn(TextElement element, Float p) {
row.add(element);
padding.add(p);
}
/* *** TableRow methods *** */
@Override
public int getColumnCount() {
protected int getColumnCount() {
return row.size();
}
@Override
public Text getColumnValue(int columnPos) {
return row.get(columnPos);
protected String getColumnValue(int columnPos) {
return row.get(columnPos).getText();
}
@Override
public boolean isList() {
protected PDFTextAlignment getAlignment(int columnPos) {
return row.get(columnPos).getAlign();
}
@Override
protected Float getCellPadding(int columnPos) {
Float currentPadding = padding.get(columnPos);
if (currentPadding == null) {
return 0F;
} else {
return currentPadding ;
}
}
@Override
protected boolean isList() {
return isList;
}
@Override
public void createList(String listName, String varName) {
protected DefaultTableRow createList(String listName, String varName) {
this.listName = listName;
this.varName = varName;
this.isList = true;
return this;
}
@Override
public String getListName() {
protected String getListName() {
return listName;
}
@Override
public String getVarName() {
protected String getVarName() {
return varName;
}
}

View File

@ -7,7 +7,7 @@ import com.google.gson.GsonBuilder;
*
* @author jomu
*/
public class GsonUtil {
class GsonUtil {
protected final static Gson getGsonInstance() {
return new GsonBuilder()
@ -15,6 +15,7 @@ public class GsonUtil {
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(Content.class, new InterfaceAdapter<>())
.registerTypeAdapter(TableRow.class, new InterfaceAdapter<>())
.registerTypeAdapter(CellValue.class, new InterfaceAdapter<>())
.create();
}

View File

@ -1,105 +0,0 @@
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
/**
*
* @author joern.muehlencord
*/
public class ImageContent extends Content {
@Expose
private Float scale = null;
@Expose
private String base64CodedImage = null;
public ImageContent(PDFDocument document, String contentString) throws IOException {
super(document);
this.base64CodedImage = contentString;
}
public ImageContent(PDFDocument document, File file) throws IOException {
super(document);
this.base64CodedImage = getEncodedString(ImageIO.read(file));
}
public ImageContent(PDFDocument document, BufferedImage img) throws IOException {
super(document);
this.base64CodedImage = getEncodedString(img);
}
public ImageContent(PDFDocument document, Float x, Float y, String contentString) throws IOException {
super(document, x, y);
this.base64CodedImage = contentString;
}
public ImageContent(PDFDocument document, Float x, Float y, File file) throws IOException {
super(document, x, y);
this.base64CodedImage = getEncodedString(ImageIO.read(file));
}
public ImageContent(PDFDocument document, Float x, Float y, BufferedImage img) throws IOException {
super(document, x, y);
this.base64CodedImage = getEncodedString(img);
}
public ImageContent(PDFDocument document, Float x, Float y, Float scale, String contentString) throws IOException {
super(document, x, y);
this.scale = scale;
this.base64CodedImage = contentString;
}
public ImageContent(PDFDocument document, Float x, Float y, Float scale, File file) throws IOException {
super(document, x, y);
this.scale = scale;
this.base64CodedImage = getEncodedString(ImageIO.read(file));
}
public ImageContent(PDFDocument document, Float x, Float y, Float scale, BufferedImage img) throws IOException {
super(document, x, y);
this.scale = scale;
this.base64CodedImage = getEncodedString(img);
}
@Override
protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
BufferedImage image = getImage();
PDImageXObject pdImage = LosslessFactory.createFromImage(document.getPdDocument(), image);
float height = pdImage.getHeight() * getScale();
float width = pdImage.getWidth() * getScale();
cos.drawImage(pdImage, x, y, width, height);
return new Coordinate(x, y - height);
}
private String getEncodedString(BufferedImage img) throws IOException {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(img, "png", Base64.getEncoder().wrap(os));
return os.toString(StandardCharsets.UTF_8.name());
}
private BufferedImage getImage() throws IOException {
return ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(this.base64CodedImage)));
}
/* *** getter / setter */
public Float getScale() {
if (scale == null) {
return 1F;
} else {
return scale;
}
}
}

View File

@ -13,7 +13,7 @@ import com.google.gson.JsonSerializer;
*
* @author jomu
*/
public class InterfaceAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {
class InterfaceAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {
@Override
public final JsonElement serialize(final T object, final Type interfaceType, final JsonSerializationContext context) {

View File

@ -4,7 +4,7 @@ package de.muehlencord.shared.pdf;
*
* @author joern.muehlencord
*/
public interface ListTemplate {
interface ListTemplate {
public void createList (String listName, String varName);

View File

@ -17,13 +17,13 @@ import org.apache.pdfbox.pdmodel.font.PDType1Font;
public class PDFDocument {
@Expose
private PaperSize paperSize;
private PDFPaperSize paperSize;
@Expose
protected Font standardFont;
protected PDFFont standardFont;
@Expose
protected Map<String, Font> fontMap;
protected Map<String, PDFFont> fontMap;
@Expose
private final List<Content> contentList;
@ -32,7 +32,7 @@ public class PDFDocument {
public PDFDocument() {
this.contentList = new ArrayList<>();
this.standardFont = new Font("Helvetica", 11);
this.standardFont = new PDFFont("Helvetica", 11);
this.fontMap = null;
}
@ -115,7 +115,7 @@ public class PDFDocument {
return templateString;
}
public void addFont(String name, Font font) {
public void addFont(String name, PDFFont font) {
if (fontMap == null) {
fontMap = new ConcurrentHashMap<>();
}
@ -132,7 +132,7 @@ public class PDFDocument {
}
}
public Font getFontByAlias(String fontAlias) throws ConfigurationException {
public PDFFont getFontByAlias(String fontAlias) throws ConfigurationException {
if ((fontMap != null) && (fontMap.containsKey(fontAlias))) {
return fontMap.get(fontAlias);
} else {
@ -146,19 +146,19 @@ public class PDFDocument {
}
/* *** getter / setter *** */
public PaperSize getPaperSize() {
public PDFPaperSize getPaperSize() {
return paperSize;
}
public void setPaperSize(PaperSize paperSize) {
public void setPaperSize(PDFPaperSize paperSize) {
this.paperSize = paperSize;
}
public Font getStandardFont() {
public PDFFont getStandardFont() {
return standardFont;
}
public void setStandardFont(Font standardFont) {
public void setStandardFont(PDFFont standardFont) {
this.standardFont = standardFont;
}

View File

@ -1,9 +0,0 @@
package de.muehlencord.shared.pdf;
/**
*
* @author joern.muehlencord
*/
public class PDFElement {
}

View File

@ -6,7 +6,7 @@ import com.google.gson.annotations.Expose;
*
* @author jomu
*/
public class Font {
public class PDFFont {
@Expose
private String fontName;
@ -15,13 +15,13 @@ public class Font {
@Expose
private int padding;
public Font(String fontName, int fontSize) {
public PDFFont(String fontName, int fontSize) {
this.fontName = fontName;
this.fontSize = fontSize;
this.padding = 2;
}
public Font(String fontName, int fontSize, int padding) {
public PDFFont(String fontName, int fontSize, int padding) {
this.fontName = fontName;
this.fontSize = fontSize;
this.padding = padding;

View File

@ -0,0 +1,91 @@
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
import de.muehlencord.shared.pdf.util.ImageUtil;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
/**
*
* @author joern.muehlencord
*/
public class PDFImageContent extends Content {
@Expose
private Float scale = null;
@Expose
private String base64CodedImage = null;
public PDFImageContent(PDFDocument document, String contentString) throws IOException {
super(document);
this.base64CodedImage = contentString;
}
public PDFImageContent(PDFDocument document, File file) throws IOException {
super(document);
this.base64CodedImage = ImageUtil.getEncodedString(ImageIO.read(file));
}
public PDFImageContent(PDFDocument document, BufferedImage img) throws IOException {
super(document);
this.base64CodedImage = ImageUtil.getEncodedString(img);
}
public PDFImageContent(PDFDocument document, Float x, Float y, String contentString) throws IOException {
super(document, x, y);
this.base64CodedImage = contentString;
}
public PDFImageContent(PDFDocument document, Float x, Float y, File file) throws IOException {
super(document, x, y);
this.base64CodedImage = ImageUtil.getEncodedString(ImageIO.read(file));
}
public PDFImageContent(PDFDocument document, Float x, Float y, BufferedImage img) throws IOException {
super(document, x, y);
this.base64CodedImage = ImageUtil.getEncodedString(img);
}
public PDFImageContent(PDFDocument document, Float x, Float y, Float scale, String contentString) throws IOException {
super(document, x, y);
this.scale = scale;
this.base64CodedImage = contentString;
}
public PDFImageContent(PDFDocument document, Float x, Float y, Float scale, File file) throws IOException {
super(document, x, y);
this.scale = scale;
this.base64CodedImage = ImageUtil.getEncodedString(ImageIO.read(file));
}
public PDFImageContent(PDFDocument document, Float x, Float y, Float scale, BufferedImage img) throws IOException {
super(document, x, y);
this.scale = scale;
this.base64CodedImage = ImageUtil.getEncodedString(img);
}
@Override
protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
BufferedImage image = ImageUtil.getImageFromEncodedString(base64CodedImage);
PDImageXObject pdImage = LosslessFactory.createFromImage(document.getPdDocument(), image);
float height = pdImage.getHeight() * getScale();
float width = pdImage.getWidth() * getScale();
cos.drawImage(pdImage, x, y, width, height);
return new Coordinate(x, y - height);
}
/* *** getter / setter */
public Float getScale() {
if (scale == null) {
return 1F;
} else {
return scale;
}
}
}

View File

@ -9,13 +9,13 @@ package de.muehlencord.shared.pdf;
*
* @author jomu
*/
public enum PaperSize {
public enum PDFPaperSize {
A4("A4");
private String label;
private PaperSize(String label) {
private PDFPaperSize(String label) {
this.label = label;
}

View File

@ -4,6 +4,7 @@ import com.google.gson.annotations.Expose;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
@ -14,12 +15,12 @@ import org.slf4j.LoggerFactory;
*
* @author jomu
*/
public class TableContent extends Content {
public class PDFTableContent extends Content {
private static final Logger LOGGER = LoggerFactory.getLogger(TableContent.class);
private static final Logger LOGGER = LoggerFactory.getLogger(PDFTableContent.class);
@Expose
private final Font headerFont;
private final PDFFont headerFont;
@Expose
private final TableHeader header;
@ -27,45 +28,69 @@ public class TableContent extends Content {
@Expose
private List<TableRow> data = null;
public TableContent(PDFDocument doc, Font hf) {
public PDFTableContent(PDFDocument doc, PDFFont hf) {
super(doc);
this.header = new TableHeader();
this.headerFont = hf;
this.data = new ArrayList<>();
this.data = new LinkedList<>();
}
public TableContent(PDFDocument doc, Font hf, int x, int y) {
public PDFTableContent(PDFDocument doc, PDFFont hf, int x, int y) {
super(doc, x, y);
this.header = new TableHeader();
this.headerFont = hf;
this.data = new ArrayList<>();
}
public DefaultTableRow addLine(String... values) {
return addLine(Arrays.asList(values));
public PDFTableContent addRow() {
DefaultTableRow newRow = new DefaultTableRow();
data.add(newRow);
return this;
}
public DefaultTableRow addLine(List<String> values) {
public PDFTableContent addListRow(String listName, String varName) {
DefaultTableRow newRow = new DefaultTableRow();
newRow.createList(listName, varName);
data.add(newRow);
return this;
}
public PDFTableContent addRow(String... values) throws ConfigurationException {
return addRow(Arrays.asList(values));
}
public PDFTableContent addRow(List<String> values) throws ConfigurationException {
DefaultTableRow newLine = new DefaultTableRow();
values.stream().forEach((cellText) -> {
newLine.add(new Text(cellText));
});
data.add(newLine);
return newLine;
for (String cellText : values) {
addColumn (cellText);
}
return this;
}
public DefaultTableRow addTextLine(Text... values) {
return addTextLine(Arrays.asList(values));
public PDFTableContent addColumn(String text) throws ConfigurationException {
if (data.isEmpty()) {
throw new ConfigurationException ("Need to call newRow first");
}
data.get(data.size()-1).addColumn(new TextElement (text));
return this;
}
public DefaultTableRow addTextLine(List<Text> values) {
DefaultTableRow newLine = new DefaultTableRow();
values.stream().forEach((text) -> {
newLine.add(text);
});
data.add(newLine);
return newLine;
public PDFTableContent addColumn(String text, PDFTextAlignment alignment) throws ConfigurationException {
if (data.isEmpty()) {
throw new ConfigurationException ("Need to call newRow first");
}
data.get(data.size()-1).addColumn(new TextElement (text, alignment));
return this;
}
public PDFTableContent addColumn(String text, PDFTextAlignment alignment, Float padding) throws ConfigurationException {
if (data.isEmpty()) {
throw new ConfigurationException ("Need to call newRow first");
}
data.get(data.size()-1).addColumn(new TextElement (text, alignment), padding);
return this;
}
protected TableRow getRow(int no) {
@ -93,21 +118,22 @@ public class TableContent extends Content {
cos.setFont(hFont, headerFont.getFontSize());
cos.newLineAtOffset(x, y);
for (int i = 0; i < header.size(); i++) {
Text currentHeader = header.getHeader(i);
String headerText = header.getHeader(i);
PDFTextAlignment currentAlignment = header.getAlignment(i);
float cellPadding = header.getCellPadding(i);
float startX;
if (currentHeader.getAlign() == TextAlignment.RIGHT) {
if (currentAlignment == PDFTextAlignment.RIGHT) {
float textWdith = (standardFont.getStringWidth(currentHeader.getText()) / 1000F) * document.getStandardFont().getFontSize();
float width = header.getColumnSize(i) - 2 * cellPadding;
float textWdith = (standardFont.getStringWidth(headerText) / 1000F) * document.getStandardFont().getFontSize();
float width = header.getColumnSize(i) - (2F * cellPadding);
startX = width - textWdith;
cos.newLineAtOffset(startX, 0);
} else {
startX = 0;
}
cos.showText(currentHeader.getText());
cos.showText(headerText);
cos.newLineAtOffset(header.getColumnSize(i) - startX, 0);
}
if (data.isEmpty()) {
@ -120,22 +146,23 @@ public class TableContent extends Content {
cos.newLineAtOffset(xOffSet, yOffset);
currentY += yOffset;
for (int colNo = 0; colNo < currentRow.getColumnCount(); colNo++) {
Text currentColText = currentRow.getColumnValue(colNo);
float cellPadding = header.getCellPadding(colNo);
String currentColText = currentRow.getColumnValue(colNo);
PDFTextAlignment currentColAlignment = currentRow.getAlignment(colNo);
float cellPadding = currentRow.getCellPadding(colNo);
float startX;
// FIXME duplication with header and textContent object
if (currentColText.getAlign() == TextAlignment.RIGHT) {
float textWdith = (standardFont.getStringWidth(currentColText.getText()) / 1000F) * document.getStandardFont().getFontSize();
float width = header.getColumnSize(colNo) - 2 * cellPadding;
if (currentColAlignment == PDFTextAlignment.RIGHT) {
float textWdith = (standardFont.getStringWidth(currentColText) / 1000F) * document.getStandardFont().getFontSize();
float width = header.getColumnSize(colNo) - (2F * cellPadding);
startX = width - textWdith;
LOGGER.info("Text width for {} = {}", currentColText.getText(), textWdith);
LOGGER.info("Text width for {} = {}", currentColText, textWdith);
cos.newLineAtOffset(startX, 0);
} else {
startX = 0;
}
cos.showText(currentColText.getText());
cos.showText(currentColText);
cos.newLineAtOffset(header.getColumnSize(colNo) - startX, 0);
}
}
@ -147,7 +174,7 @@ public class TableContent extends Content {
}
/* *** getter / setter *** */
public Font getHeaderFont() {
public PDFFont getHeaderFont() {
return headerFont;
}

View File

@ -3,12 +3,10 @@ package de.muehlencord.shared.pdf;
import com.google.gson.Gson;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
@ -67,7 +65,7 @@ public class PDFTemplate {
Coordinate coord = null;
for (Content content : pdfDoc.getContentList()) {
content.setDocument(pdfDoc); // FIXME move to serialization
content.setRect (page.getMediaBox());
content.setRect(page.getMediaBox());
content.setCoordinate(coord);
coord = content.addContentToPdf(cos);
}

View File

@ -4,7 +4,7 @@ package de.muehlencord.shared.pdf;
*
* @author joern.muehlencord
*/
public enum TextAlignment {
public enum PDFTextAlignment {
LEFT,
RIGHT;

View File

@ -11,44 +11,44 @@ import org.apache.pdfbox.pdmodel.font.PDFont;
*
* @author jomu
*/
public class TextContent extends Content {
public class PDFTextContent extends Content {
@Expose
private final List<Text> textLines;
private final List<TextElement> textLines;
public TextContent(PDFDocument doc) {
public PDFTextContent(PDFDocument doc) {
super(doc);
this.textLines = new LinkedList<>();
}
public TextContent(PDFDocument doc, Float x, Float y) {
public PDFTextContent(PDFDocument doc, Float x, Float y) {
super(doc, x, y);
this.textLines = new LinkedList<>();
}
public TextContent(PDFDocument doc, Float x, Float y, String text) {
public PDFTextContent(PDFDocument doc, Float x, Float y, String text) {
super(doc, x, y);
this.textLines = new LinkedList<>();
this.textLines.add(new Text(text));
this.textLines.add(new TextElement(text));
}
public TextContent addLine() {
this.textLines.add(new Text());
public PDFTextContent addLine() {
this.textLines.add(new TextElement());
return this;
}
public TextContent addLine(String text) {
this.textLines.add(new Text(text));
public PDFTextContent addLine(String text) {
this.textLines.add(new TextElement(text));
return this;
}
public TextContent addLine(String text, TextAlignment align) {
this.textLines.add(new Text(text, align));
public PDFTextContent addLine(String text, PDFTextAlignment align) {
this.textLines.add(new TextElement(text, align));
return this;
}
public TextContent addLine(String text, String fontAlias) {
this.textLines.add(new Text(text, fontAlias));
public PDFTextContent addLine(String text, String fontAlias) {
this.textLines.add(new TextElement(text, fontAlias));
return this;
}
@ -59,13 +59,13 @@ public class TextContent extends Content {
cos.beginText();
cos.newLineAtOffset(x, y);
float currentY = y;
for (Text textLine : textLines) {
Font font = (textLine.getFontAlias() == null ? document.getStandardFont() : document.getFontByAlias(textLine.getFontAlias()));
for (TextElement textLine : textLines) {
PDFFont font = (textLine.getFontAlias() == null ? document.getStandardFont() : document.getFontByAlias(textLine.getFontAlias()));
PDFont pdFont = document.getFont(font.getFontName());
cos.setFont(pdFont, font.getFontSize());
int leading = font.getFontSize() + font.getPadding();
if (textLine.getAlign() == TextAlignment.RIGHT) {
if (textLine.getAlign() == PDFTextAlignment.RIGHT) {
float textWdith = (pdFont.getStringWidth(textLine.getText()) / 1000F) * font.getFontSize();
float width = rect.getUpperRightX() - rect.getLowerLeftX() - 2 * margin;
float startX = width - textWdith;

View File

@ -11,72 +11,64 @@ import java.util.List;
public class TableHeader {
@Expose
private final List<Text> headers;
@Expose
private final List<Integer> colSizes;
@Expose
private final List<Float> cellPadding;
private final List<CellValue> headerCells;
public TableHeader() {
headers = new ArrayList<>();
colSizes = new ArrayList<>();
cellPadding = new ArrayList<>();
}
public TableHeader add(Text header, int colSize) {
headers.add(header);
colSizes.add(colSize);
cellPadding.add(null);
return this;
headerCells = new ArrayList<>();
}
public TableHeader add(String headerText, int colSize) {
headers.add(new Text(headerText));
colSizes.add(colSize);
cellPadding.add(null);
headerCells.add (new TextCellValue (headerText, colSize));
return this;
}
public TableHeader add(String headerText, int colSize, Float padding) {
headers.add(new Text(headerText));
colSizes.add(colSize);
cellPadding.add(padding);
headerCells.add (new TextCellValue (headerText, colSize));
return this;
}
public TableHeader add(String headerText, int colSize, TextAlignment align) {
headers.add(new Text(headerText, align));
colSizes.add(colSize);
cellPadding.add(null);
public TableHeader add(String headerText, int colSize, PDFTextAlignment align) {
headerCells.add (new TextCellValue (headerText, colSize, align));
return this;
}
public TableHeader add(String headerText, int colSize, TextAlignment align, float padding) {
headers.add(new Text(headerText, align));
colSizes.add(colSize);
cellPadding.add(padding);
public TableHeader add(String headerText, int colSize, PDFTextAlignment align, float padding) {
headerCells.add (new TextCellValue (headerText, colSize, align, padding));
return this;
}
/* *** getter *** */
public int size() {
return headers.size();
return headerCells.size();
}
public int getColumnSize(int pos) {
return colSizes.get(pos);
return headerCells.get(pos).getColSize();
}
public Text getHeader(int pos) {
return headers.get(pos);
}
public Float getCellPadding(int pos) {
if (cellPadding.get(pos) == null) {
return 0F;
public String getHeader(int pos) throws ConfigurationException {
CellValue header = headerCells.get(pos);
if (header instanceof TextCellValue) {
TextCellValue textHeader = (TextCellValue) header;
return textHeader.getCellText();
} else {
return cellPadding.get(pos);
return "Unsupported";
}
}
public PDFTextAlignment getAlignment (int pos) {
CellValue header = headerCells.get(pos);
if (header instanceof TextCellValue) {
TextCellValue textHeader = (TextCellValue) header;
return textHeader.getAlignment();
} else {
return null;
}
}
public Float getCellPadding(int pos) {
return headerCells.get(pos).getCellPadding();
}
}

View File

@ -4,18 +4,27 @@ package de.muehlencord.shared.pdf;
*
* @author joern.muehlencord
*/
public abstract class TableRow {
abstract class TableRow {
public abstract int getColumnCount();
protected abstract TableRow createList(String listName, String varName);
public abstract void createList(String listName, String varName);
protected abstract int getColumnCount();
public abstract boolean isList();
protected abstract boolean isList();
public abstract Text getColumnValue(int columnPos);
protected abstract String getColumnValue(int columnPos);
public abstract String getListName();
protected abstract PDFTextAlignment getAlignment(int columnPos);
protected abstract Float getCellPadding (int columnPos);
protected abstract String getListName();
protected abstract String getVarName();
protected abstract void addColumn(TextElement element);
protected abstract void addColumn(TextElement element, Float padding);
public abstract String getVarName();
}

View File

@ -0,0 +1,65 @@
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
/**
*
* @author joern.muehlencord
*/
public class TextCellValue extends CellValue {
@Expose
private final TextElement cellValue;
@Expose
private final Integer colSize;
@Expose
private final Float cellPadding;
public TextCellValue(String cellValue, int colSizes) {
this.cellValue = new TextElement(cellValue);
this.colSize = colSizes;
this.cellPadding = null;
}
public TextCellValue(String cellValue, int colSizes, float cellPadding) {
this.cellValue = new TextElement(cellValue);
this.colSize = colSizes;
this.cellPadding = cellPadding;
}
public TextCellValue(String cellValue, int colSizes, PDFTextAlignment align) {
this.cellValue = new TextElement(cellValue, align);
this.colSize = colSizes;
this.cellPadding = null;
}
public TextCellValue(String cellValue, int colSize, PDFTextAlignment align, float cellPadding) {
this.cellValue = new TextElement(cellValue, align);
this.colSize = colSize;
this.cellPadding = cellPadding;
}
/* *** getter / setter *** */
public String getCellText() {
return cellValue.getText();
}
public PDFTextAlignment getAlignment() {
return cellValue.getAlign();
}
@Override
public int getColSize() {
return colSize;
}
@Override
public float getCellPadding() {
if (cellPadding == null) {
return 0F;
} else {
return cellPadding;
}
}
}

View File

@ -6,40 +6,40 @@ import com.google.gson.annotations.Expose;
*
* @author joern.muehlencord
*/
public class Text extends PDFElement {
class TextElement {
@Expose
private final String text;
@Expose
private final String fontAlias;
@Expose
private TextAlignment align;
private final PDFTextAlignment align;
public Text() {
public TextElement() {
this.text = "";
this.fontAlias = null;
this.align = null;
}
public Text(String text) {
public TextElement(String text) {
this.text = text;
this.fontAlias = null;
this.align = null;
}
public Text(String text, TextAlignment align) {
public TextElement(String text, PDFTextAlignment align) {
this.text = text;
this.fontAlias = null;
this.align = align;
}
public Text(String text, String fontAlias) {
public TextElement(String text, String fontAlias) {
this.text = text;
this.fontAlias = fontAlias;
this.align = null;
}
public Text(String text, String fontAlias, TextAlignment align) {
public TextElement(String text, String fontAlias, PDFTextAlignment align) {
this.text = text;
this.fontAlias = fontAlias;
this.align = align;
@ -54,9 +54,9 @@ public class Text extends PDFElement {
return fontAlias;
}
public TextAlignment getAlign() {
public PDFTextAlignment getAlign() {
if (align == null) {
return TextAlignment.LEFT;
return PDFTextAlignment.LEFT;
} else {
return align;
}

View File

@ -0,0 +1,27 @@
package de.muehlencord.shared.pdf.util;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.imageio.ImageIO;
/**
*
* @author joern.muehlencord
*/
public class ImageUtil {
public static String getEncodedString(BufferedImage img) throws IOException {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(img, "png", Base64.getEncoder().wrap(os));
return os.toString(StandardCharsets.UTF_8.name());
}
public static BufferedImage getImageFromEncodedString(String base64CodedString) throws IOException {
return ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(base64CodedString)));
}
}

View File

@ -1,6 +1,7 @@
package de.muehlencord.shared.pdf;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
@ -11,6 +12,11 @@ public class Invoice {
private final List<InvoiceLine> invoiceLines;
private Date invoiceDate;
private String customerNumber;
private String invoiceNumber;
private String logo = null;
public Invoice() {
this.invoiceLines = new ArrayList<>();
}
@ -19,7 +25,40 @@ public class Invoice {
this.invoiceLines.add(il);
}
/* *** getter / setter *** */
public List<InvoiceLine> getInvoiceLines() {
return invoiceLines;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public Date getInvoiceDate() {
return invoiceDate;
}
public void setInvoiceDate(Date invoiceDate) {
this.invoiceDate = invoiceDate;
}
public String getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(String customerNumber) {
this.customerNumber = customerNumber;
}
public String getInvoiceNumber() {
return invoiceNumber;
}
public void setInvoiceNumber(String invoiceNumber) {
this.invoiceNumber = invoiceNumber;
}
}

View File

@ -1,5 +1,6 @@
package de.muehlencord.shared.pdf;
import de.muehlencord.shared.pdf.util.ImageUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
@ -26,70 +27,67 @@ public class PDFDocumentTest {
public void testToJson() throws FileNotFoundException, IOException, ConfigurationException, TemplateException {
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.addFont("bold", new PDFFont("Helvetica-Bold", 12, 2));
doc.addFont("helv12", new PDFFont("Helvetica", 12, 2));
doc.setPaperSize(PaperSize.A4);
TextContent addressContent = new TextContent(doc, 40F, 692F, "Max Mustermann")
doc.setPaperSize(PDFPaperSize.A4);
PDFTextContent addressContent = new PDFTextContent(doc, 40F, 692F, "Max Mustermann")
.addLine("Musterstraße 123")
.addLine("12345 Musterhausen");
doc.addContent(addressContent);
String fileName = "c:/temp/logo-verkehrsverein-hoevelh.jpg";
BufferedImage image = ImageIO.read(new File(fileName));
ImageContent logoContent = new ImageContent(doc, 400F, 700F, 0.6F, image);
PDFImageContent logoContent = new PDFImageContent(doc, 400F, 700F, 0.6F, "${invoice.logo}");
doc.addContent(logoContent);
TextContent informationContent = new TextContent(doc, 400F, 662F);
PDFTextContent informationContent = new PDFTextContent(doc, 400F, 662F);
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");
informationContent.addLine("Hövelhof, den ${invoice.invoiceDate?date}", "bold");
doc.addContent(informationContent);
TableContent informationContent2 = new TableContent(doc, doc.getStandardFont());
PDFTableContent informationContent2 = new PDFTableContent(doc, doc.getStandardFont());
informationContent2.getHeaders()
.add("Kunden-Nr", 100)
.add("${customerNumber}", 100);
informationContent2.addLine("Rechnungs-Nr.:", "${invoiceNumber}");
informationContent2.addLine("Ausgabe: ", "Dezember");
informationContent2.addLine("Rechnungsdatum:", "${invoiceDate?date}");
.add("${invoice.customerNumber}", 100);
informationContent2.addRow("Rechnungs-Nr.:", "${invoice.invoiceNumber}")
.addRow("Ausgabe: ", "Dezember")
.addRow("Rechnungsdatum:", "${invoice.invoiceDate?date}");
doc.addContent(informationContent2);
TextContent invoiceInfoInformation = new TextContent(doc, 40F, 442F, "Sehr geehrter Anzeigenkunde, ")
PDFTextContent invoiceInfoInformation = new PDFTextContent(doc, 40F, 442F, "Sehr geehrter Anzeigenkunde, ")
.addLine()
.addLine()
.addLine("Wir danken für den Auftrag und bitten um Erledigung der folgenden Anzeigenabrechnung")
.addLine();
doc.addContent(invoiceInfoInformation);
TableContent invoiceLines = new TableContent(doc, doc.getFontByAlias("bold"));
PDFTableContent invoiceLines = new PDFTableContent(doc, doc.getFontByAlias("bold"));
invoiceLines.getHeaders()
.add("Menge", 50, 10F)
.add("Beschreibung", 300, 10F)
.add(new Text("Einzelpreis", TextAlignment.RIGHT), 80)
.add(new Text("Summe", TextAlignment.RIGHT), 80);
invoiceLines.addTextLine(
new Text("1000", TextAlignment.RIGHT),
new Text("Anzeige Hövelhofer Rundschau"),
new Text("10,00 €", TextAlignment.RIGHT),
new Text("10,00 €", TextAlignment.RIGHT));
invoiceLines.addTextLine(
new Text("${invoiceline.amount}", TextAlignment.RIGHT),
new Text("${invoiceline.description}"),
new Text("${invoiceline.price}", TextAlignment.RIGHT),
new Text("${invoiceline.total}", TextAlignment.RIGHT)).createList("invoiceLines", "invoiceline");
invoiceLines.addTextLine(
new Text("2", TextAlignment.RIGHT),
new Text("Anzeige Hövelhofer Rundschau"),
new Text("10,00 €", TextAlignment.RIGHT),
new Text("20,00 €", TextAlignment.RIGHT));
.add("Menge", 50)
.add("Beschreibung", 300)
.add("Einzelpreis", 80, PDFTextAlignment.RIGHT)
.add("Summe", 80, PDFTextAlignment.RIGHT);
invoiceLines.addRow()
.addColumn("1000", PDFTextAlignment.RIGHT, 5F)
.addColumn("Anzeige Hövelhofer Rundschau")
.addColumn("10,00 €", PDFTextAlignment.RIGHT)
.addColumn("10,00 €", PDFTextAlignment.RIGHT);
invoiceLines.addListRow("invoice.invoiceLines", "invoiceline")
.addColumn("${invoiceline.amount}", PDFTextAlignment.RIGHT, 5F )
.addColumn("${invoiceline.description}")
.addColumn("${invoiceline.price}", PDFTextAlignment.RIGHT)
.addColumn("${invoiceline.total}", PDFTextAlignment.RIGHT);
invoiceLines.addRow()
.addColumn("2", PDFTextAlignment.RIGHT, 5F)
.addColumn("Anzeige Hövelhofer Rundschau")
.addColumn("10,00 €", PDFTextAlignment.RIGHT)
.addColumn("20,00 €", PDFTextAlignment.RIGHT);
doc.addContent(invoiceLines);
TextContent test = new TextContent(doc)
PDFTextContent test = new PDFTextContent(doc)
.addLine("Das ist ein Test");
doc.addContent(test);
@ -109,14 +107,18 @@ public class PDFDocumentTest {
PDFTemplate pdfDoc = new PDFTemplate(template);
Invoice invoice = new Invoice();
invoice.setInvoiceDate(new Date());
invoice.setCustomerNumber("8755");
invoice.setInvoiceNumber("1234567");
invoice.addInvoiceLine(new InvoiceLine("Product 1", "10,00 €", "1", "10,00 €"));
invoice.addInvoiceLine(new InvoiceLine("Product 2", "5,00 €", "10", "50,00 €"));
invoice.addInvoiceLine(new InvoiceLine("Product 3", "100,00 €", "20", "2000,00 €"));
pdfDoc.addToDatamodel("invoiceDate", new Date());
pdfDoc.addToDatamodel("customerNumber", "8755");
pdfDoc.addToDatamodel("invoiceNumber", "1234567");
pdfDoc.addToDatamodel("invoiceLines", invoice.getInvoiceLines());
String fileName = "c:/temp/logo-verkehrsverein-hoevelh.jpg";
BufferedImage image = ImageIO.read(new File(fileName));
invoice.setLogo(ImageUtil.getEncodedString(image));
pdfDoc.addToDatamodel("invoice", invoice);
pdfDoc.create("c:/temp/test.pdf");
}

View File

@ -18,7 +18,7 @@ public class TextTest {
+ "\"text\": \"Rechnungs-Nr.:\"\n"
+ "}";
Text text = GsonUtil.getGsonInstance().fromJson(jsonString, Text.class);
TextElement text = GsonUtil.getGsonInstance().fromJson(jsonString, TextElement.class);
assertNotNull ("text object", text);
assertEquals ("text value", "Rechnungs-Nr.:", text.getText());