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.

FopServlet.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. import java.io.*;
  8. import javax.servlet.*;
  9. import javax.servlet.http.*;
  10. import org.xml.sax.InputSource;
  11. import org.xml.sax.XMLReader;
  12. import org.apache.fop.apps.Driver;
  13. import org.apache.fop.apps.Version;
  14. import org.apache.fop.apps.XSLTInputHandler;
  15. import org.apache.avalon.framework.logger.ConsoleLogger;
  16. import org.apache.avalon.framework.logger.Logger;
  17. /**
  18. * Example servlet to generate a PDF from a servlet.
  19. * Servlet param is:
  20. * <ul>
  21. * <li>fo: the path to a formatting object file to render
  22. * </ul>
  23. *
  24. * Example URL: http://servername/fop/servlet/FopServlet?fo=readme.fo
  25. * Example URL: http://servername/fop/servlet/FopServlet?xml=data.xml&xsl=format.xsl
  26. * Compiling: you will need
  27. * - servlet_2_2.jar
  28. * - fop.jar
  29. * - sax api
  30. * - avalon-framework-x.jar (where x is the version found the FOP lib dir)
  31. *
  32. * Running: you will need in the WEB-INF/lib/ directory:
  33. * - fop.jar
  34. * - batik.jar
  35. * - xalan-2.0.0.jar
  36. * - avalon-framework-x.jar (where x is the version found the FOP lib dir)
  37. */
  38. public class FopServlet extends HttpServlet {
  39. public static final String FO_REQUEST_PARAM = "fo";
  40. public static final String XML_REQUEST_PARAM = "xml";
  41. public static final String XSL_REQUEST_PARAM = "xsl";
  42. Logger log = null;
  43. public void doGet(HttpServletRequest request,
  44. HttpServletResponse response) throws ServletException {
  45. if (log == null) {
  46. log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
  47. }
  48. try {
  49. String foParam = request.getParameter(FO_REQUEST_PARAM);
  50. String xmlParam = request.getParameter(XML_REQUEST_PARAM);
  51. String xslParam = request.getParameter(XSL_REQUEST_PARAM);
  52. if (foParam != null) {
  53. File fofile = new File(foParam);
  54. //log.warn("FO: "+fofile.getCanonicalPath());
  55. FileInputStream file = new FileInputStream(fofile);
  56. renderFO(new InputSource(file), response);
  57. } else if ((xmlParam != null) && (xslParam != null)) {
  58. XSLTInputHandler input =
  59. new XSLTInputHandler(new File(xmlParam),
  60. new File(xslParam));
  61. renderXML(input, response);
  62. } else {
  63. PrintWriter out = response.getWriter();
  64. out.println("<html><head><title>Error</title></head>\n"+
  65. "<body><h1>FopServlet Error</h1><h3>No 'fo' "+
  66. "request param given.</body></html>");
  67. }
  68. } catch (ServletException ex) {
  69. throw ex;
  70. }
  71. catch (Exception ex) {
  72. throw new ServletException(ex);
  73. }
  74. }
  75. /**
  76. * Renders an FO inputsource into a PDF file which is written
  77. * directly to the response object's OutputStream
  78. */
  79. public void renderFO(InputSource foFile,
  80. HttpServletResponse response) throws ServletException {
  81. try {
  82. ByteArrayOutputStream out = new ByteArrayOutputStream();
  83. response.setContentType("application/pdf");
  84. Driver driver = new Driver(foFile, out);
  85. driver.enableLogging(log);
  86. driver.setRenderer(Driver.RENDER_PDF);
  87. driver.run();
  88. byte[] content = out.toByteArray();
  89. response.setContentLength(content.length);
  90. response.getOutputStream().write(content);
  91. response.getOutputStream().flush();
  92. } catch (Exception ex) {
  93. throw new ServletException(ex);
  94. }
  95. }
  96. /**
  97. * Renders an XML file into a PDF file by applying a stylesheet
  98. * that converts the XML to XSL:FO. The PDF is written
  99. * directly to the response object's OutputStream
  100. */
  101. public void renderXML(XSLTInputHandler input,
  102. HttpServletResponse response) throws ServletException {
  103. try {
  104. ByteArrayOutputStream out = new ByteArrayOutputStream();
  105. response.setContentType("application/pdf");
  106. Driver driver = new Driver();
  107. driver.enableLogging(log);
  108. driver.setRenderer(Driver.RENDER_PDF);
  109. driver.setOutputStream(out);
  110. driver.render(input.getParser(), input.getInputSource());
  111. byte[] content = out.toByteArray();
  112. response.setContentLength(content.length);
  113. response.getOutputStream().write(content);
  114. response.getOutputStream().flush();
  115. } catch (Exception ex) {
  116. throw new ServletException(ex);
  117. }
  118. }
  119. }