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 1356646 2012-07-03 09:46:41Z mehdi $ */ 19 20 package embedding; 21 22 //Java 23 import java.io.File; 24 import java.io.OutputStream; 25 26 import javax.xml.transform.Result; 27 import javax.xml.transform.Source; 28 import javax.xml.transform.Transformer; 29 import javax.xml.transform.TransformerFactory; 30 import javax.xml.transform.sax.SAXResult; 31 import javax.xml.transform.stream.StreamSource; 32 33 import org.apache.fop.apps.FOUserAgent; 34 import org.apache.fop.apps.Fop; 35 import org.apache.fop.apps.FopFactory; 36 import org.apache.fop.apps.MimeConstants; 37 38 /** 39 * This class demonstrates the conversion of an XML file to PDF using 40 * JAXP (XSLT) and FOP (XSL-FO). 41 */ 42 public class ExampleXML2PDF { 43 44 /** 45 * Main method. 46 * @param args command-line arguments 47 */ main(String[] args)48 public static void main(String[] args) { 49 try { 50 System.out.println("FOP ExampleXML2PDF\n"); 51 System.out.println("Preparing..."); 52 53 // Setup directories 54 File baseDir = new File("."); 55 File outDir = new File(baseDir, "out"); 56 outDir.mkdirs(); 57 58 // Setup input and output files 59 File xmlfile = new File(baseDir, "xml/xml/projectteam.xml"); 60 File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl"); 61 File pdffile = new File(outDir, "ResultXML2PDF.pdf"); 62 63 System.out.println("Input: XML (" + xmlfile + ")"); 64 System.out.println("Stylesheet: " + xsltfile); 65 System.out.println("Output: PDF (" + pdffile + ")"); 66 System.out.println(); 67 System.out.println("Transforming..."); 68 69 // configure fopFactory as desired 70 final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); 71 72 FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 73 // configure foUserAgent as desired 74 75 // Setup output 76 OutputStream out = new java.io.FileOutputStream(pdffile); 77 out = new java.io.BufferedOutputStream(out); 78 79 try { 80 // Construct fop with desired output format 81 Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); 82 83 // Setup XSLT 84 TransformerFactory factory = TransformerFactory.newInstance(); 85 Transformer transformer = factory.newTransformer(new StreamSource(xsltfile)); 86 87 // Set the value of a <param> in the stylesheet 88 transformer.setParameter("versionParam", "2.0"); 89 90 // Setup input for XSLT transformation 91 Source src = new StreamSource(xmlfile); 92 93 // Resulting SAX events (the generated FO) must be piped through to FOP 94 Result res = new SAXResult(fop.getDefaultHandler()); 95 96 // Start XSLT transformation and FOP processing 97 transformer.transform(src, res); 98 } finally { 99 out.close(); 100 } 101 102 System.out.println("Success!"); 103 } catch (Exception e) { 104 e.printStackTrace(System.err); 105 System.exit(-1); 106 } 107 } 108 } 109