diff --git a/pdf/src/main/java/de/muehlencord/shared/pdf/ImageContent.java b/pdf/src/main/java/de/muehlencord/shared/pdf/ImageContent.java index 2921cdf..da9e1cb 100644 --- a/pdf/src/main/java/de/muehlencord/shared/pdf/ImageContent.java +++ b/pdf/src/main/java/de/muehlencord/shared/pdf/ImageContent.java @@ -1,8 +1,16 @@ 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; /** @@ -13,29 +21,79 @@ public class ImageContent extends Content { @Expose private Float scale = null; - - public ImageContent(PDFDocument document) { + @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, Float x, Float y) { + 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, Float scale) { + 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 { - PDImageXObject pdImage = PDImageXObject.createFromFile("c:/temp/logo-verkehrsverein-hoevelh.jpg", document.getPdDocument()); + 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) { @@ -44,5 +102,4 @@ public class ImageContent extends Content { return scale; } } - } diff --git a/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java b/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java index d30cc15..9c4d221 100644 --- a/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java +++ b/pdf/src/test/java/de/muehlencord/shared/pdf/PDFDocumentTest.java @@ -3,10 +3,12 @@ package de.muehlencord.shared.pdf; 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.util.Date; +import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.junit.FixMethodOrder; import org.junit.Test; @@ -32,9 +34,12 @@ public class PDFDocumentTest { .addLine("Musterstraße 123") .addLine("12345 Musterhausen"); doc.addContent(addressContent); - - ImageContent logoContent = new ImageContent (doc, 400F,700F, 0.6F); - doc.addContent (logoContent); + + 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); + doc.addContent(logoContent); + TextContent informationContent = new TextContent(doc, 400F, 662F); informationContent.addLine("Anzeigenabrechnung", "bold");