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: ExampleFO2PDFUsingSAXParser.java 1356646 2012-07-03 09:46:41Z mehdi $ */
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 import javax.xml.parsers.FactoryConfigurationError;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.parsers.SAXParser;
32 import javax.xml.parsers.SAXParserFactory;
33 
34 import org.xml.sax.SAXException;
35 import org.xml.sax.helpers.DefaultHandler;
36 
37 import org.apache.fop.apps.FOUserAgent;
38 import org.apache.fop.apps.Fop;
39 import org.apache.fop.apps.FopFactory;
40 import org.apache.fop.apps.MimeConstants;
41 
42 /**
43  * This class demonstrates the conversion of an FO file to PDF using FOP.
44  * It uses a SAXParser with FOP as the DefaultHandler
45  */
46 public class ExampleFO2PDFUsingSAXParser {
47 
48     // configure fopFactory as desired
49     private final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
50 
51     /**
52      * Converts an FO file to a PDF file using FOP
53      * @param fo the FO file
54      * @param pdf the target PDF file
55      * @throws FactoryConfigurationError In case of a problem with the JAXP factory configuration
56      * @throws ParserConfigurationException In case of a problem with the parser configuration
57      * @throws SAXException In case of a problem during XML processing
58      * @throws IOException In case of an I/O problem
59      */
convertFO2PDF(File fo, File pdf)60     public void convertFO2PDF(File fo, File pdf)
61         throws FactoryConfigurationError,
62                ParserConfigurationException,
63                SAXException, IOException {
64 
65         FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
66         // configure foUserAgent as desired
67 
68         OutputStream out = null;
69 
70         try {
71             // Setup output stream.  Note: Using BufferedOutputStream
72             // for performance reasons (helpful with FileOutputStreams).
73             out = new FileOutputStream(pdf);
74             out = new BufferedOutputStream(out);
75 
76             // Construct fop and setup output format
77             Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
78 
79             // Setup SAX parser
80             // throws FactoryConfigurationError
81             SAXParserFactory factory = SAXParserFactory.newInstance();
82             factory.setNamespaceAware(true);
83             // throws ParserConfigurationException
84             SAXParser parser = factory.newSAXParser();
85 
86             // Obtain FOP's DefaultHandler
87             // throws FOPException
88             DefaultHandler dh = fop.getDefaultHandler();
89 
90             // Start parsing and FOP processing
91             // throws SAXException, IOException
92             parser.parse(fo, dh);
93 
94         } finally {
95             out.close();
96         }
97     }
98 
99 
100     /**
101      * Main method.
102      * @param args command-line arguments
103      */
main(String[] args)104     public static void main(String[] args) {
105         try {
106             System.out.println("FOP ExampleFO2PDFUsingSAXParser\n");
107             System.out.println("Preparing...");
108 
109             //Setup directories
110             File baseDir = new File(".");
111             File outDir = new File(baseDir, "out");
112             outDir.mkdirs();
113 
114             //Setup input and output files
115             File fofile = new File(baseDir, "xml/fo/helloworld.fo");
116             File pdffile = new File(outDir, "ResultFO2PDFUsingSAXParser.pdf");
117 
118             System.out.println("Input: XSL-FO (" + fofile + ")");
119             System.out.println("Output: PDF (" + pdffile + ")");
120             System.out.println();
121             System.out.println("Transforming...");
122 
123             ExampleFO2PDFUsingSAXParser app = new ExampleFO2PDFUsingSAXParser();
124             app.convertFO2PDF(fofile, pdffile);
125 
126             System.out.println("Success!");
127         } catch (Exception e) {
128             e.printStackTrace(System.err);
129             System.exit(-1);
130         }
131     }
132 }
133 
134