KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > ExampleXML2PDF


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: ExampleXML2PDF.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package embedding;
21
22 //Java
23
import java.io.File JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 //JAXP
27
import javax.xml.transform.Transformer JavaDoc;
28 import javax.xml.transform.TransformerFactory JavaDoc;
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.Result JavaDoc;
31 import javax.xml.transform.stream.StreamSource JavaDoc;
32 import javax.xml.transform.sax.SAXResult JavaDoc;
33
34 //FOP
35
import org.apache.fop.apps.FOUserAgent;
36 import org.apache.fop.apps.Fop;
37 import org.apache.fop.apps.FopFactory;
38 import org.apache.fop.apps.MimeConstants;
39
40 /**
41  * This class demonstrates the conversion of an XML file to PDF using
42  * JAXP (XSLT) and FOP (XSL-FO).
43  */

44 public class ExampleXML2PDF {
45
46     /**
47      * Main method.
48      * @param args command-line arguments
49      */

50     public static void main(String JavaDoc[] args) {
51         try {
52             System.out.println("FOP ExampleXML2PDF\n");
53             System.out.println("Preparing...");
54
55             // Setup directories
56
File JavaDoc baseDir = new File JavaDoc(".");
57             File JavaDoc outDir = new File JavaDoc(baseDir, "out");
58             outDir.mkdirs();
59
60             // Setup input and output files
61
File JavaDoc xmlfile = new File JavaDoc(baseDir, "xml/xml/projectteam.xml");
62             File JavaDoc xsltfile = new File JavaDoc(baseDir, "xml/xslt/projectteam2fo.xsl");
63             File JavaDoc pdffile = new File JavaDoc(outDir, "ResultXML2PDF.pdf");
64
65             System.out.println("Input: XML (" + xmlfile + ")");
66             System.out.println("Stylesheet: " + xsltfile);
67             System.out.println("Output: PDF (" + pdffile + ")");
68             System.out.println();
69             System.out.println("Transforming...");
70             
71             // configure fopFactory as desired
72
FopFactory fopFactory = FopFactory.newInstance();
73
74             FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
75             // configure foUserAgent as desired
76

77             // Setup output
78
OutputStream JavaDoc out = new java.io.FileOutputStream JavaDoc(pdffile);
79             out = new java.io.BufferedOutputStream JavaDoc(out);
80             
81             try {
82                 // Construct fop with desired output format
83
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
84     
85                 // Setup XSLT
86
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
87                 Transformer JavaDoc transformer = factory.newTransformer(new StreamSource JavaDoc(xsltfile));
88                 
89                 // Set the value of a <param> in the stylesheet
90
transformer.setParameter("versionParam", "2.0");
91             
92                 // Setup input for XSLT transformation
93
Source JavaDoc src = new StreamSource JavaDoc(xmlfile);
94             
95                 // Resulting SAX events (the generated FO) must be piped through to FOP
96
Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
97     
98                 // Start XSLT transformation and FOP processing
99
transformer.transform(src, res);
100             } finally {
101                 out.close();
102             }
103             
104             System.out.println("Success!");
105         } catch (Exception JavaDoc e) {
106             e.printStackTrace(System.err);
107             System.exit(-1);
108         }
109     }
110 }
111
Popular Tags