1 17 18 19 20 package embedding; 21 22 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 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 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 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 54 public class ExampleDOM2PDF { 55 56 private FopFactory fopFactory = FopFactory.newInstance(); 58 59 60 protected static String foNS = "http://www.w3.org/1999/XSL/Format"; 61 62 67 public void convertDOM2PDF(Document xslfoDoc, File pdf) { 68 try { 69 FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 70 72 OutputStream out = new java.io.FileOutputStream (pdf); 74 out = new java.io.BufferedOutputStream (out); 75 76 try { 77 Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); 79 80 TransformerFactory factory = TransformerFactory.newInstance(); 82 Transformer transformer = factory.newTransformer(); 84 Source src = new DOMSource (xslfoDoc); 86 87 Result res = new SAXResult (fop.getDefaultHandler()); 89 90 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 107 public static void main(String [] args) { 108 try { 109 System.out.println("FOP ExampleDOM2PDF\n"); 110 111 File baseDir = new File ("."); 113 File outDir = new File (baseDir, "out"); 114 outDir.mkdirs(); 115 116 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 139 private static Document buildDOMDocument() throws ParserConfigurationException { 140 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 181 protected static void addElement(Node parent, String newNodeName, 182 String textVal) { 183 if (textVal == null) { 184 return; 185 } 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 194 | Popular Tags |