KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > ExampleFO2PDF


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: ExampleFO2PDF.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.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 //JAXP
30
import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerFactory JavaDoc;
32 import javax.xml.transform.Source JavaDoc;
33 import javax.xml.transform.Result JavaDoc;
34 import javax.xml.transform.stream.StreamSource JavaDoc;
35 import javax.xml.transform.sax.SAXResult JavaDoc;
36
37
38 // FOP
39
import org.apache.fop.apps.FOUserAgent;
40 import org.apache.fop.apps.Fop;
41 import org.apache.fop.apps.FOPException;
42 import org.apache.fop.apps.FopFactory;
43 import org.apache.fop.apps.FormattingResults;
44 import org.apache.fop.apps.MimeConstants;
45 import org.apache.fop.apps.PageSequenceResults;
46
47 /**
48  * This class demonstrates the conversion of an FO file to PDF using FOP.
49  */

50 public class ExampleFO2PDF {
51
52     // configure fopFactory as desired
53
private FopFactory fopFactory = FopFactory.newInstance();
54
55     /**
56      * Converts an FO file to a PDF file using FOP
57      * @param fo the FO file
58      * @param pdf the target PDF file
59      * @throws IOException In case of an I/O problem
60      * @throws FOPException In case of a FOP problem
61      */

62     public void convertFO2PDF(File JavaDoc fo, File JavaDoc pdf) throws IOException JavaDoc, FOPException {
63         
64         OutputStream JavaDoc out = null;
65         
66         try {
67             FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
68             // configure foUserAgent as desired
69

70             // Setup output stream. Note: Using BufferedOutputStream
71
// for performance reasons (helpful with FileOutputStreams).
72
out = new FileOutputStream JavaDoc(pdf);
73             out = new BufferedOutputStream JavaDoc(out);
74
75             // Construct fop with desired output format
76
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
77
78             // Setup JAXP using identity transformer
79
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
80             Transformer JavaDoc transformer = factory.newTransformer(); // identity transformer
81

82             // Setup input stream
83
Source JavaDoc src = new StreamSource JavaDoc(fo);
84
85             // Resulting SAX events (the generated FO) must be piped through to FOP
86
Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
87             
88             // Start XSLT transformation and FOP processing
89
transformer.transform(src, res);
90             
91             // Result processing
92
FormattingResults foResults = fop.getResults();
93             java.util.List JavaDoc pageSequences = foResults.getPageSequences();
94             for (java.util.Iterator JavaDoc it = pageSequences.iterator(); it.hasNext();) {
95                 PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next();
96                 System.out.println("PageSequence "
97                         + (String.valueOf(pageSequenceResults.getID()).length() > 0
98                                 ? pageSequenceResults.getID() : "<no id>")
99                         + " generated " + pageSequenceResults.getPageCount() + " pages.");
100             }
101             System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
102
103         } catch (Exception JavaDoc e) {
104             e.printStackTrace(System.err);
105             System.exit(-1);
106         } finally {
107             out.close();
108         }
109     }
110
111
112     /**
113      * Main method.
114      * @param args command-line arguments
115      */

116     public static void main(String JavaDoc[] args) {
117         try {
118             System.out.println("FOP ExampleFO2PDF\n");
119             System.out.println("Preparing...");
120             
121             //Setup directories
122
File JavaDoc baseDir = new File JavaDoc(".");
123             File JavaDoc outDir = new File JavaDoc(baseDir, "out");
124             outDir.mkdirs();
125
126             //Setup input and output files
127
File JavaDoc fofile = new File JavaDoc(baseDir, "xml/fo/helloworld.fo");
128             //File fofile = new File(baseDir, "../fo/pagination/franklin_2pageseqs.fo");
129
File JavaDoc pdffile = new File JavaDoc(outDir, "ResultFO2PDF.pdf");
130
131             System.out.println("Input: XSL-FO (" + fofile + ")");
132             System.out.println("Output: PDF (" + pdffile + ")");
133             System.out.println();
134             System.out.println("Transforming...");
135             
136             ExampleFO2PDF app = new ExampleFO2PDF();
137             app.convertFO2PDF(fofile, pdffile);
138             
139             System.out.println("Success!");
140         } catch (Exception JavaDoc e) {
141             e.printStackTrace(System.err);
142             System.exit(-1);
143         }
144     }
145 }
146
Popular Tags