| 1 17 18 19 20 package embedding; 21 22 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 29 import javax.xml.parsers.SAXParserFactory ; 31 import javax.xml.parsers.FactoryConfigurationError ; 32 import javax.xml.parsers.SAXParser ; 33 import javax.xml.parsers.ParserConfigurationException ; 34 35 import org.xml.sax.helpers.DefaultHandler ; 37 import org.xml.sax.SAXException ; 38 39 import org.apache.fop.apps.FOUserAgent; 41 import org.apache.fop.apps.Fop; 42 import org.apache.fop.apps.FopFactory; 43 import org.apache.fop.apps.MimeConstants; 44 45 49 public class ExampleFO2PDFUsingSAXParser { 50 51 private FopFactory fopFactory = FopFactory.newInstance(); 53 54 63 public void convertFO2PDF(File fo, File pdf) 64 throws FactoryConfigurationError , 65 ParserConfigurationException , 66 SAXException , IOException { 67 68 FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 69 71 OutputStream out = null; 72 73 try { 74 out = new FileOutputStream (pdf); 77 out = new BufferedOutputStream (out); 78 79 Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); 81 82 SAXParserFactory factory = SAXParserFactory.newInstance(); 85 factory.setNamespaceAware(true); 86 SAXParser parser = factory.newSAXParser(); 88 89 DefaultHandler dh = fop.getDefaultHandler(); 92 93 parser.parse(fo, dh); 96 97 } finally { 98 out.close(); 99 } 100 } 101 102 103 107 public static void main(String [] args) { 108 try { 109 System.out.println("FOP ExampleFO2PDFUsingSAXParser\n"); 110 System.out.println("Preparing..."); 111 112 File baseDir = new File ("."); 114 File outDir = new File (baseDir, "out"); 115 outDir.mkdirs(); 116 117 File fofile = new File (baseDir, "xml/fo/helloworld.fo"); 119 File pdffile = new File (outDir, "ResultFO2PDFUsingSAXParser.pdf"); 120 121 System.out.println("Input: XSL-FO (" + fofile + ")"); 122 System.out.println("Output: PDF (" + pdffile + ")"); 123 System.out.println(); 124 System.out.println("Transforming..."); 125 126 ExampleFO2PDFUsingSAXParser app = new ExampleFO2PDFUsingSAXParser(); 127 app.convertFO2PDF(fofile, pdffile); 128 129 System.out.println("Success!"); 130 } catch (Exception e) { 131 e.printStackTrace(System.err); 132 System.exit(-1); 133 } 134 } 135 } 136 137 | Popular Tags |