KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > ExampleDOM2PDF


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: ExampleDOM2PDF.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 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import javax.xml.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28
29 //JAXP
30
import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerFactory JavaDoc;
32 import javax.xml.transform.Source JavaDoc;
33 import javax.xml.transform.Result JavaDoc;
34 import javax.xml.transform.dom.DOMSource JavaDoc;
35 import javax.xml.transform.sax.SAXResult JavaDoc;
36
37 // DOM
38
import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.Text JavaDoc;
42
43 // FOP
44
import org.apache.fop.apps.FOUserAgent;
45 import org.apache.fop.apps.Fop;
46 import org.apache.fop.apps.FopFactory;
47 import org.apache.fop.apps.MimeConstants;
48
49
50 /**
51  * This class demonstrates the conversion of a DOM Document to PDF
52  * using JAXP (XSLT) and FOP (XSL-FO).
53  */

54 public class ExampleDOM2PDF {
55
56     // configure fopFactory as desired
57
private FopFactory fopFactory = FopFactory.newInstance();
58     
59     /** xsl-fo namespace URI */
60     protected static String JavaDoc foNS = "http://www.w3.org/1999/XSL/Format";
61
62     /**
63      * Converts a DOM Document to a PDF file using FOP.
64      * @param xslfoDoc the DOM Document
65      * @param pdf the target PDF file
66      */

67     public void convertDOM2PDF(Document JavaDoc xslfoDoc, File JavaDoc pdf) {
68         try {
69             FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
70             // configure foUserAgent as desired
71

72             // Setup output
73
OutputStream JavaDoc out = new java.io.FileOutputStream JavaDoc(pdf);
74             out = new java.io.BufferedOutputStream JavaDoc(out);
75     
76             try {
77                 // Construct fop with desired output format and output stream
78
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
79                 
80                 // Setup Identity Transformer
81
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
82                 Transformer JavaDoc transformer = factory.newTransformer(); // identity transformer
83

84                 // Setup input for XSLT transformation
85
Source JavaDoc src = new DOMSource JavaDoc(xslfoDoc);
86                 
87                 // Resulting SAX events (the generated FO) must be piped through to FOP
88
Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
89                 
90                 // Start XSLT transformation and FOP processing
91
transformer.transform(src, res);
92             } finally {
93                 out.close();
94             }
95             
96         } catch (Exception JavaDoc e) {
97             e.printStackTrace(System.err);
98             System.exit(-1);
99         }
100
101     }
102
103     /**
104      * Main method.
105      * @param args command-line arguments
106      */

107     public static void main(String JavaDoc[] args) {
108         try {
109             System.out.println("FOP ExampleDOM2PDF\n");
110             
111             //Setup directories
112
File JavaDoc baseDir = new File JavaDoc(".");
113             File JavaDoc outDir = new File JavaDoc(baseDir, "out");
114             outDir.mkdirs();
115             
116             //Setup output file
117
File JavaDoc pdffile = new File JavaDoc(outDir, "ResultDOM2PDF.pdf");
118             System.out.println("PDF Output File: " + pdffile);
119             System.out.println();
120             
121             Document JavaDoc foDoc = buildDOMDocument();
122             
123             ExampleDOM2PDF app = new ExampleDOM2PDF();
124             app.convertDOM2PDF(foDoc, pdffile);
125             
126             System.out.println("Success!");
127             
128         } catch (Exception JavaDoc e) {
129             e.printStackTrace(System.err);
130             System.exit(-1);
131         }
132     }
133
134     /**
135      * Builds the example FO document as a DOM in memory.
136      * @return the FO document
137      * @throws ParserConfigurationException In case there is a problem creating a DOM document
138      */

139     private static Document JavaDoc buildDOMDocument() throws ParserConfigurationException JavaDoc {
140         // Create a sample XSL-FO DOM document
141
Document JavaDoc foDoc = null;
142         Element JavaDoc root = null, ele1 = null, ele2 = null, ele3 = null;
143         
144         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
145         dbf.setNamespaceAware(true);
146         DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
147         foDoc = db.newDocument();
148         
149         root = foDoc.createElementNS(foNS, "fo:root");
150         foDoc.appendChild(root);
151         
152         ele1 = foDoc.createElementNS(foNS, "fo:layout-master-set");
153         root.appendChild(ele1);
154         ele2 = foDoc.createElementNS(foNS, "fo:simple-page-master");
155         ele1.appendChild(ele2);
156         ele2.setAttributeNS(null, "master-name", "letter");
157         ele2.setAttributeNS(null, "page-height", "11in");
158         ele2.setAttributeNS(null, "page-width", "8.5in");
159         ele2.setAttributeNS(null, "margin-top", "1in");
160         ele2.setAttributeNS(null, "margin-bottom", "1in");
161         ele2.setAttributeNS(null, "margin-left", "1in");
162         ele2.setAttributeNS(null, "margin-right", "1in");
163         ele3 = foDoc.createElementNS(foNS, "fo:region-body");
164         ele2.appendChild(ele3);
165         ele1 = foDoc.createElementNS(foNS, "fo:page-sequence");
166         root.appendChild(ele1);
167         ele1.setAttributeNS(null, "master-reference", "letter");
168         ele2 = foDoc.createElementNS(foNS, "fo:flow");
169         ele1.appendChild(ele2);
170         ele2.setAttributeNS(null, "flow-name", "xsl-region-body");
171         addElement(ele2, "fo:block", "Hello World!");
172         return foDoc;
173     }
174
175     /**
176      * Adds an element to the DOM.
177      * @param parent parent node to attach the new element to
178      * @param newNodeName name of the new node
179      * @param textVal content of the element
180      */

181     protected static void addElement(Node JavaDoc parent, String JavaDoc newNodeName,
182                                 String JavaDoc textVal) {
183         if (textVal == null) {
184             return;
185         } // use only with text nodes
186
Element JavaDoc newElement = parent.getOwnerDocument().createElementNS(
187                                         foNS, newNodeName);
188         Text JavaDoc elementText = parent.getOwnerDocument().createTextNode(textVal);
189         newElement.appendChild(elementText);
190         parent.appendChild(newElement);
191     }
192 }
193
194
Popular Tags