removed pdf library

This commit is contained in:
2019-09-15 13:37:30 +02:00
parent 6348f81bed
commit af298c8060
44 changed files with 1 additions and 2838 deletions

View File

@ -1,66 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.muehlencord</groupId>
<artifactId>shared</artifactId>
<version>1.2-SNAPSHOT</version>
</parent>
<groupId>de.muehlencord.shared</groupId>
<artifactId>shared-pdf</artifactId>
<version>1.2-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,29 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
abstract class CellValue {
protected abstract int getColSize();
protected abstract float getCellPadding();
}

View File

@ -1,40 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
class ColumnDefinition {
@Expose
private final Float width;
protected ColumnDefinition(Float width) {
this.width = width;
}
/* *** getter *** */
protected float getWidth() {
return width;
}
}

View File

@ -1,52 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class ConfigurationException extends Exception {
/**
* Creates a new instance of <code>ConfigurationException</code> without
* detail message.
*/
public ConfigurationException() {
}
/**
* Constructs an instance of <code>ConfigurationException</code> with the
* specified detail message.
*
* @param msg the detail message.
*/
public ConfigurationException(String msg) {
super(msg);
}
/**
* Constructs an instance of <code>ConfigurationException</code> with the
* specified detail message and the given root cause.
*
* @param msg the detail message.
* @param th the root cause.
*/
public ConfigurationException(String msg, Throwable th) {
super(msg);
}
}

View File

@ -1,91 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
abstract class Content implements TemplateSerializeable {
protected PDFDocument document;
protected PDRectangle rect;
@Expose
protected Float x;
@Expose
protected Float y;
public Content(PDFDocument document) {
this.document = document;
this.x = null;
this.y = null;
}
public Content(PDFDocument doc, float x, float y) {
this(doc);
this.x = x;
this.y = y;
}
public void setDocument(PDFDocument doc) {
this.document = doc;
}
public void setCoordinate(Coordinate coord) {
if (x == null) {
x = coord.getX();
}
if (y == null) {
y = coord.getY();
}
}
protected abstract Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException;
public abstract String getTemplateJsonString();
/* *** getter / setter *** */
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
protected PDRectangle getRectangle() {
return rect;
}
protected void setRect(PDRectangle rect) {
this.rect = rect;
}
}

View File

@ -1,39 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
class Coordinate {
final float x;
final float y;
public Coordinate(float x, float y) {
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
}

View File

@ -1,144 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
class DefaultTableRow extends TableRow implements TemplateSerializeable {
@Expose
private final List<TextElement> row;
@Expose
private final List<Float> padding;
@Expose
private Boolean isList;
@Expose
private String listName;
@Expose
private String varName;
protected DefaultTableRow() {
this.row = new ArrayList<>();
this.padding = new ArrayList<>();
this.isList = false;
this.listName = null;
this.varName = null;
}
@Override
public String getTemplateJsonString() {
// copy object, null values for listName and isList so they are no longer printed in the template
DefaultTableRow newRow = new DefaultTableRow ();
newRow.row.addAll(this.row);
newRow.padding.addAll(this.padding);
newRow.isList = null;
newRow.listName = null;
newRow.varName = null;
String returnValue = GsonUtil.getInstance().toJson(newRow);
if (isList()) {
String listStartString = "<#list ";
listStartString += getListName();
listStartString += " as ";
listStartString += getVarName();
listStartString += ">\n";
String listEndString = "<#if (" + getVarName() + "?has_next)>,</#if>";
listEndString += "</#list>\n";
returnValue = listStartString + returnValue + listEndString;
}
return returnValue;
}
@Override
protected void addColumn(TextElement element) {
row.add(element);
padding.add(null);
}
@Override
protected void addColumn(TextElement element, float p) {
row.add(element);
padding.add(p);
}
/* *** TableRow methods *** */
@Override
protected int getColumnCount() {
return row.size();
}
@Override
protected String getColumnValue(int columnPos) {
return row.get(columnPos).getText();
}
@Override
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 String getFontAlias(int columnPos) {
return row.get(columnPos).getFontAlias();
}
@Override
protected boolean isList() {
return isList;
}
@Override
protected DefaultTableRow createList(String listName, String varName) {
this.listName = listName;
this.varName = varName;
this.isList = true;
return this;
}
@Override
protected String getListName() {
return listName;
}
@Override
protected String getVarName() {
return varName;
}
}

View File

@ -1,40 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
class DefaultTableRowTypeAdapterFactory extends GsonCustomizedTypeAdapterFactory<DefaultTableRow> {
protected DefaultTableRowTypeAdapterFactory() {
super(DefaultTableRow.class);
}
@Override
protected JsonElement updateElement(JsonElement toSerialize) {
JsonObject elem = toSerialize.getAsJsonObject();
final JsonObject member = new JsonObject();
member.addProperty("type", DefaultTableRow.class.getName());
member.add("data", toSerialize.getAsJsonObject());
return member;
}
}

View File

@ -1,89 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
abstract class GsonCustomizedTypeAdapterFactory<C> implements TypeAdapterFactory {
protected final Class<C> customizedClass;
public GsonCustomizedTypeAdapterFactory(Class<C> customizedClass) {
this.customizedClass = customizedClass;
}
/**
* Override this to muck with {@code toSerialize} before it is written to
* the outgoing JSON stream.
*/
protected void beforeWrite(C source, JsonElement toSerialize) {
// do nothing in default implementation
}
/**
* Override this to muck with {@code deserialized} before it parsed into the
* application type.
*/
protected void afterRead(JsonElement deserialized) {
// do nothing in default implementation
}
protected JsonElement updateElement(JsonElement toSerialize) {
// do nothing in default implementation
return toSerialize;
}
@Override
public final <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
TypeAdapter<T> returnValue = null;
if (customizedClass.isAssignableFrom(type.getRawType())) {
returnValue = (TypeAdapter<T>) customizeMyClassAdapter(gson, (TypeToken<C>) type);
}
return returnValue;
}
private TypeAdapter<C> customizeMyClassAdapter(Gson gson, TypeToken<C> type) {
final TypeAdapter<C> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<C>() {
@Override
public void write(JsonWriter out, C value) throws IOException {
JsonElement tree = delegate.toJsonTree(value);
tree = updateElement(tree);
beforeWrite(value, tree);
elementAdapter.write(out, tree);
}
@Override
public C read(JsonReader in) throws IOException {
JsonElement tree = elementAdapter.read(in);
afterRead(tree);
return delegate.fromJsonTree(tree);
}
};
}
}

