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: ExampleXML2PDF.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package embedding;
021:
022: //Java
023: import java.io.File;
024: import java.io.OutputStream;
025:
026: //JAXP
027: import javax.xml.transform.Transformer;
028: import javax.xml.transform.TransformerFactory;
029: import javax.xml.transform.Source;
030: import javax.xml.transform.Result;
031: import javax.xml.transform.stream.StreamSource;
032: import javax.xml.transform.sax.SAXResult;
033:
034: //FOP
035: import org.apache.fop.apps.FOUserAgent;
036: import org.apache.fop.apps.Fop;
037: import org.apache.fop.apps.FopFactory;
038: import org.apache.fop.apps.MimeConstants;
039:
040: /**
041: * This class demonstrates the conversion of an XML file to PDF using
042: * JAXP (XSLT) and FOP (XSL-FO).
043: */
044: public class ExampleXML2PDF {
045:
046: /**
047: * Main method.
048: * @param args command-line arguments
049: */
050: public static void main(String[] args) {
051: try {
052: System.out.println("FOP ExampleXML2PDF\n");
053: System.out.println("Preparing...");
054:
055: // Setup directories
056: File baseDir = new File(".");
057: File outDir = new File(baseDir, "out");
058: outDir.mkdirs();
059:
060: // Setup input and output files
061: File xmlfile = new File(baseDir, "xml/xml/projectteam.xml");
062: File xsltfile = new File(baseDir,
063: "xml/xslt/projectteam2fo.xsl");
064: File pdffile = new File(outDir, "ResultXML2PDF.pdf");
065:
066: System.out.println("Input: XML (" + xmlfile + ")");
067: System.out.println("Stylesheet: " + xsltfile);
068: System.out.println("Output: PDF (" + pdffile + ")");
069: System.out.println();
070: System.out.println("Transforming...");
071:
072: // configure fopFactory as desired
073: FopFactory fopFactory = FopFactory.newInstance();
074:
075: FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
076: // configure foUserAgent as desired
077:
078: // Setup output
079: OutputStream out = new java.io.FileOutputStream(pdffile);
080: out = new java.io.BufferedOutputStream(out);
081:
082: try {
083: // Construct fop with desired output format
084: Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
085: foUserAgent, out);
086:
087: // Setup XSLT
088: TransformerFactory factory = TransformerFactory
089: .newInstance();
090: Transformer transformer = factory
091: .newTransformer(new StreamSource(xsltfile));
092:
093: // Set the value of a <param> in the stylesheet
094: transformer.setParameter("versionParam", "2.0");
095:
096: // Setup input for XSLT transformation
097: Source src = new StreamSource(xmlfile);
098:
099: // Resulting SAX events (the generated FO) must be piped through to FOP
100: Result res = new SAXResult(fop.getDefaultHandler());
101:
102: // Start XSLT transformation and FOP processing
103: transformer.transform(src, res);
104: } finally {
105: out.close();
106: }
107:
108: System.out.println("Success!");
109: } catch (Exception e) {
110: e.printStackTrace(System.err);
111: System.exit(-1);
112: }
113: }
114: }
|