001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.text.pdf; 020 021import java.io.IOException; 022import java.util.Set; 023 024import lucee.commons.io.IOUtil; 025import lucee.commons.io.res.Resource; 026import lucee.runtime.exp.ExpressionException; 027import lucee.runtime.exp.PageException; 028import lucee.runtime.img.Image; 029import lucee.runtime.op.Caster; 030 031import org.jpedal.PdfDecoder; 032import org.jpedal.exception.PdfException; 033 034public class PDF2ImageJPedal extends PDF2Image { 035 036 037 public void writeImages(byte[] input,Set pages,Resource outputDirectory, String prefix,String format, int scale, 038 boolean overwrite, boolean goodQuality,boolean transparent) throws PageException, IOException { 039 PdfDecoder dec = createPdfDecoder(input); 040 Resource res; 041 int count = dec.getPageCount(); 042 043 for(int page=1;page<=count;page++) { 044 if(pages!=null && !pages.contains(Integer.valueOf(page)))continue; 045 //res=outputDirectory.getRealResource(prefix+"_page_"+page+"."+format); 046 res=createDestinationResource(outputDirectory,prefix,page,format,overwrite); 047 writeImage(dec,page,res,format,scale,overwrite,goodQuality, transparent); 048 } 049 050 } 051 052 053 054 private static void writeImage(PdfDecoder dec, int page, Resource destination,String format, int scale, 055 boolean overwrite, boolean goodQuality, boolean transparent) throws PageException, IOException { 056 if(scale<1 || scale>100) 057 throw new ExpressionException("invalid scale definition ["+Caster.toString(scale)+"], value should be in range from 1 to 100"); 058 059 060 Image img=null; 061 try { 062 img = new Image(transparent?dec.getPageAsTransparentImage(page):dec.getPageAsImage(page)); 063 } catch (PdfException e) { 064 throw Caster.toPageException(e); 065 } 066 if(scale!=100) 067 img.resize(scale, goodQuality?"highestquality":"highperformance", 1); 068 img.writeOut(destination,format, overwrite, 1f); 069 } 070 071 072 public Image toImage(byte[] input,int page) throws IOException, PageException { 073 try { 074 return new Image(createPdfDecoder(input).getPageAsImage(page)); 075 } catch (PdfException e) { 076 throw Caster.toPageException(e); 077 } 078 } 079 080 private static PdfDecoder createPdfDecoder(Resource res) throws PageException,IOException { 081 return createPdfDecoder(IOUtil.toBytes(res)); 082 } 083 084 private static PdfDecoder createPdfDecoder(byte[] input) throws PageException { 085 PdfDecoder decoder = new PdfDecoder(true); 086 decoder.useHiResScreenDisplay(true); 087 try { 088 decoder.openPdfArray(input); 089 } catch (PdfException e) { 090 throw Caster.toPageException(e); 091 } 092 return decoder; 093 } 094 095}