View File

@ -1,45 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
class GsonUtil {
private final static Gson INSTANCE = new GsonBuilder()
.setPrettyPrinting()
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(Content.class, new InterfaceAdapter<>())
.registerTypeAdapter(TableRow.class, new InterfaceAdapter<>())
.registerTypeAdapter(CellValue.class, new InterfaceAdapter<>())
.registerTypeAdapterFactory(new PDFImageContentTypeAdapterFactory())
.registerTypeAdapterFactory(new PDFTableContentTypeAdapterFactory())
.registerTypeAdapterFactory(new PDFTextContentTypeAdapterFactory())
.registerTypeAdapterFactory(new DefaultTableRowTypeAdapterFactory())
.registerTypeAdapterFactory(new TextCellValueTypeAdapterFactory())
.create();
protected final static Gson getInstance() {
return INSTANCE;
}
}

View File

@ -1,69 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
class InterfaceAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {
@Override
public final JsonElement serialize(final T object, final Type interfaceType, final JsonSerializationContext context) {
final JsonObject member = new JsonObject();
member.addProperty("type", object.getClass().getName());
member.add("data", context.serialize(object, object.getClass()));
return member;
}
@Override
public final T deserialize(final JsonElement elem, final Type interfaceType, final JsonDeserializationContext context) throws JsonParseException {
final JsonObject member = (JsonObject) elem;
final JsonElement typeString = get(member, "type");
final JsonElement data = get(member, "data");
final Type actualType = typeForName(typeString);
return context.deserialize(data, actualType);
}
private Type typeForName(final JsonElement typeElem) {
try {
return Class.forName(typeElem.getAsString());
} catch (ClassNotFoundException e) {
throw new JsonParseException(e);
}
}
private JsonElement get(final JsonObject wrapper, final String memberName) {
final JsonElement elem = wrapper.get(memberName);
if (elem == null) {
throw new JsonParseException(
"no '" + memberName + "' member found in json file.");
}
return elem;
}
}

View File

