diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2015-02-25 11:46:22 +0100 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2015-02-25 11:46:22 +0100 |
commit | af77eda98bfac090a5792672dd89e7f0e0475338 (patch) | |
tree | 1260a1e4cfbda0fc4783232c11ffeb161a24c1c7 /pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib | |
parent | 18a3495d083b8bfd90853c88ffe12c2ff21d0add (diff) | |
download | pdf-as-4-af77eda98bfac090a5792672dd89e7f0e0475338.tar.gz pdf-as-4-af77eda98bfac090a5792672dd89e7f0e0475338.tar.bz2 pdf-as-4-af77eda98bfac090a5792672dd89e7f0e0475338.zip |
allow signature profile image to be encoded base64 string
Diffstat (limited to 'pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib')
3 files changed, 54 insertions, 15 deletions
diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/IDGenerator.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/IDGenerator.java new file mode 100644 index 00000000..cdb7873b --- /dev/null +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/IDGenerator.java @@ -0,0 +1,5 @@ +package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox; + +public interface IDGenerator { + public String createHashedId(String value); +} diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java index 9c1adcec..de944d43 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java @@ -26,10 +26,12 @@ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.security.MessageDigest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -37,6 +39,9 @@ import java.util.Map; import javax.imageio.ImageIO; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.binary.Hex; +import org.apache.commons.io.IOUtils; import org.apache.pdfbox.cos.COSArray; import org.apache.pdfbox.cos.COSDictionary; import org.apache.pdfbox.cos.COSName; @@ -65,7 +70,7 @@ import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.common.utils.ImageUtils; import at.knowcenter.wag.egov.egiz.table.Entry; -public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { +public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements IDGenerator { private static final Logger logger = LoggerFactory .getLogger(PDFAsVisualSignatureBuilder.class); @@ -116,6 +121,17 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { getStructure().setTemplate(template); } + public String createHashedId(String value) { + try { + MessageDigest md = MessageDigest.getInstance("SHA-1"); + md.reset(); + return Hex.encodeHexString(md.digest(value.getBytes("UTF-8"))); + } catch(Throwable e) { + logger.warn("Failed to generate ID for Image using value", e); + return value; + } + } + private void readTableResources(PDFBoxTable table, PDDocument template) throws PdfAsException, IOException { @@ -162,15 +178,33 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { for (int j = 0; j < row.size(); j++) { Entry cell = (Entry) row.get(j); if (cell.getType() == Entry.TYPE_IMAGE) { - String img_ref = (String) cell.getValue(); + String img_value = (String) cell.getValue(); + String img_ref = createHashedId(img_value); if (!images.containsKey(img_ref)) { - File img_file = ImageUtils.getImageFile(img_ref, settings); - BufferedImage img = null; + try { - img = ImageIO.read(img_file); - } catch (IOException e) { - throw new PdfAsException("error.pdf.stamp.04", e); + File img_file = ImageUtils.getImageFile(img_value, settings); + + try { + img = ImageIO.read(img_file); + } catch (IOException e) { + throw new PdfAsException("error.pdf.stamp.04", e); + } + } catch(PdfAsException | IOException e) { + ByteArrayInputStream bais = null; + try { + bais = new ByteArrayInputStream(Base64.decodeBase64(img_value)); + img = ImageIO.read(bais); + bais.close(); + } catch(Throwable e1) { + // Ignore value is not base 64! + logger.debug("Value is not base64: ", e1); + // rethrow e + throw e; + } finally { + IOUtils.closeQuietly(bais); + } } float width = colsSizes[j]; @@ -250,7 +284,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { TableDrawUtils.drawTable(getStructure().getPage(), stream, 1, 1, designer.getWidth(), designer.getHeight(), properties.getMainTable(), template, false, - innerFormResources, images, settings); + innerFormResources, images, settings, this); stream.close(); PDStream innterFormStream = getStructure().getPage().getContents(); getStructure().setInnterFormStream(innterFormStream); diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java index 1530ab03..dff8e543 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java @@ -54,7 +54,7 @@ public class TableDrawUtils { PDPageContentStream contentStream, float x, float y, float width, float height, PDFBoxTable abstractTable, PDDocument doc, boolean subtable, PDResources formResources, - Map<String, ImageObject> images, ISettings settings) + Map<String, ImageObject> images, ISettings settings, IDGenerator generator) throws PdfAsException { logger.debug("Drawing Table: X {} Y {} WIDTH {} HEIGHT {} \n{}", x, y, @@ -69,14 +69,14 @@ public class TableDrawUtils { doc, subtable, settings); drawContent(page, contentStream, x, y, width, height, abstractTable, - doc, subtable, formResources, images, settings); + doc, subtable, formResources, images, settings, generator); } public static void drawContent(PDPage page, PDPageContentStream contentStream, float x, float y, float width, float height, PDFBoxTable abstractTable, PDDocument doc, boolean subtable, PDResources formResources, - Map<String, ImageObject> images, ISettings settings) + Map<String, ImageObject> images, ISettings settings, IDGenerator generator) throws PdfAsException { float contentx = x; @@ -124,7 +124,7 @@ public class TableDrawUtils { drawImage(page, contentStream, contentx, contenty, colWidth, abstractTable.getRowHeights()[i], padding, abstractTable, doc, cell, formResources, - images, settings); + images, settings, generator); break; case Entry.TYPE_TABLE: @@ -137,7 +137,7 @@ public class TableDrawUtils { drawTable(page, contentStream, contentx, contenty - abstractTable.getRowHeights()[i], colWidth, abstractTable.getRowHeights()[i], tbl_value, doc, - true, formResources, images, settings); + true, formResources, images, settings, generator); break; default: logger.warn("Unknown Cell entry type: " + cell.getType()); @@ -329,12 +329,12 @@ public class TableDrawUtils { float width, float height, float padding, PDFBoxTable abstractTable, PDDocument doc, Entry cell, PDResources formResources, Map<String, ImageObject> images, - ISettings settings) throws PdfAsException { + ISettings settings, IDGenerator generator) throws PdfAsException { try { float innerHeight = height; float innerWidth = width; - String img_ref = (String) cell.getValue(); + String img_ref = generator.createHashedId((String) cell.getValue()); if (!images.containsKey(img_ref)) { logger.warn("Image not prepared! : " + img_ref); throw new PdfAsException("Image not prepared! : " + img_ref); |