You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExampleDOM2PDF.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /* $Id$ */
  18. package embedding;
  19. // Java
  20. import java.io.File;
  21. import java.io.OutputStream;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import javax.xml.parsers.DocumentBuilder;
  24. import javax.xml.parsers.ParserConfigurationException;
  25. //JAXP
  26. import javax.xml.transform.Transformer;
  27. import javax.xml.transform.TransformerFactory;
  28. import javax.xml.transform.Source;
  29. import javax.xml.transform.Result;
  30. import javax.xml.transform.dom.DOMSource;
  31. import javax.xml.transform.sax.SAXResult;
  32. // DOM
  33. import org.w3c.dom.Document;
  34. import org.w3c.dom.Element;
  35. import org.w3c.dom.Node;
  36. import org.w3c.dom.Text;
  37. // FOP
  38. import org.apache.fop.apps.FOUserAgent;
  39. import org.apache.fop.apps.Fop;
  40. import org.apache.fop.apps.FopFactory;
  41. import org.apache.fop.apps.MimeConstants;
  42. /**
  43. * This class demonstrates the conversion of a DOM Document to PDF
  44. * using JAXP (XSLT) and FOP (XSL-FO).
  45. */
  46. public class ExampleDOM2PDF {
  47. // configure fopFactory as desired
  48. private FopFactory fopFactory = FopFactory.newInstance();
  49. /** xsl-fo namespace URI */
  50. protected static String foNS = "http://www.w3.org/1999/XSL/Format";
  51. /**
  52. * Converts a DOM Document to a PDF file using FOP.
  53. * @param xslfoDoc the DOM Document
  54. * @param pdf the target PDF file
  55. */
  56. public void convertDOM2PDF(Document xslfoDoc, File pdf) {
  57. try {
  58. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  59. // configure foUserAgent as desired
  60. // Setup output
  61. OutputStream out = new java.io.FileOutputStream(pdf);
  62. out = new java.io.BufferedOutputStream(out);
  63. try {
  64. // Construct fop with desired output format and output stream
  65. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
  66. // Setup Identity Transformer
  67. TransformerFactory factory = TransformerFactory.newInstance();
  68. Transformer transformer = factory.newTransformer(); // identity transformer
  69. // Setup input for XSLT transformation
  70. Source src = new DOMSource(xslfoDoc);
  71. // Resulting SAX events (the generated FO) must be piped through to FOP
  72. Result res = new SAXResult(fop.getDefaultHandler());
  73. // Start XSLT transformation and FOP processing
  74. transformer.transform(src, res);
  75. } finally {
  76. out.close();
  77. }
  78. } catch (Exception e) {
  79. e.printStackTrace(System.err);
  80. System.exit(-1);
  81. }
  82. }
  83. /**
  84. * Main method.
  85. * @param args command-line arguments
  86. */
  87. public static void main(String[] args) {
  88. try {
  89. System.out.println("FOP ExampleDOM2PDF\n");
  90. //Setup directories
  91. File baseDir = new File(".");
  92. File outDir = new File(baseDir, "out");
  93. outDir.mkdirs();
  94. //Setup output file
  95. File pdffile = new File(outDir, "ResultDOM2PDF.pdf");
  96. System.out.println("PDF Output File: " + pdffile);
  97. System.out.println();
  98. Document foDoc = buildDOMDocument();
  99. ExampleDOM2PDF app = new ExampleDOM2PDF();
  100. app.convertDOM2PDF(foDoc, pdffile);
  101. System.out.println("Success!");
  102. } catch (Exception e) {
  103. e.printStackTrace(System.err);
  104. System.exit(-1);
  105. }
  106. }
  107. /**
  108. * Builds the example FO document as a DOM in memory.
  109. * @return the FO document
  110. * @throws ParserConfigurationException In case there is a problem creating a DOM document
  111. */
  112. private static Document buildDOMDocument() throws ParserConfigurationException {
  113. // Create a sample XSL-FO DOM document
  114. Document foDoc = null;
  115. Element root = null, ele1 = null, ele2 = null, ele3 = null;
  116. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  117. dbf.setNamespaceAware(true);
  118. DocumentBuilder db = dbf.newDocumentBuilder();
  119. foDoc = db.newDocument();
  120. root = foDoc.createElementNS(foNS, "fo:root");
  121. foDoc.appendChild(root);
  122. ele1 = foDoc.createElementNS(foNS, "fo:layout-master-set");
  123. root.appendChild(ele1);
  124. ele2 = foDoc.createElementNS(foNS, "fo:simple-page-master");
  125. ele1.appendChild(ele2);
  126. ele2.setAttributeNS(null, "master-name", "letter");
  127. ele2.setAttributeNS(null, "page-height", "11in");
  128. ele2.setAttributeNS(null, "page-width", "8.5in");
  129. ele2.setAttributeNS(null, "margin-top", "1in");
  130. ele2.setAttributeNS(null, "margin-bottom", "1in");
  131. ele2.setAttributeNS(null, "margin-left", "1in");
  132. ele2.setAttributeNS(null, "margin-right", "1in");
  133. ele3 = foDoc.createElementNS(foNS, "fo:region-body");
  134. ele2.appendChild(ele3);
  135. ele1 = foDoc.createElementNS(foNS, "fo:page-sequence");
  136. root.appendChild(ele1);
  137. ele1.setAttributeNS(null, "master-reference", "letter");
  138. ele2 = foDoc.createElementNS(foNS, "fo:flow");
  139. ele1.appendChild(ele2);
  140. ele2.setAttributeNS(null, "flow-name", "xsl-region-body");
  141. addElement(ele2, "fo:block", "Hello World!");
  142. return foDoc;
  143. }
  144. /**
  145. * Adds an element to the DOM.
  146. * @param parent parent node to attach the new element to
  147. * @param newNodeName name of the new node
  148. * @param textVal content of the element
  149. */
  150. protected static void addElement(Node parent, String newNodeName,
  151. String textVal) {
  152. if (textVal == null) {
  153. return;
  154. } // use only with text nodes
  155. Element newElement = parent.getOwnerDocument().createElementNS(
  156. foNS, newNodeName);
  157. Text elementText = parent.getOwnerDocument().createTextNode(textVal);
  158. newElement.appendChild(elementText);
  159. parent.appendChild(newElement);
  160. }
  161. }