| 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$ */
|
| 19 |
|
| 20 |
package embedding;
|
| 21 |
|
| 22 |
// Java
|
| 23 |
import java.io.File;
|
| 24 |
import java.io.OutputStream;
|
| 25 |
import javax.xml.parsers.DocumentBuilderFactory;
|
| 26 |
import javax.xml.parsers.DocumentBuilder;
|
| 27 |
import javax.xml.parsers.ParserConfigurationException;
|
| 28 |
|
| 29 |
//JAXP
|
| 30 |
import javax.xml.transform.Transformer;
|
| 31 |
import javax.xml.transform.TransformerFactory;
|
| 32 |
import javax.xml.transform.Source;
|
| 33 |
import javax.xml.transform.Result;
|
| 34 |
import javax.xml.transform.dom.DOMSource;
|
| 35 |
import javax.xml.transform.sax.SAXResult;
|
| 36 |
|
| 37 |
// DOM
|
| 38 |
import org.w3c.dom.Document;
|
| 39 |
import org.w3c.dom.Element;
|
| 40 |
import org.w3c.dom.Node;
|
| 41 |
import org.w3c.dom.Text;
|
| 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 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 xslfoDoc, File pdf) {
|
| 68 |
try {
|
| 69 |
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
|
| 70 |
// configure foUserAgent as desired
|
| 71 |
|
| 72 |
// Setup output
|
| 73 |
OutputStream out = new java.io.FileOutputStream(pdf);
|
| 74 |
out = new java.io.BufferedOutputStream(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 factory = TransformerFactory.newInstance();
|
| 82 |
Transformer transformer = factory.newTransformer(); // identity transformer
|
| 83 |
|
| 84 |
// Setup input for XSLT transformation
|
| 85 |
Source src = new DOMSource(xslfoDoc);
|
| 86 |
|
| 87 |
// Resulting SAX events (the generated FO) must be piped through to FOP
|
| 88 |
Result res = new SAXResult(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 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[] args) {
|
| 108 |
try {
|
| 109 |
System.out.println("FOP ExampleDOM2PDF\n");
|
| 110 |
|
| 111 |
//Setup directories
|
| 112 |
File baseDir = new File(".");
|
| 113 |
File outDir = new File(baseDir, "out");
|
| 114 |
outDir.mkdirs();
|
| 115 |
|
| 116 |
//Setup output file
|
| 117 |
File pdffile = new File(outDir, "ResultDOM2PDF.pdf");
|
| 118 |
System.out.println("PDF Output File: " + pdffile);
|
| 119 |
System.out.println();
|
| 120 |
|
| 121 |
Document foDoc = buildDOMDocument();
|
| 122 |
|
| 123 |
ExampleDOM2PDF app = new ExampleDOM2PDF();
|
| 124 |
app.convertDOM2PDF(foDoc, pdffile);
|
| 125 |
|
| 126 |
System.out.println("Success!");
|
| 127 |
|
| 128 |
} catch (Exception 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 buildDOMDocument() throws ParserConfigurationException {
|
| 140 |
// Create a sample XSL-FO DOM document
|
| 141 |
Document foDoc = null;
|
| 142 |
Element root = null, ele1 = null, ele2 = null, ele3 = null;
|
| 143 |
|
| 144 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
| 145 |
dbf.setNamespaceAware(true);
|
| 146 |
DocumentBuilder 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 parent, String newNodeName,
|
| 182 |
String textVal) {
|
| 183 |
if (textVal == null) {
|
| 184 |
return;
|
| 185 |
} // use only with text nodes
|
| 186 |
Element newElement = parent.getOwnerDocument().createElementNS(
|
| 187 |
foNS, newNodeName);
|
| 188 |
Text elementText = parent.getOwnerDocument().createTextNode(textVal);
|
| 189 |
newElement.appendChild(elementText);
|
| 190 |
parent.appendChild(newElement);
|
| 191 |
}
|
| 192 |
}
|
| 193 |
|