Ticket #514: resources.patch

File resources.patch, 1.9 kB (added by bartvde, 3 years ago)
  • print-lib/src/main/java/org/mapfish/print/PDFUtils.java

     
    3939import java.util.regex.Matcher; 
    4040import java.util.regex.Pattern; 
    4141 
     42import java.io.BufferedInputStream; 
     43import java.io.ByteArrayOutputStream; 
     44 
    4245/** 
    4346 * Some utility functions for iText. 
    4447 */ 
     
    9295     * Gets an iText image. Avoids doing the query twice. 
    9396     */ 
    9497    protected static Image getImageDirect(RenderingContext context, URI uri) throws IOException, DocumentException { 
     98        if(uri.toString().indexOf("resource:")>-1) { 
     99            BufferedInputStream bis = null; 
     100            ByteArrayOutputStream baos = null; 
     101            try { 
     102                bis = new BufferedInputStream(PDFUtils.class.getResourceAsStream(uri.toString().substring("resource:".length()))); 
     103                baos = new ByteArrayOutputStream(); 
     104                byte[] data = new byte[1024]; 
     105                int tmp = bis.read(data); 
     106                while(tmp != -1) { 
     107                    baos.write(data, 0, tmp); 
     108                    tmp = bis.read(data); 
     109                } 
     110                Image image = Image.getInstance(baos.toByteArray()); 
     111                return image; 
     112            } catch (DocumentException e) { 
     113                LOGGER.warn("Couldn't generate a transparent image"); 
     114                throw e; 
     115            } 
     116            finally { 
     117                if(bis != null) { 
     118                    bis.close(); 
     119                } 
     120                if(baos != null) { 
     121                    baos.close(); 
     122                } 
     123            } 
     124        } 
    95125        if (!uri.isAbsolute()) { 
    96126            //non-absolute URI are local, so we can use the normal iText method 
    97127            return Image.getInstance(uri.toString());