aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas <>2021-12-17 14:13:07 +0100
committerThomas <>2021-12-17 18:18:28 +0100
commit45f1112cbde2c17b2dba8ec9adf50e304126e49c (patch)
treef32cd4e60e30cdfebee68833714fae6ea9074e37
parentfa7b8df2f05078c98ccd950db82b92027904a266 (diff)
downloadeinfach-signieren-45f1112cbde2c17b2dba8ec9adf50e304126e49c.tar.gz
einfach-signieren-45f1112cbde2c17b2dba8ec9adf50e304126e49c.tar.bz2
einfach-signieren-45f1112cbde2c17b2dba8ec9adf50e304126e49c.zip
switch from iText to pdfBox
-rw-r--r--simpleSigning/build.gradle14
-rw-r--r--simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java148
2 files changed, 57 insertions, 105 deletions
diff --git a/simpleSigning/build.gradle b/simpleSigning/build.gradle
index 02dae00..3ce5286 100644
--- a/simpleSigning/build.gradle
+++ b/simpleSigning/build.gradle
@@ -34,14 +34,14 @@ dependencies {
implementation group: 'org.apache.logging.log4j', name: 'log4j-to-slf4j', version: '2.16.0'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.8'
implementation "commons-codec:commons-codec:1.9"
- //compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '1.8.5'
- implementation ('com.lowagie:itext:2.1.7') {
- exclude group: 'bouncycastle'
- }
+ implementation group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.24'
+ //implementation ('com.lowagie:itext:2.1.7') {
+ // exclude group: 'bouncycastle'
+ //}
implementation 'com.thetransactioncompany:cors-filter:2.10'
- implementation group: 'org.bouncycastle', name: 'bcmail-jdk15on', version: '1.70'
- implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.70'
- implementation group: 'org.bouncycastle', name: 'bctsp-jdk15on', version: '1.46'
+ //implementation group: 'org.bouncycastle', name: 'bcmail-jdk15on', version: '1.70'
+ //implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.70'
+ //implementation group: 'org.bouncycastle', name: 'bctsp-jdk15on', version: '1.46'
compileOnly 'javax.servlet:javax.servlet-api:3.1.0'
testImplementation group: 'junit', name: 'junit', version: '4.+'
}
diff --git a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java
index 9c94dac..85d64e7 100644
--- a/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java
+++ b/simpleSigning/src/main/java/at/gv/egiz/simpleSigning/helper/PDFHelper.java
@@ -7,14 +7,14 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.lowagie.text.Document;
-import com.lowagie.text.PageSize;
-import com.lowagie.text.Paragraph;
-import com.lowagie.text.pdf.PdfWriter;
-
import at.gv.egiz.simpleSigning.ErrorSignature;
public class PDFHelper {
@@ -22,112 +22,64 @@ public class PDFHelper {
private static final Logger logger = LoggerFactory
.getLogger(PDFHelper.class);
- private static byte[] createPDFiText(String text) {
- try {
- Document document = new Document(PageSize.A4, 36, 72, 108, 180);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- PdfWriter.getInstance(document,baos);
- document.open();
- document.add(new Paragraph(text));
- System.out.println("Text is inserted into pdf file");
- document.close();
- baos.close();
- return baos.toByteArray();
- } catch(Throwable e) {
- logger.error("Failed to create PDF", e);
- }
- return null;
- }
+// private static byte[] createPDFiText(String text) {
+// try {
+// Document document = new Document(PageSize.A4, 36, 72, 108, 180);
+// ByteArrayOutputStream baos = new ByteArrayOutputStream();
+// PdfWriter.getInstance(document,baos);
+// document.open();
+// document.add(new Paragraph(text));
+// System.out.println("Text is inserted into pdf file");
+// document.close();
+// baos.close();
+// return baos.toByteArray();
+// } catch(Throwable e) {
+// logger.error("Failed to create PDF", e);
+// }
+// return null;
+// }
- /*
private static byte[] createPDFPdfBox(String text) {
- PDDocument document = null;
-
- try {
+ try {
// Create a document and add a page to it
- document = new PDDocument();
+ PDDocument document = new PDDocument();
PDPage page = new PDPage();
- document.addPage(page);
+ document.addPage( page );
+ // Create a new font object selecting one of the PDF base fonts
+ PDFont font = PDType1Font.HELVETICA_BOLD;
+
+ // Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page);
-
- PDFont pdfFont = PDType1Font.HELVETICA;
- float fontSize = 25;
- float leading = 1.5f * fontSize;
-
- PDRectangle mediabox = page.findMediaBox();
- float margin = 72;
- float width = mediabox.getWidth() - 2*margin;
- float startX = mediabox.getLowerLeftX() + margin;
- float startY = mediabox.getUpperRightY() - margin;
-
- List<String> lines = new ArrayList<String>();
- int lastSpace = -1;
- while (text.length() > 0)
- {
- int spaceIndex = text.indexOf(' ', lastSpace + 1);
- if (spaceIndex < 0)
- {
- lines.add(text);
- text = "";
- }
- else
- {
- String subString = text.substring(0, spaceIndex);
- float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
- if (size > width)
- {
- if (lastSpace < 0) // So we have a word longer than the line... draw it anyways
- lastSpace = spaceIndex;
- subString = text.substring(0, lastSpace);
- lines.add(subString);
- text = text.substring(lastSpace).trim();
- lastSpace = -1;
- }
- else
- {
- lastSpace = spaceIndex;
- }
- }
- }
-
- contentStream.beginText();
- contentStream.setFont(pdfFont, fontSize);
- contentStream.moveTextPositionByAmount(startX, startY);
- for (String line: lines)
- {
- contentStream.drawString(line);
- contentStream.moveTextPositionByAmount(0, -leading);
- }
- contentStream.endText();
-
-
-
+
+ // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
+ contentStream.beginText();
+ contentStream.setFont( font, 12 );
+ contentStream.newLineAtOffset(100, 700);
+ contentStream.showText(text);
+ contentStream.endText();
+
// Make sure that the content stream is closed:
contentStream.close();
+
+ // Save the result into byte array
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ document.save(byteArrayOutputStream);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- // Save the results and ensure that the document is properly closed:
- document.save(baos);
+ document.close();
+
+ return byteArrayOutputStream.toByteArray();
- return baos.toByteArray();
- } catch (Throwable e) {
- logger.error("Failed to create PDF", e);
- } finally {
- if(document != null) {
- try {
- document.close();
- } catch (Throwable e) {
- logger.error("Failed to close PDF", e);
- }
- }
- }
- return null;
+ } catch (Exception e) {
+ logger.error("Failed to create PDF", e);
+ return null;
+ }
}
- */
+
public static byte[] createPDFDocument(String text) {
- return createPDFiText(text);
+ return createPDFPdfBox(text);
+
}
public static void toError(HttpServletRequest req,