@ -1,156 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class PDFDocument {
@Expose
private final PDFPaperSize paperSize;
@Expose
protected PDFFont standardFont;
@Expose
protected Map<String, PDFFont> fontMap;
@Expose
private final List<Content> contentList;
private PDDocument pdDocument;
public PDFDocument(PDFPaperSize ps) {
this.paperSize = ps;
this.contentList = new ArrayList<>();
this.standardFont = new PDFFont("Helvetica", 11);
this.fontMap = null;
}
public String getTemplateString() {
StringBuilder sb = new StringBuilder();
int indent = 0; // TODO add indent support
sb.append("{\n");
// paper size
sb.append("\"paperSize\": ");
sb.append(paperSize.getTemplateJsonString());
sb.append(",\n");
// standardFOnt
sb.append ("\"standardFont\": ");
sb.append (standardFont.getTemplateJsonString());
sb.append (",\n");
sb.append("\"fontMap\": {\n");
Iterator<Map.Entry<String, PDFFont>> it = fontMap.entrySet().iterator();
while (it.hasNext()) {
Entry<String, PDFFont> entry = it.next();
sb.append("\"").append(entry.getKey()).append("\": ");
sb.append (entry.getValue().getTemplateJsonString());
if (it.hasNext()) {
sb.append(",");
}
sb.append("\n");
}
sb.append ("},\n"); // fontMap
sb.append ("\"contentList\": [\n");
Iterator<Content> contentIterator = contentList.iterator();
while (contentIterator.hasNext()) {
Content content = contentIterator.next();
String contentString = content.getTemplateJsonString();
sb.append (contentString);
if (contentIterator.hasNext())
sb.append (",");
sb.append ("\n");
}
sb.append ("]\n");
sb.append("}"); // closing pdfDocument
return sb.toString();
}
public void addFont(String name, PDFFont font) {
if (fontMap == null) {
fontMap = new ConcurrentHashMap<>();
}
fontMap.put(name, font);
}
protected PDFont getFont(String fontName) throws ConfigurationException {
if (fontName.equals(PDType1Font.HELVETICA.getBaseFont())) {
return PDType1Font.HELVETICA;
} else if (fontName.equals(PDType1Font.HELVETICA_BOLD.getBaseFont())) {
return PDType1Font.HELVETICA_BOLD;
} else {
throw new ConfigurationException("Font " + fontName + " not supported");
}
}
public PDFFont getFontByAlias(String fontAlias) throws ConfigurationException {
if ((fontMap != null) && (fontMap.containsKey(fontAlias))) {
return fontMap.get(fontAlias);
} else {
throw new ConfigurationException("Font " + fontAlias + " not found in mapping. ");
}
}
public PDFDocument addContent(Content content) {
contentList.add(content);
return this;
}
/* *** getter / setter *** */
public PDFPaperSize getPaperSize() {
return paperSize;
}
public PDFFont getStandardFont() {
return standardFont;
}
public void setStandardFont(PDFFont standardFont) {
this.standardFont = standardFont;
}
public List<Content> getContentList() {
return contentList;
}
public PDDocument getPdDocument() {
return pdDocument;
}
public void setPdDocument(PDDocument pdDocument) {
this.pdDocument = pdDocument;
}
}

View File

@ -1,78 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class PDFFont implements TemplateSerializeable {
@Expose
private String fontName;
@Expose
private int fontSize;
@Expose
private int padding;
public PDFFont(String fontName, int fontSize) {
this.fontName = fontName;
this.fontSize = fontSize;
this.padding = 2;
}
public PDFFont(String fontName, int fontSize, int padding) {
this.fontName = fontName;
this.fontSize = fontSize;
this.padding = padding;
}
@Override
public String getTemplateJsonString() {
return GsonUtil.getInstance().toJson (this);
}
/* *** getter / setter *** */
public String getFontName() {
return fontName;
}
public void setFontName(String fontName) {
this.fontName = fontName;
}
public int getFontSize() {
return fontSize;
}
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
}
public int getPadding() {
return padding;
}
public void setPadding(int padding) {
this.padding = padding;
}
}

View File

@ -1,118 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 (joern@muehlencord.de)
*/
public class PDFImageContent extends Content implements TemplateSerializeable {
@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);
}
/**
* defaut toJson implementation uses standard JSON string
* @return
*/
@Override
public String getTemplateJsonString() {
String returnValue = GsonUtil.getInstance().toJson(this);
return returnValue;
}
@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

@ -1,38 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
class PDFImageContentTypeAdapterFactory extends GsonCustomizedTypeAdapterFactory<PDFImageContent> {
protected PDFImageContentTypeAdapterFactory() {
super(PDFImageContent.class);
}
@Override
protected JsonElement updateElement(JsonElement toSerialize) {
final JsonObject member = new JsonObject();
member.addProperty("type", PDFImageContent.class.getName());
member.add("data", toSerialize.getAsJsonObject());
return member;
}
}

View File

