/[Apache-SVN]/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleFO2PDF.java
ViewVC logotype

Contents of /xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleFO2PDF.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 679326 - (show annotations)
Thu Jul 24 09:35:34 2008 UTC (16 months ago) by vhennebert
File size: 5247 byte(s)
Fed up with all those trailing whitespaces. Let's remove them all (once... and for all?)
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$ */
19
20 package embedding;
21
22 // Java
23 import java.io.BufferedOutputStream;
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.OutputStream;
28
29 //JAXP
30 import javax.xml.transform.Transformer;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.Source;
33 import javax.xml.transform.Result;
34 import javax.xml.transform.stream.StreamSource;
35 import javax.xml.transform.sax.SAXResult;
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 fo, File pdf) throws IOException, FOPException {
63
64 OutputStream 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(pdf);
73 out = new BufferedOutputStream(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 factory = TransformerFactory.newInstance();
80 Transformer transformer = factory.newTransformer(); // identity transformer
81
82 // Setup input stream
83 Source src = new StreamSource(fo);
84
85 // Resulting SAX events (the generated FO) must be piped through to FOP
86 Result res = new SAXResult(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 pageSequences = foResults.getPageSequences();
94 for (java.util.Iterator 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 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[] args) {
117 try {
118 System.out.println("FOP ExampleFO2PDF\n");
119 System.out.println("Preparing...");
120
121 //Setup directories
122 File baseDir = new File(".");
123 File outDir = new File(baseDir, "out");
124 outDir.mkdirs();
125
126 //Setup input and output files
127 File fofile = new File(baseDir, "xml/fo/helloworld.fo");
128 //File fofile = new File(baseDir, "../fo/pagination/franklin_2pageseqs.fo");
129 File pdffile = new File(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 e) {
141 e.printStackTrace(System.err);
142 System.exit(-1);
143 }
144 }
145 }

Properties

Name Value
svn:eol-style native
svn:keywords Id

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2