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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. //JAXP
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import javax.xml.parsers.DocumentBuilder;
  25. // DOM
  26. import org.w3c.dom.Document;
  27. import org.w3c.dom.Element;
  28. import org.w3c.dom.Node;
  29. import org.w3c.dom.Text;
  30. // Avalon
  31. import org.apache.avalon.framework.ExceptionUtil;
  32. import org.apache.avalon.framework.logger.ConsoleLogger;
  33. import org.apache.avalon.framework.logger.Logger;
  34. //FOP
  35. import org.apache.fop.apps.Driver;
  36. import org.apache.fop.apps.FOPException;
  37. /**
  38. * This class demonstrates the conversion of a DOM Document to PDF
  39. * using JAXP (XSLT) and FOP (XSL-FO).
  40. */
  41. public class ExampleDOM2PDF {
  42. /** xsl-fo namespace URI */
  43. protected static String foNS = "http://www.w3.org/1999/XSL/Format";
  44. /**
  45. * Converts a DOM Document to a PDF file using FOP.
  46. * @param xslfoDoc the DOM Document
  47. * @param pdf the target PDF file
  48. * @throws IOException In case of an I/O problem
  49. * @throws FOPException In case of a FOP problem
  50. */
  51. public void convertDOM2PDF(Document xslfoDoc, File pdf)
  52. throws IOException, FOPException {
  53. //Construct driver
  54. Driver driver = new Driver();
  55. //Setup logger
  56. Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
  57. driver.enableLogging(logger);
  58. driver.initialize();
  59. //Setup Renderer (output format)
  60. driver.setRenderer(Driver.RENDER_PDF);
  61. //Setup output
  62. OutputStream out = new java.io.FileOutputStream(pdf);
  63. out = new java.io.BufferedOutputStream(out);
  64. try {
  65. driver.setOutputStream(out);
  66. driver.render(xslfoDoc);
  67. } finally {
  68. out.close();
  69. }
  70. }
  71. /**
  72. * Main method.
  73. * @param args command-line arguments
  74. */
  75. public static void main(String[] args) {
  76. try {
  77. System.out.println("FOP ExampleDOM2PDF\n");
  78. //Setup directories
  79. File baseDir = new File(".");
  80. File outDir = new File(baseDir, "out");
  81. outDir.mkdirs();
  82. //Setup output file
  83. File pdffile = new File(outDir, "ResultDOM2PDF.pdf");
  84. System.out.println("PDF Output File: " + pdffile);
  85. System.out.println();
  86. // Create a sample XSL-FO DOM document
  87. Document foDoc = null;
  88. Element root = null, ele1 = null, ele2 = null, ele3 = null;
  89. Text elementText = null;
  90. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  91. dbf.setNamespaceAware(true);
  92. DocumentBuilder db = dbf.newDocumentBuilder();
  93. foDoc = db.newDocument();
  94. root = foDoc.createElementNS(foNS, "fo:root");
  95. foDoc.appendChild(root);
  96. ele1 = foDoc.createElementNS(foNS, "fo:layout-master-set");
  97. root.appendChild(ele1);
  98. ele2 = foDoc.createElementNS(foNS, "fo:simple-page-master");
  99. ele1.appendChild(ele2);
  100. ele2.setAttributeNS(null, "master-name", "letter");
  101. ele2.setAttributeNS(null, "page-height", "11in");
  102. ele2.setAttributeNS(null, "page-width", "8.5in");
  103. ele2.setAttributeNS(null, "margin-top", "1in");
  104. ele2.setAttributeNS(null, "margin-bottom", "1in");
  105. ele2.setAttributeNS(null, "margin-left", "1in");
  106. ele2.setAttributeNS(null, "margin-right", "1in");
  107. ele3 = foDoc.createElementNS(foNS, "fo:region-body");
  108. ele2.appendChild(ele3);
  109. ele1 = foDoc.createElementNS(foNS, "fo:page-sequence");
  110. root.appendChild(ele1);
  111. ele1.setAttributeNS(null, "master-reference", "letter");
  112. ele2 = foDoc.createElementNS(foNS, "fo:flow");
  113. ele1.appendChild(ele2);
  114. ele2.setAttributeNS(null, "flow-name", "xsl-region-body");
  115. addElement(ele2, "fo:block", "Hello World!");
  116. ExampleDOM2PDF app = new ExampleDOM2PDF();
  117. app.convertDOM2PDF(foDoc, pdffile);
  118. System.out.println("Success!");
  119. } catch (Exception e) {
  120. System.err.println(ExceptionUtil.printStackTrace(e));
  121. System.exit(-1);
  122. }
  123. }
  124. /**
  125. * Adds an element to the DOM.
  126. * @param parent parent node to attach the new element to
  127. * @param newNodeName name of the new node
  128. * @param textVal content of the element
  129. */
  130. protected static void addElement(Node parent, String newNodeName,
  131. String textVal) {
  132. if (textVal == null) {
  133. return;
  134. } // use only with text nodes
  135. Element newElement = parent.getOwnerDocument().createElementNS(
  136. foNS, newNodeName);
  137. Text elementText = parent.getOwnerDocument().createTextNode(textVal);
  138. newElement.appendChild(elementText);
  139. parent.appendChild(newElement);
  140. }
  141. }