@ -1,43 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public enum PDFPaperSize implements TemplateSerializeable {
A4("A4");
private String label;
private PDFPaperSize(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
@Override
public String getTemplateJsonString() {
return GsonUtil.getInstance().toJson(this);
}
}

View File

@ -1,254 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.Gson;
import com.google.gson.annotations.Expose;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class PDFTableContent extends Content {
private static final Logger LOGGER = LoggerFactory.getLogger(PDFTableContent.class);
@Expose
private final List<ColumnDefinition> columSizes;
@Expose
private List<TableRow> data = null;
public PDFTableContent(PDFDocument doc, PDFFont hf) {
super(doc);
this.columSizes = new ArrayList<>();
this.data = new ArrayList<>();
}
public PDFTableContent(PDFDocument doc, PDFFont hf, float x, float y) {
super(doc, x, y);
this.columSizes = new ArrayList<>();
this.data = new ArrayList<>();
}
/**
* defaut toJson implementation uses standard JSON string
*
* @return
*/
@Override
public String getTemplateJsonString() {
Gson gson = GsonUtil.getInstance();
StringBuilder sb = new StringBuilder();
sb.append ("{\n");
sb.append ("\"type\": ");
sb.append ("\"").append(this.getClass().getName()).append ("\",\n");
sb.append ("\"data\": {");
sb.append ("\"columSizes\": ");
sb.append (gson.toJson(columSizes));
sb.append (",\"data\": [");
Iterator<TableRow> it = data.iterator();
while (it.hasNext()) {
TableRow row = it.next();
sb.append (row.getTemplateJsonString());
if (it.hasNext()) {
sb.append (",\n");
}
}
sb.append ("]\n"); // data array
sb.append ("}\n"); // data list
sb.append ("}\n"); // pdf table object
return sb.toString();
// return GsonUtil.getInstance().toJson(this);
}
public PDFTableContent addColumn(float size) {
// TODO check if enough columns are defined
this.columSizes.add(new ColumnDefinition(size));
return this;
}
public PDFTableContent addColumn(float... sizes) {
// TODO check if enough columns are defined
for (float size : sizes) {
addColumn(size);
}
return this;
}
public PDFTableContent addRow() {
DefaultTableRow newRow = new DefaultTableRow();
data.add(newRow);
return this;
}
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 PDFTableContent.this.addRow(Arrays.asList(values));
}
public PDFTableContent addRow(List<String> values) throws ConfigurationException {
DefaultTableRow newRow = new DefaultTableRow();
data.add(newRow);
if (values.size() != columSizes.size()) {
throw new ConfigurationException("Table defines " + columSizes.size() + " columns, but " + values.size() + " columns given");
}
validateConfiguration(newRow, false);
for (String cellText : values) {
PDFTableContent.this.setCellValue(cellText);
}
return this;
}
public PDFTableContent setCellValue(String text) throws ConfigurationException {
TableRow lastRow = data.get(data.size() - 1);
validateConfiguration(lastRow, true);
lastRow.addColumn(new TextElement(text));
return this;
}
public PDFTableContent setCellValue(String text, String fontAlias) throws ConfigurationException {
TableRow lastRow = data.get(data.size() - 1);
validateConfiguration(lastRow, true);
data.get(data.size() - 1).addColumn(new TextElement(text, fontAlias));
return this;
}
public PDFTableContent setCellValue(String text, float padding) throws ConfigurationException {
TableRow lastRow = data.get(data.size() - 1);
validateConfiguration(lastRow, true);
lastRow.addColumn(new TextElement(text), padding);
return this;
}
public PDFTableContent setCellValue(String text, String fontAlias, float padding) throws ConfigurationException {
TableRow lastRow = data.get(data.size() - 1);
validateConfiguration(lastRow, true);
lastRow.addColumn(new TextElement(text, fontAlias), padding);
return this;
}
public PDFTableContent setCellValue(String text, PDFTextAlignment alignment) throws ConfigurationException {
TableRow lastRow = data.get(data.size() - 1);
validateConfiguration(lastRow, true);
lastRow.addColumn(new TextElement(text, alignment));
return this;
}
public PDFTableContent setCellValue(String text, String fontAlias, PDFTextAlignment alignment) throws ConfigurationException {
if (data.isEmpty()) {
throw new ConfigurationException("Need to call newRow first");
}
data.get(data.size() - 1).addColumn(new TextElement(text, fontAlias, alignment));
return this;
}
public PDFTableContent setCellValue(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;
}
@Override
protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
cos.beginText();
int xOffSet = 0;
for (int i = 0; i < columSizes.size(); i++) {
xOffSet -= columSizes.get(i).getWidth();
}
int yOffset = document.getStandardFont().getFontSize() * -1 - document.getStandardFont().getPadding();
float currentX = x;
float currentY = y;
cos.newLineAtOffset(x, y);
for (int lineNo = 0; lineNo < data.size(); lineNo++) {
TableRow currentRow = data.get(lineNo);
for (int colNo = 0; colNo < currentRow.getColumnCount(); colNo++) {
String currentColText = currentRow.getColumnValue(colNo);
PDFTextAlignment currentColAlignment = currentRow.getAlignment(colNo);
float cellPadding = currentRow.getCellPadding(colNo);
float colWidth = columSizes.get(colNo).getWidth();
String fontAlias = currentRow.getFontAlias(colNo);
PDFFont font;
int fontSize;
if (fontAlias != null) {
font = document.getFontByAlias(fontAlias);
fontSize = document.getFontByAlias(fontAlias).getFontSize();
} else {
font = document.getStandardFont();
fontSize = document.getStandardFont().getFontSize();
}
PDFont pdFont = document.getFont(font.getFontName());
cos.setFont(pdFont, fontSize);
float startX;
// FIXME duplication with header and textContent object
if (currentColAlignment == PDFTextAlignment.RIGHT) {
float textWdith = (pdFont.getStringWidth(currentColText) / 1000F) * fontSize;
float width = colWidth - (2F * cellPadding);
startX = width - textWdith;
LOGGER.info("Text width for {} = {}", currentColText, textWdith);
cos.newLineAtOffset(startX, 0);
} else {
startX = 0;
}
cos.showText(currentColText);
cos.newLineAtOffset(columSizes.get(colNo).getWidth() - startX, 0);
}
cos.newLineAtOffset(xOffSet, yOffset);
currentY += yOffset;
}
currentY += yOffset;
cos.endText();
return new Coordinate(currentX, currentY);
}
private void validateConfiguration(TableRow lastRow, boolean calledFromSetCellValue) throws ConfigurationException {
if (data.isEmpty()) {
throw new ConfigurationException("Need to call newRow first");
}
if (calledFromSetCellValue) {
if (lastRow.getColumnCount() >= columSizes.size()) {
throw new ConfigurationException("Cannot add more columns than defined");
}
}
}
}

