001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ExampleFO2PDF.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package embedding;
021:
022: // Java
023: import java.io.BufferedOutputStream;
024: import java.io.File;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.io.OutputStream;
028:
029: //JAXP
030: import javax.xml.transform.Transformer;
031: import javax.xml.transform.TransformerFactory;
032: import javax.xml.transform.Source;
033: import javax.xml.transform.Result;
034: import javax.xml.transform.stream.StreamSource;
035: import javax.xml.transform.sax.SAXResult;
036:
037: // FOP
038: import org.apache.fop.apps.FOUserAgent;
039: import org.apache.fop.apps.Fop;
040: import org.apache.fop.apps.FOPException;
041: import org.apache.fop.apps.FopFactory;
042: import org.apache.fop.apps.FormattingResults;
043: import org.apache.fop.apps.MimeConstants;
044: import org.apache.fop.apps.PageSequenceResults;
045:
046: /**
047: * This class demonstrates the conversion of an FO file to PDF using FOP.
048: */
049: public class ExampleFO2PDF {
050:
051: // configure fopFactory as desired
052: private FopFactory fopFactory = FopFactory.newInstance();
053:
054: /**
055: * Converts an FO file to a PDF file using FOP
056: * @param fo the FO file
057: * @param pdf the target PDF file
058: * @throws IOException In case of an I/O problem
059: * @throws FOPException In case of a FOP problem
060: */
061: public void convertFO2PDF(File fo, File pdf) throws IOException,
062: FOPException {
063:
064: OutputStream out = null;
065:
066: try {
067: FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
068: // configure foUserAgent as desired
069:
070: // Setup output stream. Note: Using BufferedOutputStream
071: // for performance reasons (helpful with FileOutputStreams).
072: out = new FileOutputStream(pdf);
073: out = new BufferedOutputStream(out);
074:
075: // Construct fop with desired output format
076: Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
077: foUserAgent, out);
078:
079: // Setup JAXP using identity transformer
080: TransformerFactory factory = TransformerFactory
081: .newInstance();
082: Transformer transformer = factory.newTransformer(); // identity transformer
083:
084: // Setup input stream
085: Source src = new StreamSource(fo);
086:
087: // Resulting SAX events (the generated FO) must be piped through to FOP
088: Result res = new SAXResult(fop.getDefaultHandler());
089:
090: // Start XSLT transformation and FOP processing
091: transformer.transform(src, res);
092:
093: // Result processing
094: FormattingResults foResults = fop.getResults();
095: java.util.List pageSequences = foResults.getPageSequences();
096: for (java.util.Iterator it = pageSequences.iterator(); it
097: .hasNext();) {
098: PageSequenceResults pageSequenceResults = (PageSequenceResults) it
099: .next();
100: System.out.println("PageSequence "
101: + (String.valueOf(pageSequenceResults.getID())
102: .length() > 0 ? pageSequenceResults
103: .getID() : "<no id>") + " generated "
104: + pageSequenceResults.getPageCount()
105: + " pages.");
106: }
107: System.out.println("Generated " + foResults.getPageCount()
108: + " pages in total.");
109:
110: } catch (Exception e) {
111: e.printStackTrace(System.err);
112: System.exit(-1);
113: } finally {
114: out.close();
115: }
116: }
117:
118: /**
119: * Main method.
120: * @param args command-line arguments
121: */
122: public static void main(String[] args) {
123: try {
124: System.out.println("FOP ExampleFO2PDF\n");
125: System.out.println("Preparing...");
126:
127: //Setup directories
128: File baseDir = new File(".");
129: File outDir = new File(baseDir, "out");
130: outDir.mkdirs();
131:
132: //Setup input and output files
133: File fofile = new File(baseDir, "xml/fo/helloworld.fo");
134: //File fofile = new File(baseDir, "../fo/pagination/franklin_2pageseqs.fo");
135: File pdffile = new File(outDir, "ResultFO2PDF.pdf");
136:
137: System.out.println("Input: XSL-FO (" + fofile + ")");
138: System.out.println("Output: PDF (" + pdffile + ")");
139: System.out.println();
140: System.out.println("Transforming...");
141:
142: ExampleFO2PDF app = new ExampleFO2PDF();
143: app.convertFO2PDF(fofile, pdffile);
144:
145: System.out.println("Success!");
146: } catch (Exception e) {
147: e.printStackTrace(System.err);
148: System.exit(-1);
149: }
150: }
151: }
|