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.commons.pdf;
020
021import java.awt.Dimension;
022import java.awt.Insets;
023import java.io.InputStream;
024import java.io.InputStreamReader;
025import java.io.OutputStream;
026import java.io.StringReader;
027import java.net.URL;
028import java.net.URLClassLoader;
029
030import lucee.Version;
031import lucee.commons.io.IOUtil;
032import lucee.commons.io.res.Resource;
033import lucee.commons.io.res.filter.ExtensionResourceFilter;
034import lucee.commons.io.res.util.FileWrapper;
035import lucee.commons.io.res.util.ResourceUtil;
036import lucee.commons.lang.ClassUtil;
037import lucee.commons.lang.Md5;
038import lucee.runtime.Info;
039import lucee.runtime.config.Config;
040import lucee.runtime.exp.PageException;
041import lucee.runtime.op.Caster;
042
043public class PDF {
044        
045        private final Object pd4ml;
046        private static Class pd4mlClass;
047        private static ClassLoader classLoader;
048        private static Class pd4mlMarkClass;
049        //private final boolean isEvaluation;
050
051        public PDF(Config config) throws PageException {
052                //this.isEvaluation=isEvaluation;
053                try {
054                        //classLoader=new URLClassLoader(new URL[]{new File("/Users/mic/Downloads/java/pd4ml/fullversion/pd4ml.volume.310/lib/pd4ml.jar").toURL()},this.getClass().getClassLoader());
055                        if(classLoader==null) {
056                                Resource temp = config.getConfigDir().getRealResource("temp");
057                                Resource file=temp.getRealResource(Md5.getDigestAsString(Info.getVersionAsString())+".lmdp");
058                        
059                                if(!file.exists()){
060                                        ResourceUtil.removeChildrenEL(temp, new ExtensionResourceFilter(".lmdp"));
061                                        
062                                file.createFile(true);
063                                //print.out(new Info().getClass().getResource("/resource/lib/pd4ml.jar"));
064                                InputStream jar = new Info().getClass().getResourceAsStream("/resource/lib/pd4ml.jar");
065                                IOUtil.copy(jar, file,true);
066                        }
067                        ClassLoader parent = Version.class.getClassLoader();
068                        classLoader=new URLClassLoader(new URL[]{FileWrapper.toFile(file).toURL()},parent);
069                        
070                        //classLoader=new URLClassLoader(new URL[]{new Info().getClass().getResource("/resource/lib/pd4ml.jar")},this.getClass().getClassLoader());
071                        }
072                        if(pd4mlClass==null)pd4mlClass=ClassUtil.loadClass(classLoader,"org.zefer.pd4ml.PD4ML");
073                        pd4ml=ClassUtil.loadInstance(pd4mlClass);
074                        
075                } catch (Exception e) {
076                        throw Caster.toPageException(e);
077                }
078                pd4mlClass=pd4ml.getClass();
079        }
080        
081        public void enableTableBreaks(boolean b) throws PageException {
082                invoke(pd4ml,"enableTableBreaks",b);
083        }
084
085        public void interpolateImages(boolean b) throws PageException {
086                invoke(pd4ml,"interpolateImages",b);
087        }
088
089        public void adjustHtmlWidth() throws PageException {
090                invoke(pd4ml,"adjustHtmlWidth");
091        }
092
093        public void setPageInsets(Insets insets) throws PageException {
094                invoke(pd4ml,"setPageInsets",insets);
095        }
096
097        public void setPageSize(Dimension dimension) throws PageException {
098                invoke(pd4ml,"setPageSize",dimension);
099        }
100
101        public void setPageHeader(PDFPageMark header) throws PageException {
102                invoke(pd4ml,"setPageHeader",toPD4PageMark(header));
103        }
104        
105        public void generateOutlines(boolean flag) throws PageException {
106                invoke(pd4ml,"generateOutlines",
107                                new Object[]{Caster.toBoolean(flag)},new Class[]{boolean.class});
108        }
109
110        public void useTTF(String pathToFontDirs,boolean embed ) throws PageException {
111                invoke(pd4ml,"useTTF",
112                                new Object[]{pathToFontDirs,Caster.toBoolean(embed)},
113                                new Class[]{String.class,boolean.class});
114        }
115        
116
117        public void setDefaultTTFs(String string, String string2, String string3) throws PageException {
118                invoke(pd4ml,"setDefaultTTFs",
119                                new Object[]{string,string2,string3},
120                                new Class[]{String.class,String.class,String.class});
121        }
122
123        public void setPageFooter(PDFPageMark footer) throws PageException {
124                //if(isEvaluation) return;
125                invoke(pd4ml,"setPageFooter",toPD4PageMark(footer));
126        }
127        
128        public void render(InputStreamReader reader, OutputStream os) throws PageException {
129                //setEvaluationFooter();
130                
131                
132                invoke(pd4ml, "render", 
133                                new Object[]{reader,os}, 
134                                new Class[]{reader.getClass(),OutputStream.class});
135                
136                //invoke(pd4ml,"render",reader,os,OutputStream.class);
137                
138        }
139
140        public void render(String str, OutputStream os,URL base) throws PageException {
141                //setEvaluationFooter();
142                
143                StringReader sr = new StringReader(str);
144                if(base==null) {
145                        invoke(pd4ml, "render", 
146                                new Object[]{sr,os}, 
147                                new Class[]{sr.getClass(),OutputStream.class});
148                }
149                else {
150                        invoke(pd4ml, "render", 
151                                new Object[]{sr,os,base}, 
152                                new Class[]{sr.getClass(),OutputStream.class,URL.class});
153                }
154                //invoke(pd4ml,"render",new StringReader(str),os,OutputStream.class);
155        }
156        
157        /*private void setEvaluationFooterX() throws PageException {
158                if(isEvaluation) invoke(pd4ml,"setPageFooter",toPD4PageMark(new PDFPageMark(-1,EVAL_TEXT)));
159        }*/
160
161
162        private Object toPD4PageMark(PDFPageMark mark) throws PageException {
163                Object pd4mlMark=null;
164                try {
165                        if(pd4mlMarkClass==null)pd4mlMarkClass=ClassUtil.loadClass(classLoader,"org.zefer.pd4ml.PD4PageMark");
166                        pd4mlMark=ClassUtil.loadInstance(pd4mlMarkClass);
167                        
168                } catch (Exception e) {
169                }
170
171                invoke(pd4mlMark,"setAreaHeight",mark.getAreaHeight());
172                invoke(pd4mlMark,"setHtmlTemplate",mark.getHtmlTemplate());
173                return pd4mlMark;
174        }
175        
176        private void invoke(Object o,String methodName, Object[] args, Class[] argClasses) throws PageException {
177                try {
178                        o.getClass().getMethod(methodName, argClasses).invoke(o, args);
179                } 
180                catch (Exception e) {
181                        throw Caster.toPageException(e);
182                }
183        }
184        /*private void invoke(Object o,String methodName, Object argument1, Object argument2,Class clazz) throws PageException {
185                try {
186                        o.getClass().getMethod(methodName, new Class[]{argument1.getClass(),clazz}).invoke(o, new Object[]{argument1,argument2});
187                } 
188                catch (Exception e) {
189                        throw Caster.toPageException(e);
190                }
191        }*/
192        private void invoke(Object o,String methodName, Object argument) throws PageException {
193                try {
194                        o.getClass().getMethod(methodName, new Class[]{argument.getClass()}).invoke(o, new Object[]{argument});
195                } 
196                catch (Exception e) {
197                        throw Caster.toPageException(e);
198                }
199        }
200
201        private void invoke(Object o,String methodName, boolean argument) throws PageException {
202                try {
203                        o.getClass().getMethod(methodName, new Class[]{boolean.class}).invoke(o, new Object[]{Caster.toRef(argument)});
204                } 
205                catch (Exception e) {
206                        throw Caster.toPageException(e);
207                }
208        }
209        private void invoke(Object o,String methodName, int argument) throws PageException {
210                try {
211                        o.getClass().getMethod(methodName, new Class[]{int.class}).invoke(o, new Object[]{Caster.toRef(argument)});
212                } 
213                catch (Exception e) {
214                        throw Caster.toPageException(e);
215                }
216        }
217        
218        private void invoke(Object o,String methodName) throws PageException {
219                try {
220                        o.getClass().getMethod(methodName, new Class[]{}).invoke(o, new Object[]{});
221                } 
222                catch (Exception e) {
223                        throw Caster.toPageException(e);
224                }
225        }
226
227
228}