View File

@ -1,38 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
class PDFTableContentTypeAdapterFactory extends GsonCustomizedTypeAdapterFactory<PDFTableContent> {
protected PDFTableContentTypeAdapterFactory() {
super(PDFTableContent.class);
}
@Override
protected JsonElement updateElement(JsonElement toSerialize) {
final JsonObject member = new JsonObject();
member.addProperty("type", PDFTableContent.class.getName());
member.add("data", toSerialize.getAsJsonObject());
return member;
}
}

View File

@ -1,90 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.Gson;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class PDFTemplate {
private static final Logger LOGGER = LoggerFactory.getLogger(PDFTemplate.class);
public final static String A4 = "A4";
private final Template template;
private final ConcurrentHashMap<String, Object> dataModel;
public PDFTemplate(Template template) {
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();
try {
template.process(dataModel, out);
} catch (TemplateException ex) {
throw new IOException("Error while processing template", ex);
}
String json = out.toString();
LOGGER.debug(json);
Gson gson = GsonUtil.getInstance();
PDFDocument pdfDoc = gson.fromJson(json, PDFDocument.class);
pdfDoc.setPdDocument(new PDDocument());
PDPage page;
switch (pdfDoc.getPaperSize()) {
case A4:
page = new PDPage(PDRectangle.A4);
break;
default:
throw new ConfigurationException("Papersize " + pdfDoc.getPaperSize().getLabel() + " not supported");
}
pdfDoc.getPdDocument().addPage(page);
try (PDPageContentStream cos = new PDPageContentStream(pdfDoc.getPdDocument(), page, AppendMode.APPEND, false)) {
Coordinate coord = new Coordinate(page.getMediaBox().getLowerLeftX(), page.getMediaBox().getUpperRightY());
for (Content content : pdfDoc.getContentList()) {
content.setDocument(pdfDoc); // FIXME move to serialization
content.setRect(page.getMediaBox());
content.setCoordinate(coord);
coord = content.addContentToPdf(cos);
}
}
pdfDoc.getPdDocument().save(filenName);
}
}

View File

@ -1,26 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public enum PDFTextAlignment {
LEFT,
RIGHT;
}

View File

@ -1,109 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class PDFTextContent extends Content {
@Expose
private final List<TextElement> textLines;
public PDFTextContent(PDFDocument doc) {
super(doc);
this.textLines = new LinkedList<>();
}
public PDFTextContent(PDFDocument doc, float x, float y) {
super(doc, x, y);
this.textLines = new LinkedList<>();
}
public PDFTextContent(PDFDocument doc, float x, float y, String text) {
super(doc, x, y);
this.textLines = new LinkedList<>();
this.textLines.add(new TextElement(text));
}
/**
* defaut toJson implementation uses standard JSON string
*
* @return
*/
@Override
public String getTemplateJsonString() {
String returnValue = GsonUtil.getInstance().toJson(this);
return returnValue;
}
public PDFTextContent addLine() {
this.textLines.add(new TextElement());
return this;
}
public PDFTextContent addLine(String text) {
this.textLines.add(new TextElement(text));
return this;
}
public PDFTextContent addLine(String text, PDFTextAlignment align) {
this.textLines.add(new TextElement(text, align));
return this;
}
public PDFTextContent addLine(String text, String fontAlias) {
this.textLines.add(new TextElement(text, fontAlias));
return this;
}
/* *** getter / setter */
@Override
protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
float margin = 40F;
cos.beginText();
cos.newLineAtOffset(x, y);
float currentY = y;
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() == PDFTextAlignment.RIGHT) {
float textWdith = (pdFont.getStringWidth(textLine.getText()) / 1000F) * font.getFontSize();
float width = rect.getUpperRightX() - rect.getLowerLeftX() - 2 * margin;
float startX = width - textWdith;
cos.newLineAtOffset(startX, 0);
}
currentY -= leading;
cos.setLeading(leading);
cos.showText(textLine.getText());
cos.newLine();
}
cos.endText();
return new Coordinate(x, currentY);
}
}

