1 17 18 19 20 package embedding; 21 22 import java.io.File ; 24 import java.io.OutputStream ; 25 import java.io.IOException ; 26 27 import javax.xml.transform.Transformer ; 29 import javax.xml.transform.TransformerFactory ; 30 import javax.xml.transform.TransformerException ; 31 import javax.xml.transform.Source ; 32 import javax.xml.transform.Result ; 33 import javax.xml.transform.stream.StreamSource ; 34 import javax.xml.transform.sax.SAXResult ; 35 36 import org.apache.fop.apps.FOUserAgent; 38 import org.apache.fop.apps.Fop; 39 import org.apache.fop.apps.FOPException; 40 import org.apache.fop.apps.FopFactory; 41 import org.apache.fop.apps.MimeConstants; 42 43 import embedding.model.ProjectTeam; 44 45 49 public class ExampleObj2PDF { 50 51 private FopFactory fopFactory = FopFactory.newInstance(); 53 54 63 public void convertProjectTeam2PDF(ProjectTeam team, File xslt, File pdf) 64 throws IOException , FOPException, TransformerException { 65 66 FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 67 69 OutputStream out = new java.io.FileOutputStream (pdf); 71 out = new java.io.BufferedOutputStream (out); 72 try { 73 Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); 75 76 TransformerFactory factory = TransformerFactory.newInstance(); 78 Transformer transformer = factory.newTransformer(new StreamSource (xslt)); 79 80 Source src = team.getSourceForProjectTeam(); 82 83 Result res = new SAXResult (fop.getDefaultHandler()); 85 86 transformer.transform(src, res); 88 } finally { 89 out.close(); 90 } 91 } 92 93 94 98 public static void main(String [] args) { 99 try { 100 System.out.println("FOP ExampleObj2PDF\n"); 101 System.out.println("Preparing..."); 102 103 File baseDir = new File ("."); 105 File outDir = new File (baseDir, "out"); 106 outDir.mkdirs(); 107 108 File xsltfile = new File (baseDir, "xml/xslt/projectteam2fo.xsl"); 110 File pdffile = new File (outDir, "ResultObj2PDF.pdf"); 111 112 System.out.println("Input: a ProjectTeam object"); 113 System.out.println("Stylesheet: " + xsltfile); 114 System.out.println("Output: PDF (" + pdffile + ")"); 115 System.out.println(); 116 System.out.println("Transforming..."); 117 118 ExampleObj2PDF app = new ExampleObj2PDF(); 119 app.convertProjectTeam2PDF(ExampleObj2XML.createSampleProjectTeam(), xsltfile, pdffile); 120 121 System.out.println("Success!"); 122 } catch (Exception e) { 123 e.printStackTrace(System.err); 124 System.exit(-1); 125 } 126 } 127 } 128 | Popular Tags |