| 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 |
|
| 26 |
//JAXP
|
| 27 |
import javax.xml.transform.Transformer;
|
| 28 |
import javax.xml.transform.TransformerFactory;
|
| 29 |
import javax.xml.transform.Source;
|
| 30 |
import javax.xml.transform.Result;
|
| 31 |
import javax.xml.transform.stream.StreamSource;
|
| 32 |
import javax.xml.transform.sax.SAXResult;
|
| 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[] args) {
|
| 51 |
try {
|
| 52 |
System.out.println("FOP ExampleXML2PDF\n");
|
| 53 |
System.out.println("Preparing...");
|
| 54 |
|
| 55 |
// Setup directories
|
| 56 |
File baseDir = new File(".");
|
| 57 |
File outDir = new File(baseDir, "out");
|
| 58 |
outDir.mkdirs();
|
| 59 |
|
| 60 |
// Setup input and output files
|
| 61 |
File xmlfile = new File(baseDir, "xml/xml/projectteam.xml");
|
| 62 |
File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
|
| 63 |
File pdffile = new File(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 out = new java.io.FileOutputStream(pdffile);
|
| 79 |
out = new java.io.BufferedOutputStream(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 factory = TransformerFactory.newInstance();
|
| 87 |
Transformer transformer = factory.newTransformer(new StreamSource(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 src = new StreamSource(xmlfile);
|
| 94 |
|
| 95 |
// Resulting SAX events (the generated FO) must be piped through to FOP
|
| 96 |
Result res = new SAXResult(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 e) {
|
| 106 |
e.printStackTrace(System.err);
|
| 107 |
System.exit(-1);
|
| 108 |
}
|
| 109 |
}
|
| 110 |
}
|