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 1804125 2017-08-04 14:15:05Z ssteiner $ */
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.transform.Result;
30 import javax.xml.transform.Source;
31 import javax.xml.transform.Transformer;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.sax.SAXResult;
34 import javax.xml.transform.stream.StreamSource;
35 
36 import org.apache.fop.apps.FOPException;
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.FormattingResults;
41 import org.apache.fop.apps.MimeConstants;
42 import org.apache.fop.apps.PageSequenceResults;
43 
44 /**
45  * This class demonstrates the conversion of an FO file to PDF using FOP.
46  */
47 public class ExampleFO2PDF {
48 
49     // configure fopFactory as desired
50     private final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
51 
52     /**
53      * Converts an FO file to a PDF file using FOP
54      * @param fo the FO file
55      * @param pdf the target PDF file
56      * @throws IOException In case of an I/O problem
57      * @throws FOPException In case of a FOP problem
58      */
convertFO2PDF(File fo, File pdf)59     public void convertFO2PDF(File fo, File pdf) throws IOException, FOPException {
60 
61         OutputStream out = null;
62 
63         try {
64             FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
65             // configure foUserAgent as desired
66 
67             // Setup output stream.  Note: Using BufferedOutputStream
68             // for performance reasons (helpful with FileOutputStreams).
69             out = new FileOutputStream(pdf);
70             out = new BufferedOutputStream(out);
71 
72             // Construct fop with desired output format
73             Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
74 
75             // Setup JAXP using identity transformer
76             TransformerFactory factory = TransformerFactory.newInstance();
77             Transformer transformer = factory.newTransformer(); // identity transformer
78 
79             // Setup input stream
80             Source src = new StreamSource(fo);
81 
82             // Resulting SAX events (the generated FO) must be piped through to FOP
83             Result res = new SAXResult(fop.getDefaultHandler());
84 
85             // Start XSLT transformation and FOP processing
86             transformer.transform(src, res);
87 
88             // Result processing
89             FormattingResults foResults = fop.getResults();
90             java.util.List pageSequences = foResults.getPageSequences();
91             for (Object pageSequence : pageSequences) {
92                 PageSequenceResults pageSequenceResults = (PageSequenceResults) pageSequence;
93                 System.out.println("PageSequence "
94                         + (String.valueOf(pageSequenceResults.getID()).length() > 0
95                         ? pageSequenceResults.getID() : "<no id>")
96                         + " generated " + pageSequenceResults.getPageCount() + " pages.");
97             }
98             System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
99 
100         } catch (Exception e) {
101             e.printStackTrace(System.err);
102             System.exit(-1);
103         } finally {
104             out.close();
105         }
106     }
107 
108 
109     /**
110      * Main method.
111      * @param args command-line arguments
112      */
main(String[] args)113     public static void main(String[] args) {
114         try {
115             System.out.println("FOP ExampleFO2PDF\n");
116             System.out.println("Preparing...");
117 
118             //Setup directories
119             File baseDir = new File(".");
120             File outDir = new File(baseDir, "out");
121             outDir.mkdirs();
122 
123             //Setup input and output files
124             File fofile = new File(baseDir, "xml/fo/helloworld.fo");
125             //File fofile = new File(baseDir, "../fo/pagination/franklin_2pageseqs.fo");
126             File pdffile = new File(outDir, "ResultFO2PDF.pdf");
127 
128             System.out.println("Input: XSL-FO (" + fofile + ")");
129             System.out.println("Output: PDF (" + pdffile + ")");
130             System.out.println();
131             System.out.println("Transforming...");
132 
133             ExampleFO2PDF app = new ExampleFO2PDF();
134             app.convertFO2PDF(fofile, pdffile);
135 
136             System.out.println("Success!");
137         } catch (Exception e) {
138             e.printStackTrace(System.err);
139             System.exit(-1);
140         }
141     }
142 }
143