KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > MultipleFO2PDF


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: MultipleFO2PDF.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package embedding;
21
22 // Java
23
import java.io.BufferedOutputStream JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29
30 //JAXP
31
import javax.xml.transform.Transformer JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
33 import javax.xml.transform.TransformerFactory JavaDoc;
34 import javax.xml.transform.Source JavaDoc;
35 import javax.xml.transform.Result JavaDoc;
36 import javax.xml.transform.stream.StreamSource JavaDoc;
37 import javax.xml.transform.sax.SAXResult JavaDoc;
38
39 // FOP
40
import org.apache.commons.io.IOUtils;
41 import org.apache.fop.apps.FOUserAgent;
42 import org.apache.fop.apps.Fop;
43 import org.apache.fop.apps.FOPException;
44 import org.apache.fop.apps.FopFactory;
45 import org.apache.fop.apps.FormattingResults;
46 import org.apache.fop.apps.MimeConstants;
47 import org.apache.fop.apps.PageSequenceResults;
48
49 /**
50  * This class demonstrates the conversion of multiple FO files to PDF using FOP.
51  * The FopFactory is reused. Its configuration is applied to each rendering run.
52  * The FOUserAgent and Fop are newly created by the FopFactory for each run.
53  * The FOUserAgent can be configured differently for each run.
54  */

55 public class MultipleFO2PDF {
56
57     // configure fopFactory as desired
58
private FopFactory fopFactory = FopFactory.newInstance();
59
60     // JAXP TransformerFactory can be reused, too
61
private TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
62     
63     /**
64      * Converts an FO file to a PDF file using FOP
65      * @param fo the FO file
66      * @param pdf the target PDF file
67      * @throws TransformerException in case of a transformation problem
68      * @throws IOException in case of an I/O problem
69      * @throws FOPException in case of a FOP problem
70      * @return the formatting results of the run
71      */

72     public FormattingResults convertFO2PDF(File JavaDoc fo, File JavaDoc pdf)
73         throws TransformerException JavaDoc, IOException JavaDoc, FOPException {
74         
75         OutputStream JavaDoc out = null;
76         Fop fop;
77         
78         try {
79             FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
80             // configure foUserAgent as desired
81

82             // Setup output stream. Note: Using BufferedOutputStream
83
// for performance reasons (helpful with FileOutputStreams).
84
out = new FileOutputStream JavaDoc(pdf);
85             out = new BufferedOutputStream JavaDoc(out);
86
87             // Construct fop with desired output format and output stream
88
fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
89
90             // Setup JAXP using identity transformer
91
Transformer JavaDoc transformer = factory.newTransformer(); // identity transformer
92

93             // Setup input stream
94
Source JavaDoc src = new StreamSource JavaDoc(fo);
95
96             // Resulting SAX events (the generated FO) must be piped through to FOP
97
Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
98             
99             // Start XSLT transformation and FOP processing
100
transformer.transform(src, res);
101         } finally {
102             IOUtils.closeQuietly(out);
103         }
104
105         return fop.getResults();
106     }
107
108     /**
109      * Listens on standard in for names of fo files to be transformed to pdf.
110      * 'quit' or the null string (for piped input) cause the listener to stop listening.
111      */

112     public void listen() {
113
114         //Setup directories
115
File JavaDoc baseDir = new File JavaDoc(".");
116         File JavaDoc outDir = new File JavaDoc(baseDir, "out");
117         outDir.mkdirs();
118         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
119         
120         while (true) {
121             try {
122                 // Listen for the input file name
123
System.out.print("Input XSL-FO file ('quit' to stop): ");
124                 String JavaDoc foname = in.readLine();
125                 if (foname == null) {
126                     System.out.println("Null input, quitting");
127                     return;
128                 }
129                 foname.trim();
130                 if (foname.equals("quit")) {
131                     System.out.println("Quitting");
132                     return;
133                 }
134                 File JavaDoc fofile = new File JavaDoc(baseDir, foname);
135                 String JavaDoc pdfname = foname;
136                 int p = foname.lastIndexOf('.');
137                 pdfname = foname.substring(0, p) + ".pdf";
138                 File JavaDoc pdffile = new File JavaDoc(outDir, pdfname);
139
140                 // transform and render
141
System.out.print("Transforming " + fofile + " to PDF file " + pdffile + "...");
142                 FormattingResults foResults = convertFO2PDF(fofile, pdffile);
143                 System.out.println("done!");
144
145                 // Result processing
146
java.util.List JavaDoc pageSequences = foResults.getPageSequences();
147                 for (java.util.Iterator JavaDoc it = pageSequences.iterator(); it.hasNext();) {
148                     PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next();
149                     System.out.println("PageSequence "
150                             + (String.valueOf(pageSequenceResults.getID()).length() > 0
151                                     ? pageSequenceResults.getID() : "<no id>")
152                             + " generated " + pageSequenceResults.getPageCount() + " pages.");
153                 }
154                 System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
155
156             } catch (Exception JavaDoc e) {
157                 System.out.println("failure!");
158                 e.printStackTrace(System.out);
159             } finally {
160                 System.out.println("");
161             }
162         }
163     }
164     
165     /**
166      * Main method. Set up the listener.
167      * @param args command-line arguments
168      */

169     public static void main(String JavaDoc[] args) {
170         System.out.println("FOP MultipleFO2PDF\n");
171         System.out.println("Preparing...");
172         MultipleFO2PDF m = new MultipleFO2PDF();
173         m.listen();
174     }
175
176 }
177
Popular Tags