View File

@ -1,38 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
class PDFTextContentTypeAdapterFactory extends GsonCustomizedTypeAdapterFactory<PDFTextContent> {
protected PDFTextContentTypeAdapterFactory() {
super(PDFTextContent.class);
}
@Override
protected JsonElement updateElement(JsonElement toSerialize) {
final JsonObject member = new JsonObject();
member.addProperty("type", PDFTextContent.class.getName());
member.add("data", toSerialize.getAsJsonObject());
return member;
}
}

View File

@ -1,49 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
abstract class TableRow implements TemplateSerializeable {
protected abstract TableRow createList(String listName, String varName);
protected abstract int getColumnCount();
protected abstract boolean isList();
protected abstract String getColumnValue(int columnPos);
protected abstract PDFTextAlignment getAlignment(int columnPos);
protected abstract float getCellPadding (int columnPos);
protected abstract String getFontAlias(int columnPos);
protected abstract String getListName();
protected abstract String getVarName();
protected abstract void addColumn(TextElement element);
protected abstract void addColumn(TextElement element, float padding);
}

View File

@ -1,40 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class TemplateException extends Exception {
/**
* Creates a new instance of <code>TemplateException</code> without detail
* message.
*/
public TemplateException() {
}
/**
* Constructs an instance of <code>TemplateException</code> with the
* specified detail message.
*
* @param msg the detail message.
*/
public TemplateException(String msg) {
super(msg);
}
}

View File

@ -1,26 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
interface TemplateSerializeable {
String getTemplateJsonString();
}

View File

@ -1,80 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
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

@ -1,38 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
class TextCellValueTypeAdapterFactory extends GsonCustomizedTypeAdapterFactory<TextCellValue> {
protected TextCellValueTypeAdapterFactory() {
super(TextCellValue.class);
}
@Override
protected JsonElement updateElement(JsonElement toSerialize) {
final JsonObject member = new JsonObject();
member.addProperty("type", TextCellValue.class.getName());
member.add("data", toSerialize.getAsJsonObject());
return member;
}
}

View File

@ -1,80 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
class TextElement {
@Expose
private final String text;
@Expose
private final String fontAlias;
@Expose
private final PDFTextAlignment align;
public TextElement() {
this.text = "";
this.fontAlias = null;
this.align = null;
}
public TextElement(String text) {
this.text = text;
this.fontAlias = null;
this.align = null;
}
public TextElement(String text, PDFTextAlignment align) {
this.text = text;
this.fontAlias = null;
this.align = align;
}
public TextElement(String text, String fontAlias) {
this.text = text;
this.fontAlias = fontAlias;
this.align = null;
}
public TextElement(String text, String fontAlias, PDFTextAlignment align) {
this.text = text;
this.fontAlias = fontAlias;
this.align = align;
}
/* *** getter / setter *** */
public String getText() {
return text;
}
public String getFontAlias() {
return fontAlias;
}
public PDFTextAlignment getAlign() {
if (align == null) {
return PDFTextAlignment.LEFT;
} else {
return align;
}
}
}

View File

@ -1,42 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 (joern@muehlencord.de)
*/
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,50 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.Test;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class DefaultTableRowTest {
@Test
public void testFromJson() {
String jsonString = "{\n"
+ " \"type\": \"de.muehlencord.shared.pdf.DefaultTableRow\",\n"
+ " \"data\": {\n"
+ " \"row\": [\n"
+ " {\n"
+ " \"text\": \"Rechnungs-Nr.:\"\n"
+ " },\n"
+ " {\n"
+ " \"text\": \"${invoiceNumber}\"\n"
+ " }\n"
+ " ],\n"
+ " \"isList\": false\n"
+ " }\n"
+ " }";
TableRow tableRow = GsonUtil.getInstance().fromJson(jsonString, TableRow.class);
assertFalse(tableRow == null, "tableRowObject");
assertEquals(2, tableRow.getColumnCount(), "column count");
assertFalse(tableRow.isList(), "isList");
}
}

View File

