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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package embedding;
  18. // Java
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import javax.xml.parsers.DocumentBuilder;
  24. //JAXP
  25. import javax.xml.transform.Transformer;
  26. import javax.xml.transform.TransformerFactory;
  27. import javax.xml.transform.Source;
  28. import javax.xml.transform.Result;
  29. import javax.xml.transform.dom.DOMSource;
  30. import javax.xml.transform.sax.SAXResult;
  31. // DOM
  32. import org.w3c.dom.Document;
  33. import org.w3c.dom.Element;
  34. import org.w3c.dom.Node;
  35. import org.w3c.dom.Text;
  36. // FOP
  37. import org.apache.fop.apps.Fop;
  38. import org.apache.fop.apps.MimeConstants;
  39. /**
  40. * This class demonstrates the conversion of a DOM Document to PDF
  41. * using JAXP (XSLT) and FOP (XSL-FO).
  42. */
  43. public class ExampleDOM2PDF {
  44. /** xsl-fo namespace URI */
  45. protected static String foNS = "http://www.w3.org/1999/XSL/Format";
  46. /**
  47. * Converts a DOM Document to a PDF file using FOP.
  48. * @param xslfoDoc the DOM Document
  49. * @param pdf the target PDF file
  50. * @throws IOException In case of an I/O problem
  51. * @throws FOPException In case of a FOP problem
  52. */
  53. public void convertDOM2PDF(Document xslfoDoc, File pdf) {
  54. try {
  55. // Construct fop with desired output format
  56. Fop fop = new Fop(MimeConstants.MIME_PDF);
  57. // Setup output
  58. OutputStream out = new java.io.FileOutputStream(pdf);
  59. out = new java.io.BufferedOutputStream(out);
  60. try {
  61. fop.setOutputStream(out);
  62. // Setup Identity Transformer
  63. TransformerFactory factory = TransformerFactory.newInstance();
  64. Transformer transformer = factory.newTransformer(); // identity transformer
  65. // Setup input for XSLT transformation
  66. Source src = new DOMSource(xslfoDoc);
  67. // Resulting SAX events (the generated FO) must be piped through to FOP
  68. Result res = new SAXResult(fop.getDefaultHandler());
  69. // Start XSLT transformation and FOP processing
  70. transformer.transform(src, res);
  71. } finally {
  72. out.close();
  73. }
  74. } catch (Exception e) {
  75. e.printStackTrace(System.err);
  76. System.exit(-1);
  77. }
  78. }
  79. /**
  80. * Main method.
  81. * @param args command-line arguments
  82. */
  83. public static void main(String[] args) {
  84. try {
  85. System.out.println("FOP ExampleDOM2PDF\n");
  86. //Setup directories
  87. File baseDir = new File(".");
  88. File outDir = new File(baseDir, "out");
  89. outDir.mkdirs();
  90. //Setup output file
  91. File pdffile = new File(outDir, "ResultDOM2PDF.pdf");
  92. System.out.println("PDF Output File: " + pdffile);
  93. System.out.println();
  94. // Create a sample XSL-FO DOM document
  95. Document foDoc = null;
  96. Element root = null, ele1 = null, ele2 = null, ele3 = null;
  97. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  98. dbf.setNamespaceAware(true);
  99. DocumentBuilder db = dbf.newDocumentBuilder();
  100. foDoc = db.newDocument();
  101. root = foDoc.createElementNS(foNS, "fo:root");
  102. foDoc.appendChild(root);
  103. ele1 = foDoc.createElementNS(foNS, "fo:layout-master-set");
  104. root.appendChild(ele1);
  105. ele2 = foDoc.createElementNS(foNS, "fo:simple-page-master");
  106. ele1.appendChild(ele2);
  107. ele2.setAttributeNS(null, "master-name", "letter");
  108. ele2.setAttributeNS(null, "page-height", "11in");
  109. ele2.setAttributeNS(null, "page-width", "8.5in");
  110. ele2.setAttributeNS(null, "margin-top", "1in");
  111. ele2.setAttributeNS(null, "margin-bottom", "1in");
  112. ele2.setAttributeNS(null, "margin-left", "1in");
  113. ele2.setAttributeNS(null, "margin-right", "1in");
  114. ele3 = foDoc.createElementNS(foNS, "fo:region-body");
  115. ele2.appendChild(ele3);
  116. ele1 = foDoc.createElementNS(foNS, "fo:page-sequence");
  117. root.appendChild(ele1);
  118. ele1.setAttributeNS(null, "master-reference", "letter");
  119. ele2 = foDoc.createElementNS(foNS, "fo:flow");
  120. ele1.appendChild(ele2);
  121. ele2.setAttributeNS(null, "flow-name", "xsl-region-body");
  122. addElement(ele2, "fo:block", "Hello World!");
  123. ExampleDOM2PDF app = new ExampleDOM2PDF();
  124. app.convertDOM2PDF(foDoc, pdffile);
  125. System.out.println("Success!");
  126. } catch (Exception e) {
  127. e.printStackTrace(System.err);
  128. System.exit(-1);
  129. }
  130. }
  131. /**
  132. * Adds an element to the DOM.
  133. * @param parent parent node to attach the new element to
  134. * @param newNodeName name of the new node
  135. * @param textVal content of the element
  136. */
  137. protected static void addElement(Node parent, String newNodeName,
  138. String textVal) {
  139. if (textVal == null) {
  140. return;
  141. } // use only with text nodes
  142. Element newElement = parent.getOwnerDocument().createElementNS(
  143. foNS, newNodeName);
  144. Text elementText = parent.getOwnerDocument().createTextNode(textVal);
  145. newElement.appendChild(elementText);
  146. parent.appendChild(newElement);
  147. }
  148. }