001/* 002 * Copyright (c) 2003-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2015-08-24 22:44:14 +0000 (Mon, 24 Aug 2015) $' 007 * '$Revision: 33630 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.reporting.rio.util; 031 032import java.awt.image.BufferedImage; 033import java.io.File; 034import java.io.IOException; 035 036import javax.imageio.ImageIO; 037 038import org.jpedal.PdfDecoder; 039import org.kepler.util.DotKeplerManager; 040 041import net.sourceforge.iharder.Base64; 042 043public class ImageUtil { 044 045 /** 046 * Wrapper method for decoding Base64 character encoded binary data 047 * used for serializing graphics in XML 048 * uses the iharder.sourceforge.net utility (public domain!) 049 * @param base64String 050 * @return path to local temporary file 051 */ 052 public static String decodeToFile(String base64String) { 053 String path = null; 054 File tempFile = null; 055 try { 056 //create tempfiles in ~/.kepler/module/reporting/ for now 057 File tempDir = DotKeplerManager.getInstance() 058 .getTransientModuleDirectory("reporting"); 059 060 if (!tempDir.exists()) { 061 tempDir.mkdir(); 062 } 063 tempFile = File.createTempFile("generatedImage", null, tempDir); 064 } catch (IOException e) { 065 // TODO Auto-generated catch block 066 e.printStackTrace(); 067 } 068 path = tempFile.getAbsolutePath(); 069 Base64.decodeToFile(base64String, path); 070 071 return path; 072 } 073 074 /** 075 * Found this example here: 076 * http://forums.sun.com/thread.jspa?threadID=355342 077 * This implementation uses the Multivalent library. 078 * Commented out since Multivalent appears to be GPL 079 * http://multivalent.sourceforge.net/license.html 080 * The Multivalent jar was removed April 2013. 081 * 082 * @param inputPdf PDF file to convert to PNG 083 * @param png output file 084 * 085 */ 086 /* 087 public static void convertPdf2Png(File inputPdf, File png) { 088 089 try { 090 091 PDF pdf = 092 (PDF) Behavior.getInstance("AdobePDF", "AdobePDF", null, null, null); 093 094 pdf.setInput(inputPdf); 095 096 Document doc = new Document("doc", null, null); 097 pdf.parse(doc); 098 doc.clear(); 099 100 doc.putAttr(Document.ATTR_PAGE, Integer.toString(1)); 101 pdf.parse(doc); 102 103 Node top = doc.childAt(0); 104 105 doc.formatBeforeAfter(400, 400, null); 106 int w = top.bbox.width; 107 int h = top.bbox.height; 108 BufferedImage img = new BufferedImage(w, h, 109 BufferedImage.TYPE_INT_RGB); 110 Graphics2D g = img.createGraphics(); 111 g.setClip(0, 0, w, h); 112 113 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 114 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 115 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 116 RenderingHints.VALUE_ANTIALIAS_ON); 117 g.setRenderingHint(RenderingHints.KEY_RENDERING, 118 RenderingHints.VALUE_RENDER_QUALITY); 119 120 Context cx = doc.getStyleSheet().getContext(g, null); 121 top.paintBeforeAfter(g.getClipBounds(), cx); 122 123 ImageIO.write(img, "png", png); 124 125 /* we close everything ... * / 126 doc.removeAllChildren(); 127 cx.reset(); 128 g.dispose(); 129 130 pdf.getReader().close(); 131 132 doc = null; 133 134 } catch (Exception e) { 135 e.printStackTrace(); 136 } 137 } 138 */ 139 140 /** 141 * Implementation uses JPedal. 142 * @param pdf 143 * @param png 144 */ 145 public static void pdf2png(File pdf, File png) { 146 try { 147 PdfDecoder decode_pdf = new PdfDecoder(true); 148 decode_pdf.useHiResScreenDisplay(true); 149 decode_pdf.openPdfFile(pdf.getAbsolutePath()); 150 151 BufferedImage image_to_save = decode_pdf.getPageAsImage(1); 152 ImageIO.write(image_to_save, "png", png); 153 154 } catch (Exception e) { 155 e.printStackTrace(); 156 } 157 158 } 159 160}