@ -1,79 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
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<>();
}
public void addInvoiceLine(InvoiceLine il) {
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,73 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class InvoiceLine {
private String description;
private String price;
private String amount;
private String total;
public InvoiceLine(String description, String price, String amount, String total) {
this.description = description;
this.price = price;
this.amount = amount;
this.total = total;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
}

View File

@ -1,39 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class PDFFontTest {
public PDFFontTest() {
}
@Test
public void testTemplateSerialisation() {
PDFFont font = new PDFFont("Test", 12, 0);
String gsonString = GsonUtil.getInstance().toJson(font);
String templateString = font.getTemplateJsonString();
System.out.println(templateString);
assertEquals(gsonString, templateString);
}
}

View File

@ -1,46 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import org.junit.jupiter.api.Test;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class PDFImageContentTest {
public PDFImageContentTest() {
}
@Test
public void testImage() throws IOException, URISyntaxException {
URL urlLogo = PDFImageContentTest.class.getResource("logo-verkehrsverein-hoevelh.jpg");
File imageFile = new File (urlLogo.toURI());
PDFDocument doc = new PDFDocument(PDFPaperSize.A4);
PDFImageContent logoContent = new PDFImageContent(doc, 400F, 700F, 0.6F, imageFile);
System.out.println(GsonUtil.getInstance().toJson(logoContent));
System.out.println(GsonUtil.getInstance().toJson(logoContent));
}
}

View File

@ -1,54 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.jupiter.api.Test;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class PDFTextContentTest {
@Test
public void testSomeMethod() {
PDFDocument doc = new PDFDocument(PDFPaperSize.A4);
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 ${invoice.invoiceDate?date}", "bold");
doc.addContent(informationContent);
// System.out.println (informationContent.getTemplateJsonString());
// System.out.println ("*****");
// System.out.println (GsonUtil.getGsonInstance().toJson(informationContent));
System.out.println("*****");
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new PDFImageContentTypeAdapterFactory())
.setPrettyPrinting()
.excludeFieldsWithoutExposeAnnotation()
.create();
System.out.println(gson.toJson(informationContent, PDFTextContent.class));
}
}

View File

@ -1,43 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.Test;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class TextTest {
public TextTest() {
}
@Test
public void testFromJson() {
String jsonString = "{\n"
+ "\"text\": \"Rechnungs-Nr.:\"\n"
+ "}";
TextElement text = GsonUtil.getInstance().fromJson(jsonString, TextElement.class);
assertFalse(text == null, "text object");
assertEquals("Rechnungs-Nr.:", text.getText(), "text value");
}
}

View File

@ -1,78 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf.test;
import de.muehlencord.shared.pdf.ConfigurationException;
import de.muehlencord.shared.pdf.PDFDocument;
import de.muehlencord.shared.pdf.PDFFont;
import de.muehlencord.shared.pdf.PDFPaperSize;
import de.muehlencord.shared.pdf.PDFTableContent;
import de.muehlencord.shared.pdf.PDFTemplate;
import de.muehlencord.shared.pdf.TemplateException;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class GsonSerialisation {
@Test
public void testSerialisation() throws ConfigurationException, TemplateException, IOException {
PDFDocument doc = new PDFDocument(PDFPaperSize.A4);
doc.addFont("helv12", new PDFFont("Helvetica", 12, 2));
PDFTableContent userList = new PDFTableContent(doc, doc.getFontByAlias("helv12"), 40F, 692F);
userList.addColumn(50, 300);
userList.addListRow("users", "user")
.setCellValue("${user.firstName}")
.setCellValue("${user.lastName}");
doc.addContent(userList);
// System.out.println ("*******");
// System.out.println (doc.toJson());
String jsonString = doc.getTemplateString();
String fileName = System.getProperty("java.io.tmpdir") + "/users.ftlh";
File file = new File(fileName);
FileUtils.writeStringToFile(file, jsonString, "UTF-8");
System.out.println("*******");
System.out.println(jsonString);
System.out.println("*******");
Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
cfg.setDirectoryForTemplateLoading(new File(System.getProperty("java.io.tmpdir")));
cfg.setDefaultEncoding("UTF-8");
Template template = cfg.getTemplate("users.ftlh");
PDFTemplate pdfDoc = new PDFTemplate(template);
List<User> users = new LinkedList<>();
users.add(new User("John", "Doe"));
users.add(new User("Jane", "Roe"));
pdfDoc.addToDatamodel("users", users);
fileName = System.getProperty("java.io.tmpdir") + "/users.pdf";
pdfDoc.create(fileName);
}
}

View File

@ -1,158 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf.test;
import de.muehlencord.shared.pdf.ConfigurationException;
import de.muehlencord.shared.pdf.Invoice;
import de.muehlencord.shared.pdf.InvoiceLine;
import de.muehlencord.shared.pdf.PDFDocument;
import de.muehlencord.shared.pdf.PDFFont;
import de.muehlencord.shared.pdf.PDFImageContent;
import de.muehlencord.shared.pdf.PDFPaperSize;
import de.muehlencord.shared.pdf.PDFTableContent;
import de.muehlencord.shared.pdf.PDFTemplate;
import de.muehlencord.shared.pdf.PDFTextAlignment;
import de.muehlencord.shared.pdf.PDFTextContent;
import de.muehlencord.shared.pdf.TemplateException;
import de.muehlencord.shared.pdf.util.ImageUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
@TestMethodOrder(OrderAnnotation.class)
public class PDFDocumentTest {
private String jsonString = null;
@Test
public void testToJson() throws FileNotFoundException, IOException, ConfigurationException, TemplateException, URISyntaxException {
System.out.println("testToJson");
PDFDocument doc = new PDFDocument(PDFPaperSize.A4);
doc.addFont("bold", new PDFFont("Helvetica-Bold", 12, 2));
doc.addFont("helv12", new PDFFont("Helvetica", 12, 2));
PDFTextContent addressContent = new PDFTextContent(doc, 40F, 692F, "Max Mustermann")
.addLine("Musterstraße 123")
.addLine("12345 Musterhausen");
doc.addContent(addressContent);
PDFImageContent logoContent = new PDFImageContent(doc, 400F, 700F, 0.6F, "${invoice.logo}");
doc.addContent(logoContent);
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 ${invoice.invoiceDate?date}", "bold");
doc.addContent(informationContent);
PDFTableContent informationContent2 = new PDFTableContent(doc, doc.getStandardFont());
informationContent2.addColumn(100, 100)
.addRow("Kunden-Nr", "${invoice.customerNumber}")
.addRow("Rechnungs-Nr.:", "${invoice.invoiceNumber}")
.addRow("Ausgabe: ", "Dezember")
.addRow("Rechnungsdatum:", "${invoice.invoiceDate?date}");
doc.addContent(informationContent2);
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);
PDFTableContent invoiceLines = new PDFTableContent(doc, doc.getFontByAlias("bold"));
invoiceLines.addColumn(50, 300, 80, 80);
invoiceLines.addRow()
.setCellValue("Menge", "bold", 10F)
.setCellValue("Beschreibung", "bold", 10F)
.setCellValue("Einzelpreis", "bold", PDFTextAlignment.RIGHT)
.setCellValue("Summe", "bold", PDFTextAlignment.RIGHT);
invoiceLines.addRow()
.setCellValue("1000", PDFTextAlignment.RIGHT, 5F)
.setCellValue("Anzeige Hövelhofer Rundschau")
.setCellValue("10,00 €", PDFTextAlignment.RIGHT)
.setCellValue("10,00 €", PDFTextAlignment.RIGHT);
invoiceLines.addListRow("invoice.invoiceLines", "invoiceline")
.setCellValue("${invoiceline.amount}", PDFTextAlignment.RIGHT, 5F)
.setCellValue("${invoiceline.description}")
.setCellValue("${invoiceline.price}", PDFTextAlignment.RIGHT)
.setCellValue("${invoiceline.total}", PDFTextAlignment.RIGHT);
invoiceLines.addRow()
.setCellValue("2", PDFTextAlignment.RIGHT, 5F)
.setCellValue("Anzeige Hövelhofer Rundschau")
.setCellValue("10,00 €", PDFTextAlignment.RIGHT)
.setCellValue("20,00 €", PDFTextAlignment.RIGHT);
doc.addContent(invoiceLines);
PDFTextContent test = new PDFTextContent(doc)
.addLine("Das ist ein Test");
doc.addContent(test);
jsonString = doc.getTemplateString();
String fileName = System.getProperty("java.io.tmpdir") + "/test.ftlh";
File file = new File(fileName);
FileUtils.writeStringToFile(file, jsonString, "UTF-8");
// create pdf
Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
cfg.setDirectoryForTemplateLoading(new File(System.getProperty("java.io.tmpdir")));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
Template template = cfg.getTemplate("test.ftlh");
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 €"));
URL urlLogo = PDFDocumentTest.class.getResource("logo-verkehrsverein-hoevelh.jpg");
File imageFile = new File (urlLogo.toURI());
BufferedImage image = ImageIO.read(imageFile);
invoice.setLogo(ImageUtil.getEncodedString(image));
pdfDoc.addToDatamodel("invoice", invoice);
fileName = System.getProperty("java.io.tmpdir") + "/test.pdf";
pdfDoc.create(fileName);
}
}

View File

@ -1,47 +0,0 @@
/*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.pdf.test;
import com.google.gson.annotations.Expose;
/**
*
* @author Joern Muehlencord (joern@muehlencord.de)
*/
public class User {
@Expose
private final String firstName;
@Expose
private final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.9 KiB

View File

@ -1,53 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="true">
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c] %m%n" />
</layout>
</appender>
<logger name="org.hibernate">
<level value="warn" />
</logger>
<logger name="org.gson">
<level value="DEBUG" />
</logger>
<category name="de.muehlencord">
<priority value="DEBUG"/>
</category>
<category name="com.sun">
<priority value="WARN"/>
</category>
<category name="javax.xml">
<priority value="WARN"/>
</category>
<root>
<level value="DEBUG" />
<appender-ref ref="consoleAppender" />
</root>
</log4j:configuration>

14
pom.xml
View File

@ -28,7 +28,6 @@ limitations under the License.
<module>network</module>
<module>util</module>
<module>jeeutil</module>
<module>pdf</module>
<module>shiro-faces</module>
<module>poi-util</module>
<module>db</module>
@ -202,18 +201,7 @@ limitations under the License.
<artifactId>hibernate-core</artifactId>
<version>5.3.10.Final</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>