diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2015-02-25 12:50:41 +0100 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2015-02-25 12:50:41 +0100 |
commit | 23a283834becf8323489231ed5c48725f9716bb6 (patch) | |
tree | 9c5b4b099656dbdc77118c1b2fd5ab201f091f63 | |
parent | de87f168478445ffb2b018804ea46a834dffcfbf (diff) | |
download | pdf-as-4-23a283834becf8323489231ed5c48725f9716bb6.tar.gz pdf-as-4-23a283834becf8323489231ed5c48725f9716bb6.tar.bz2 pdf-as-4-23a283834becf8323489231ed5c48725f9716bb6.zip |
fixed base64 image value
8 files changed, 241 insertions, 124 deletions
diff --git a/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/ProduceSignBlockImg.java b/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/ProduceSignBlockImg.java index a39a0a40..e3a087b3 100644 --- a/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/ProduceSignBlockImg.java +++ b/pdf-as-cli/src/test/java/at/gv/egiz/pdfas/cli/test/ProduceSignBlockImg.java @@ -22,7 +22,9 @@ public class ProduceSignBlockImg { PdfAs pdfAs = PdfAsFactory.createPdfAs(new File("/home/afitzek/.pdfas")); Configuration cfg = pdfAs.getConfiguration(); - + cfg.setValue("sig_obj.SIGNATURBLOCK_SMALL_DE.value.SIG_LABEL", "iVBORw0KGgoAAAANSUhEUgAAAMgAAADIAQAAAACFI5MzAAAA30lEQVR42u2XQRKDMAhFcZVj5KamuWmO4SqUfLQz " + +"tema70xYYZ4Lhp+PKPovZJFFFnkGOcRiU61Hfo00M5GRViT1euQhL8kVx/WQjZB41aSk7cJIoHaTpPN7EEngktHOuX8iiYdbZDZ3IslQeBQ7pHYrk5Fi7UyQ+kttBmLHuIn2yq3qYJK75J7MwbKDExHVbmLjzMczFfm4BB8NJnJNl9Ha1LafDSWUYCqjaruPhYxAZ" + +"Jj4vj1xEJS8J+1J6cg5YFqZ7FWRxNU2HxfsKUzkdIkP5tmGEkfW/9wiizyUvAFFQH2e7NyBBgAAAABJRU5ErkJggg=="); SignParameter signParameter = PdfAsFactory.createSignParameter(cfg, null, null); X509Certificate crt = new X509Certificate(new FileInputStream("/home/afitzek/qualified.cer")); diff --git a/pdf-as-common/build.gradle b/pdf-as-common/build.gradle index 709bc3ee..0190c859 100644 --- a/pdf-as-common/build.gradle +++ b/pdf-as-common/build.gradle @@ -24,6 +24,8 @@ dependencies { compile group: 'commons-collections', name: 'commons-collections', version: '3.2' compile group: 'commons-io', name: 'commons-io', version: '2.4' compile group: 'ognl', name: 'ognl', version: '3.0.6' + compile 'commons-codec:commons-codec:1.10' + testCompile group: 'junit', name: 'junit', version: '4.+' } diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/api/ws/PDFASSignParameters.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/api/ws/PDFASSignParameters.java index 5fa5c3ee..32f10aa9 100644 --- a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/api/ws/PDFASSignParameters.java +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/api/ws/PDFASSignParameters.java @@ -78,6 +78,7 @@ public class PDFASSignParameters implements Serializable { String invokeErrorUrl; String transactionId; String keyIdentifier; + String qrCodeContent; String profile; PDFASPropertyMap preprocessor; @@ -145,6 +146,14 @@ public class PDFASSignParameters implements Serializable { public void setKeyIdentifier(String keyIdentifier) { this.keyIdentifier = keyIdentifier; } + + @XmlElement(required = false, nillable = true, name="qrCodeContent") + public String getQRCodeContent() { + return qrCodeContent; + } + public void setQRCodeContent(String qrCodeContent) { + this.qrCodeContent = qrCodeContent; + } @XmlElement(required = false, nillable = true, name="preprocessorArguments") public PDFASPropertyMap getPreprocessor() { diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/ImageUtils.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/ImageUtils.java index dcf1f17b..ab087f2c 100644 --- a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/ImageUtils.java +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/utils/ImageUtils.java @@ -32,7 +32,9 @@ import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.IndexColorModel; import java.awt.image.WritableRaster; +import java.io.ByteArrayInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; @@ -41,6 +43,8 @@ import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,33 +52,30 @@ import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.settings.ISettings; public class ImageUtils { - + private static final Logger logger = LoggerFactory .getLogger(ImageUtils.class); - + public static BufferedImage removeAlphaChannel(BufferedImage src) { - if (src.getColorModel().hasAlpha()) - { - BufferedImage image = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB); - Graphics2D g = image.createGraphics(); - g.setComposite(AlphaComposite.Src); - g.drawImage(src, 0, 0, null); - g.dispose(); - return image; - } + if (src.getColorModel().hasAlpha()) { + BufferedImage image = new BufferedImage(src.getWidth(), + src.getHeight(), BufferedImage.TYPE_INT_RGB); + Graphics2D g = image.createGraphics(); + g.setComposite(AlphaComposite.Src); + g.drawImage(src, 0, 0, null); + g.dispose(); + return image; + } return src; /* - BufferedImage rgbImage = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_3BYTE_BGR); - for (int x = 0; x < src.getWidth(); ++x) - { - for (int y = 0; y < src.getHeight(); ++y) - { - rgbImage.setRGB(x, y, src.getRGB(x, y) & 0xFFFFFF); - } - } - return rgbImage;*/ + * BufferedImage rgbImage = new BufferedImage(src.getWidth(), + * src.getHeight(), BufferedImage.TYPE_3BYTE_BGR); for (int x = 0; x < + * src.getWidth(); ++x) { for (int y = 0; y < src.getHeight(); ++y) { + * rgbImage.setRGB(x, y, src.getRGB(x, y) & 0xFFFFFF); } } return + * rgbImage; + */ } - + public static BufferedImage convertRGBAToIndexed(BufferedImage src) { BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED); @@ -110,46 +111,96 @@ public class ImageUtils { return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(), null); } - - public static Dimension getImageDimensions(InputStream is) throws IOException { + + public static Dimension getImageDimensions(InputStream is) + throws IOException { ImageInputStream in = ImageIO.createImageInputStream(is); try { - final Iterator<ImageReader> readers = ImageIO.getImageReaders(in); - if (readers.hasNext()) { - ImageReader reader = readers.next(); - try { - reader.setInput(in); - return new Dimension(reader.getWidth(0), reader.getHeight(0)); - } finally { - reader.dispose(); - } - } - throw new IOException("Failed to read Image file"); + final Iterator<ImageReader> readers = ImageIO.getImageReaders(in); + if (readers.hasNext()) { + ImageReader reader = readers.next(); + try { + reader.setInput(in); + return new Dimension(reader.getWidth(0), + reader.getHeight(0)); + } finally { + reader.dispose(); + } + } + throw new IOException("Failed to read Image file"); } finally { - if (in != null) in.close(); + if (in != null) + in.close(); } } - - public static File getImageFile(String imageFile, ISettings settings) throws PdfAsException, IOException { + + public static File getImageFile(String imageFile, ISettings settings) + throws PdfAsException, IOException { File img_file = new File(imageFile); if (!img_file.isAbsolute()) { logger.debug("Image file declaration is relative. Prepending path of resources directory."); - logger.debug("Image Location: " - + settings.getWorkingDirectory() - + File.separator + imageFile); - img_file = new File(settings.getWorkingDirectory() + logger.debug("Image Location: " + settings.getWorkingDirectory() + File.separator + imageFile); + img_file = new File(settings.getWorkingDirectory() + File.separator + + imageFile); } else { logger.debug("Image file declaration is absolute. Skipping file relocation."); } if (!img_file.exists()) { - logger.debug("Image file \"" - + img_file.getCanonicalPath() + logger.debug("Image file \"" + img_file.getCanonicalPath() + "\" doesn't exist."); throw new PdfAsException("error.pdf.stamp.04"); } - + return img_file; } + + public static Dimension getImageDimensions(String imageValue, + ISettings settings) throws PdfAsException, IOException { + InputStream is = getImageInputStream(imageValue, settings); + try { + return getImageDimensions(is); + } catch (Throwable e) { + throw new PdfAsException("error.pdf.stamp.04", e); + } finally { + IOUtils.closeQuietly(is); + } + } + + public static InputStream getImageInputStream(String imageValue, + ISettings settings) throws PdfAsException, IOException { + InputStream is = null; + try { + File img_file = ImageUtils.getImageFile(imageValue, settings); + + if (!img_file.exists()) { + throw new PdfAsException("error.pdf.stamp.04"); + } + + is = new FileInputStream(img_file); + } catch (PdfAsException | IOException e) { + try { + is = new ByteArrayInputStream(Base64.decodeBase64(imageValue)); + } catch (Throwable e1) { + // Ignore value is not base 64! + logger.debug("Value is not base64: ", e1); + // rethrow e + throw e; + } + } + return is; + } + + public static BufferedImage getImage(String imageValue, ISettings settings) + throws PdfAsException, IOException { + InputStream is = getImageInputStream(imageValue, settings); + try { + return ImageIO.read(is); + } catch (Throwable e) { + throw new PdfAsException("error.pdf.stamp.04", e); + } finally { + IOUtils.closeQuietly(is); + } + } } diff --git a/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/B64ImageTest.java b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/B64ImageTest.java new file mode 100644 index 00000000..7a0519fa --- /dev/null +++ b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/B64ImageTest.java @@ -0,0 +1,40 @@ +package at.gv.egiz.pdfas.lib.test.mains; + +import java.io.File; +import java.io.FileInputStream; + +import at.gv.egiz.pdfas.common.settings.ISettings; +import at.gv.egiz.pdfas.common.utils.StreamUtils; +import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; +import at.gv.egiz.pdfas.lib.api.Configuration; +import at.gv.egiz.pdfas.lib.api.PdfAs; +import at.gv.egiz.pdfas.lib.api.PdfAsFactory; +import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; +import at.gv.egiz.pdfas.lib.api.sign.SignParameter; + +public class B64ImageTest { + + public static final String keyStoreFile = "/home/afitzek/devel/pdfas_neu/test.p12"; + public static final String keyStoreType = "PKCS12"; + public static final String keyStorePass = "123456"; + //public static final String keyAlias = "pdf"; + public static final String keyAlias = "ecc_test"; + public static final String keyPass = "123456"; + + public static void main(String[] args) { + String user_home = System.getProperty("user.home"); + String pdfas_dir = user_home + File.separator + ".pdfas"; + PdfAs pdfas = PdfAsFactory.createPdfAs(new File(pdfas_dir)); + try { + Configuration config = pdfas.getConfiguration(); + ISettings settings = (ISettings) config; + + + } catch (Throwable e) { + e.printStackTrace(); + } finally { + + } + } + +} 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 de944d43..d0b71fd0 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 @@ -181,31 +181,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder implements String img_value = (String) cell.getValue(); String img_ref = createHashedId(img_value); if (!images.containsKey(img_ref)) { - BufferedImage img = null; - - try { - 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); - } - } + BufferedImage img = ImageUtils.getImage(img_value, settings); float width = colsSizes[j]; float height = table.getRowHeights()[i] + padding * 2; diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java index 22947643..8959d631 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java @@ -69,7 +69,7 @@ public class PDFBoxTable { float[] colWidths; PDDocument originalDoc; - + private void normalizeContent(Table abstractTable) throws PdfAsException { try { int rows = abstractTable.getRows().size(); @@ -93,8 +93,8 @@ public class PDFBoxTable { } } - private void initializeStyle(Table abstractTable, PDFBoxTable parent, PDDocument originalDoc) - throws IOException { + private void initializeStyle(Table abstractTable, PDFBoxTable parent, + PDDocument originalDoc) throws IOException { this.table = abstractTable; try { normalizeContent(abstractTable); @@ -148,7 +148,8 @@ public class PDFBoxTable { } public PDFBoxTable(Table abstractTable, PDFBoxTable parent, float fixSize, - ISettings settings, PDDocument originalDoc) throws IOException, PdfAsException { + ISettings settings, PDDocument originalDoc) throws IOException, + PdfAsException { this.settings = settings; this.originalDoc = originalDoc; initializeStyle(abstractTable, parent, originalDoc); @@ -177,19 +178,22 @@ public class PDFBoxTable { } } calculateHeightsBasedOnWidths(); - - logger.debug("Generating Table with fixed With {} got width {}", fixSize, getWidth()); + + logger.debug("Generating Table with fixed With {} got width {}", + fixSize, getWidth()); } public PDFBoxTable(Table abstractTable, PDFBoxTable parent, - ISettings settings, PDDocument originalDoc) throws IOException, PdfAsException { + ISettings settings, PDDocument originalDoc) throws IOException, + PdfAsException { this.settings = settings; this.originalDoc = originalDoc; initializeStyle(abstractTable, parent, originalDoc); this.calculateWidthHeight(); } - private void calculateHeightsBasedOnWidths() throws IOException, PdfAsException { + private void calculateHeightsBasedOnWidths() throws IOException, + PdfAsException { int rows = this.table.getRows().size(); rowHeights = new float[rows]; addPadding = new boolean[rows]; @@ -203,7 +207,7 @@ public class PDFBoxTable { for (int j = 0; j < row.size(); j++) { Entry cell = (Entry) row.get(j); - float colWidth = 0;//colWidths[j]; + float colWidth = 0;// colWidths[j]; int colsleft = cell.getColSpan(); @@ -288,16 +292,15 @@ public class PDFBoxTable { ArrayList<Entry> row = this.table.getRows().get(i); for (int j = 0; j < row.size(); j++) { Entry cell = (Entry) row.get(j); - if(cell.getType() == Entry.TYPE_TABLE) { - PDFBoxTable tbl = (PDFBoxTable)cell.getValue(); - if(rowHeights[i] != tbl.getHeight()) - { + if (cell.getType() == Entry.TYPE_TABLE) { + PDFBoxTable tbl = (PDFBoxTable) cell.getValue(); + if (rowHeights[i] != tbl.getHeight()) { tbl.setHeight(rowHeights[i]); } } } } - + this.tableWidth = 0; for (int i = 0; i < colWidths.length; i++) { @@ -489,27 +492,31 @@ public class PDFBoxTable { float maxLineHeight = 0; try { byte[] linebytes = StringUtils.applyWinAnsiEncoding(lines[i]); - for(int j = 0; j < linebytes.length; j++) { - float he = c.getFontHeight(linebytes, j, 1) / 1000 * fontSize; - if(he > maxLineHeight) { + for (int j = 0; j < linebytes.length; j++) { + float he = c.getFontHeight(linebytes, j, 1) / 1000 + * fontSize; + if (he > maxLineHeight) { maxLineHeight = he; } } } catch (UnsupportedEncodingException e) { logger.warn("failed to determine String height", e); - maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 * fontSize; + maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 + * fontSize; } catch (IOException e) { logger.warn("failed to determine String height", e); - maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 * fontSize; + maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 + * fontSize; } - + heights[i] = maxLineHeight; } - + return heights; } - private float getCellHeight(Entry cell, float width) throws IOException, PdfAsException { + private float getCellHeight(Entry cell, float width) throws IOException, + PdfAsException { boolean isValue = true; switch (cell.getType()) { case Entry.TYPE_CAPTION: @@ -530,27 +537,19 @@ public class PDFBoxTable { fontSize); cell.setValue(concatLines(lines)); float[] heights = getStringHeights(lines, c, fontSize); - return fontSize * heights.length + padding * 2; + return fontSize * heights.length + padding * 2; case Entry.TYPE_IMAGE: - String imageFile = (String)cell.getValue(); - File img_file = ImageUtils.getImageFile(imageFile, settings); + String imageFile = (String) cell.getValue(); if (style != null && style.getImageScaleToFit() != null) { - //if (style.getImageScaleToFit().getHeight() < width) { - return style.getImageScaleToFit().getHeight() + padding * 2; - //} + // if (style.getImageScaleToFit().getHeight() < width) { + return style.getImageScaleToFit().getHeight() + padding * 2; + // } } - FileInputStream fis = new FileInputStream(img_file); - try { - Dimension dim = ImageUtils.getImageDimensions(fis); - if(dim.getHeight() > 80.0f) { - return width + padding * 2; - } - return (float)dim.getHeight() + padding * 2; - } finally { - if(fis != null) { - fis.close(); - } + Dimension dim = ImageUtils.getImageDimensions(imageFile, settings); + if (dim.getHeight() > 80.0f) { + return width + padding * 2; } + return (float) dim.getHeight() + padding * 2; case Entry.TYPE_TABLE: PDFBoxTable pdfBoxTable = null; if (cell.getValue() instanceof Table) { @@ -598,23 +597,16 @@ public class PDFBoxTable { return fontSize + padding * 2; } case Entry.TYPE_IMAGE: - String imageFile = (String)cell.getValue(); - File img_file = ImageUtils.getImageFile(imageFile, settings); + String imageFile = (String) cell.getValue(); if (style != null && style.getImageScaleToFit() != null) { return style.getImageScaleToFit().getHeight() + padding * 2; } - FileInputStream fis = new FileInputStream(img_file); - try { - Dimension dim = ImageUtils.getImageDimensions(fis); - if(dim.getHeight() > 80.0f) { - return 80.0f + padding * 2; - } - return (float)dim.getHeight() + padding * 2; - } finally { - if(fis != null) { - fis.close(); - } + Dimension dim = ImageUtils.getImageDimensions(imageFile, settings); + if (dim.getHeight() > 80.0f) { + return 80.0f + padding * 2; } + return (float) dim.getHeight() + padding * 2; + case Entry.TYPE_TABLE: PDFBoxTable pdfBoxTable = null; if (cell.getValue() instanceof Table) { @@ -648,10 +640,10 @@ public class PDFBoxTable { public float getHeight() { return tableHeight; } - + public void setHeight(float height) { float diff = height - this.getHeight(); - if(diff > 0) { + if (diff > 0) { this.rowHeights[rowHeights.length - 1] += diff; calcTotals(); } else { diff --git a/pdf-as-pdfbox/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/TTFFontTest.java b/pdf-as-pdfbox/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/TTFFontTest.java new file mode 100644 index 00000000..fdc59ab8 --- /dev/null +++ b/pdf-as-pdfbox/src/test/java/at/gv/egiz/pdfas/lib/testpdfbox/TTFFontTest.java @@ -0,0 +1,45 @@ +package at.gv.egiz.pdfas.lib.testpdfbox; + +import java.io.File; +import java.util.Iterator; +import java.util.List; + +import org.apache.pdfbox.cos.COSBase; +import org.apache.pdfbox.cos.COSName; +import org.apache.pdfbox.cos.COSObject; +import org.apache.pdfbox.pdmodel.PDDocument; + +public class TTFFontTest { + + public static void main(String[] args) { + try { + PDDocument doc = PDDocument.load(new File("/home/afitzek/Downloads/pdf_groesse/willenserklaerung_signedByUser.pdf")); + + List<COSObject> cosObjects = doc.getDocument().getObjectsByType(COSName.FONT); + + Iterator<COSObject> cosObjectIt = cosObjects.iterator(); + + while(cosObjectIt.hasNext()) { + COSObject cosObject = cosObjectIt.next(); + COSBase subType = cosObject.getItem(COSName.SUBTYPE); + COSBase baseFont = cosObject.getItem(COSName.BASE_FONT); + COSBase aTest = cosObject.getItem(COSName.A); + + System.out.println(aTest); + + if(subType.equals(COSName.TRUE_TYPE)) { + System.out.println("Object Number: " + cosObject.getObjectNumber().intValue() + + subType.toString()); + System.out.println(" BaseFont: " + baseFont.toString()); + } + + + } + + + } catch(Throwable e) { + e.printStackTrace(); + } + } + +} |