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: ExampleDOM2PDF.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: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.parsers.DocumentBuilder;
027: import javax.xml.parsers.ParserConfigurationException;
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.dom.DOMSource;
035: import javax.xml.transform.sax.SAXResult;
036:
037: // DOM
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Element;
040: import org.w3c.dom.Node;
041: import org.w3c.dom.Text;
042:
043: // FOP
044: import org.apache.fop.apps.FOUserAgent;
045: import org.apache.fop.apps.Fop;
046: import org.apache.fop.apps.FopFactory;
047: import org.apache.fop.apps.MimeConstants;
048:
049: /**
050: * This class demonstrates the conversion of a DOM Document to PDF
051: * using JAXP (XSLT) and FOP (XSL-FO).
052: */
053: public class ExampleDOM2PDF {
054:
055: // configure fopFactory as desired
056: private FopFactory fopFactory = FopFactory.newInstance();
057:
058: /** xsl-fo namespace URI */
059: protected static String foNS = "http://www.w3.org/1999/XSL/Format";
060:
061: /**
062: * Converts a DOM Document to a PDF file using FOP.
063: * @param xslfoDoc the DOM Document
064: * @param pdf the target PDF file
065: */
066: public void convertDOM2PDF(Document xslfoDoc, File pdf) {
067: try {
068: FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
069: // configure foUserAgent as desired
070:
071: // Setup output
072: OutputStream out = new java.io.FileOutputStream(pdf);
073: out = new java.io.BufferedOutputStream(out);
074:
075: try {
076: // Construct fop with desired output format and output stream
077: Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
078: foUserAgent, out);
079:
080: // Setup Identity Transformer
081: TransformerFactory factory = TransformerFactory
082: .newInstance();
083: Transformer transformer = factory.newTransformer(); // identity transformer
084:
085: // Setup input for XSLT transformation
086: Source src = new DOMSource(xslfoDoc);
087:
088: // Resulting SAX events (the generated FO) must be piped through to FOP
089: Result res = new SAXResult(fop.getDefaultHandler());
090:
091: // Start XSLT transformation and FOP processing
092: transformer.transform(src, res);
093: } finally {
094: out.close();
095: }
096:
097: } catch (Exception e) {
098: e.printStackTrace(System.err);
099: System.exit(-1);
100: }
101:
102: }
103:
104: /**
105: * Main method.
106: * @param args command-line arguments
107: */
108: public static void main(String[] args) {
109: try {
110: System.out.println("FOP ExampleDOM2PDF\n");
111:
112: //Setup directories
113: File baseDir = new File(".");
114: File outDir = new File(baseDir, "out");
115: outDir.mkdirs();
116:
117: //Setup output file
118: File pdffile = new File(outDir, "ResultDOM2PDF.pdf");
119: System.out.println("PDF Output File: " + pdffile);
120: System.out.println();
121:
122: Document foDoc = buildDOMDocument();
123:
124: ExampleDOM2PDF app = new ExampleDOM2PDF();
125: app.convertDOM2PDF(foDoc, pdffile);
126:
127: System.out.println("Success!");
128:
129: } catch (Exception e) {
130: e.printStackTrace(System.err);
131: System.exit(-1);
132: }
133: }
134:
135: /**
136: * Builds the example FO document as a DOM in memory.
137: * @return the FO document
138: * @throws ParserConfigurationException In case there is a problem creating a DOM document
139: */
140: private static Document buildDOMDocument()
141: throws ParserConfigurationException {
142: // Create a sample XSL-FO DOM document
143: Document foDoc = null;
144: Element root = null, ele1 = null, ele2 = null, ele3 = null;
145:
146: DocumentBuilderFactory dbf = DocumentBuilderFactory
147: .newInstance();
148: dbf.setNamespaceAware(true);
149: DocumentBuilder db = dbf.newDocumentBuilder();
150: foDoc = db.newDocument();
151:
152: root = foDoc.createElementNS(foNS, "fo:root");
153: foDoc.appendChild(root);
154:
155: ele1 = foDoc.createElementNS(foNS, "fo:layout-master-set");
156: root.appendChild(ele1);
157: ele2 = foDoc.createElementNS(foNS, "fo:simple-page-master");
158: ele1.appendChild(ele2);
159: ele2.setAttributeNS(null, "master-name", "letter");
160: ele2.setAttributeNS(null, "page-height", "11in");
161: ele2.setAttributeNS(null, "page-width", "8.5in");
162: ele2.setAttributeNS(null, "margin-top", "1in");
163: ele2.setAttributeNS(null, "margin-bottom", "1in");
164: ele2.setAttributeNS(null, "margin-left", "1in");
165: ele2.setAttributeNS(null, "margin-right", "1in");
166: ele3 = foDoc.createElementNS(foNS, "fo:region-body");
167: ele2.appendChild(ele3);
168: ele1 = foDoc.createElementNS(foNS, "fo:page-sequence");
169: root.appendChild(ele1);
170: ele1.setAttributeNS(null, "master-reference", "letter");
171: ele2 = foDoc.createElementNS(foNS, "fo:flow");
172: ele1.appendChild(ele2);
173: ele2.setAttributeNS(null, "flow-name", "xsl-region-body");
174: addElement(ele2, "fo:block", "Hello World!");
175: return foDoc;
176: }
177:
178: /**
179: * Adds an element to the DOM.
180: * @param parent parent node to attach the new element to
181: * @param newNodeName name of the new node
182: * @param textVal content of the element
183: */
184: protected static void addElement(Node parent, String newNodeName,
185: String textVal) {
186: if (textVal == null) {
187: return;
188: } // use only with text nodes
189: Element newElement = parent.getOwnerDocument().createElementNS(
190: foNS, newNodeName);
191: Text elementText = parent.getOwnerDocument().createTextNode(
192: textVal);
193: newElement.appendChild(elementText);
194: parent.appendChild(newElement);
195: }
196: }
|