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.

PDFOutputHandler.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.apps;
  8. import org.xml.sax.*;
  9. import com.jclark.xsl.sax.*;
  10. import java.io.*;
  11. // FOP
  12. import org.apache.fop.fo.XTFOTreeBuilder;
  13. import org.apache.fop.fo.XTElementMapping;
  14. import org.apache.fop.layout.AreaTree;
  15. import org.apache.fop.layout.FontInfo;
  16. import org.apache.fop.render.Renderer;
  17. import org.apache.fop.messaging.MessageHandler;
  18. // ////////////////////////////////////////////////////////////////////////////////////
  19. /**
  20. * A DocumentHandler that writes a PDF representation to an OutputStream.
  21. *
  22. * Use with James Clark's XT. Just put FOP on your class path and add
  23. * <xsl:output method="fop:org.apache.fop.apps.PDFOutputHandler"
  24. * xmlns:fop="http://www.jclark.com/xt/java"/>
  25. * to your stylesheet. Now XT will automatically call FOP.
  26. *
  27. */
  28. public class PDFOutputHandler extends XTFOTreeBuilder
  29. implements OutputDocumentHandler {
  30. /**
  31. * the area tree that is the result of formatting the FO tree
  32. */
  33. protected AreaTree areaTree;
  34. /**
  35. * the renderer to use to output the area tree
  36. */
  37. protected Renderer renderer;
  38. /**
  39. * the PrintWriter to use to output the results of the renderer
  40. */
  41. protected PrintWriter writer;
  42. /**
  43. * the stream to use to output the results of the renderer
  44. */
  45. protected OutputStream stream;
  46. private boolean keepOpen;
  47. // ////////////////////////////////////////////////////////////////////////////////////
  48. /**
  49. */
  50. public PDFOutputHandler() {}
  51. // ////////////////////////////////////////////////////////////////////////////////////
  52. /**
  53. */
  54. public PDFOutputHandler(OutputStream out) {
  55. this();
  56. this.stream = out;
  57. }
  58. // ////////////////////////////////////////////////////////////////////////////////////
  59. /**
  60. */
  61. public DocumentHandler init(Destination dest,
  62. AttributeList atts) throws IOException {
  63. this.stream = dest.getOutputStream("application/pdf", null);
  64. this.keepOpen = dest.keepOpen();
  65. String version = org.apache.fop.apps.Version.getVersion();
  66. setRenderer("org.apache.fop.render.pdf.PDFRenderer", version);
  67. addElementMapping("org.apache.fop.fo.StandardElementMapping");
  68. addElementMapping("org.apache.fop.svg.SVGElementMapping");
  69. return this;
  70. }
  71. // ////////////////////////////////////////////////////////////////////////////////////
  72. /**
  73. * set the class name of the Renderer to use as well as the
  74. * producer string for those renderers that can make use of it
  75. */
  76. public void setRenderer(String rendererClassName, String producer) {
  77. this.renderer = createRenderer(rendererClassName);
  78. this.renderer.setProducer(producer);
  79. }
  80. // ////////////////////////////////////////////////////////////////////////////////////
  81. /**
  82. * SAX passthrough, finish rendering the document
  83. */
  84. public void endDocument() throws SAXException {
  85. super.endDocument();
  86. try {
  87. doFormat();
  88. doRender();
  89. } catch (IOException io) {
  90. throw new SAXException(io);
  91. } catch (FOPException fop) {
  92. throw new SAXException(fop);
  93. }
  94. writer.flush();
  95. }
  96. // ////////////////////////////////////////////////////////////////////////////////////
  97. /**
  98. * format the formatting object tree into an area tree
  99. */
  100. public void doFormat() throws FOPException {
  101. FontInfo fontInfo = new FontInfo();
  102. this.renderer.setupFontInfo(fontInfo);
  103. // this.areaTree = new AreaTree();
  104. this.areaTree.setFontInfo(fontInfo);
  105. format(areaTree);
  106. }
  107. // ////////////////////////////////////////////////////////////////////////////////////
  108. /**
  109. * render the area tree to the output form
  110. */
  111. public void doRender() throws IOException, FOPException {
  112. //this.renderer.render(areaTree, this.stream);
  113. }
  114. // ////////////////////////////////////////////////////////////////////////////////////
  115. /**
  116. * add the given element mapping.
  117. *
  118. * an element mapping maps element names to Java classes
  119. */
  120. public void addElementMapping(XTElementMapping mapping) {
  121. mapping.addToBuilder(this);
  122. }
  123. // ////////////////////////////////////////////////////////////////////////////////////
  124. /**
  125. * add the element mapping with the given class name
  126. */
  127. public void addElementMapping(String mappingClassName) {
  128. createElementMapping(mappingClassName).addToBuilder(this);
  129. }
  130. // ////////////////////////////////////////////////////////////////////////////////////
  131. /**
  132. * protected method used by addElementMapping(String) to
  133. * instantiate element mapping class
  134. */
  135. protected XTElementMapping createElementMapping(String mappingClassName) {
  136. MessageHandler.logln("using element mapping " + mappingClassName);
  137. try {
  138. return (XTElementMapping)Class.forName(mappingClassName).newInstance();
  139. } catch (ClassNotFoundException e) {
  140. MessageHandler.errorln("Could not find " + mappingClassName);
  141. } catch (InstantiationException e) {
  142. MessageHandler.errorln("Could not instantiate "
  143. + mappingClassName);
  144. } catch (IllegalAccessException e) {
  145. MessageHandler.errorln("Could not access " + mappingClassName);
  146. } catch (ClassCastException e) {
  147. MessageHandler.errorln(mappingClassName
  148. + " is not an element mapping");
  149. }
  150. return null;
  151. }
  152. // ////////////////////////////////////////////////////////////////////////////////////
  153. /**
  154. * protected method used by setRenderer(String, String) to
  155. * instantiate the Renderer class
  156. */
  157. protected Renderer createRenderer(String rendererClassName) {
  158. MessageHandler.logln("using renderer " + rendererClassName);
  159. try {
  160. return (Renderer)Class.forName(rendererClassName).newInstance();
  161. } catch (ClassNotFoundException e) {
  162. MessageHandler.errorln("Could not find " + rendererClassName);
  163. } catch (InstantiationException e) {
  164. MessageHandler.errorln("Could not instantiate "
  165. + rendererClassName);
  166. } catch (IllegalAccessException e) {
  167. MessageHandler.errorln("Could not access " + rendererClassName);
  168. } catch (ClassCastException e) {
  169. MessageHandler.errorln(rendererClassName + " is not a renderer");
  170. }
  171. return null;
  172. }
  173. }