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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 1999-2005 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 org.apache.fop.servlet;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.File;
  20. import java.io.PrintWriter;
  21. import javax.servlet.ServletException;
  22. import javax.servlet.http.HttpServlet;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import javax.xml.transform.Result;
  26. import javax.xml.transform.Source;
  27. import javax.xml.transform.Transformer;
  28. import javax.xml.transform.TransformerException;
  29. import javax.xml.transform.TransformerFactory;
  30. import javax.xml.transform.sax.SAXResult;
  31. import javax.xml.transform.stream.StreamSource;
  32. import org.apache.commons.logging.impl.SimpleLog;
  33. //FOP
  34. import org.apache.fop.apps.Fop;
  35. import org.apache.fop.apps.FOPException;
  36. import org.apache.fop.apps.MimeConstants;
  37. /**
  38. * Example servlet to generate a PDF from a servlet.
  39. * <br/>
  40. * Servlet param is:
  41. * <ul>
  42. * <li>fo: the path to a XSL-FO file to render
  43. * </ul>
  44. * or
  45. * <ul>
  46. * <li>xml: the path to an XML file to render</li>
  47. * <li>xslt: the path to an XSLT file that can transform the above XML to XSL-FO</li>
  48. * </ul>
  49. * <br/>
  50. * Example URL: http://servername/fop/servlet/FopServlet?fo=readme.fo
  51. * <br/>
  52. * Example URL: http://servername/fop/servlet/FopServlet?xml=data.xml&xslt=format.xsl
  53. * <br/>
  54. * For this to work with Internet Explorer, you might need to append "&ext=.pdf"
  55. * to the URL.
  56. *
  57. * @author <a href="mailto:fop-dev@xml.apache.org">Apache XML FOP Development Team</a>
  58. * @version $Id$
  59. * (todo) Ev. add caching mechanism for Templates objects
  60. */
  61. public class FopServlet extends HttpServlet {
  62. /** Name of the parameter used for the XSL-FO file */
  63. protected static final String FO_REQUEST_PARAM = "fo";
  64. /** Name of the parameter used for the XML file */
  65. protected static final String XML_REQUEST_PARAM = "xml";
  66. /** Name of the parameter used for the XSLT file */
  67. protected static final String XSLT_REQUEST_PARAM = "xslt";
  68. /** Logger to give to FOP */
  69. protected SimpleLog log = null;
  70. /** The TransformerFactory to use to create Transformer instances */
  71. protected TransformerFactory transFactory = null;
  72. /**
  73. * @see javax.servlet.GenericServlet#init()
  74. */
  75. public void init() throws ServletException {
  76. this.log = new SimpleLog("FOP/Servlet");
  77. log.setLevel(SimpleLog.LOG_LEVEL_WARN);
  78. this.transFactory = TransformerFactory.newInstance();
  79. }
  80. /**
  81. * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
  82. */
  83. public void doGet(HttpServletRequest request,
  84. HttpServletResponse response) throws ServletException {
  85. try {
  86. //Get parameters
  87. String foParam = request.getParameter(FO_REQUEST_PARAM);
  88. String xmlParam = request.getParameter(XML_REQUEST_PARAM);
  89. String xsltParam = request.getParameter(XSLT_REQUEST_PARAM);
  90. //Analyze parameters and decide with method to use
  91. byte[] content = null;
  92. if (foParam != null) {
  93. content = renderFO(foParam);
  94. } else if ((xmlParam != null) && (xsltParam != null)) {
  95. content = renderXML(xmlParam, xsltParam);
  96. } else {
  97. PrintWriter out = response.getWriter();
  98. out.println("<html><head><title>Error</title></head>\n"
  99. + "<body><h1>FopServlet Error</h1><h3>No 'fo' "
  100. + "request param given.</body></html>");
  101. }
  102. if (content != null) {
  103. //Send the result back to the client
  104. response.setContentType("application/pdf");
  105. response.setContentLength(content.length);
  106. response.getOutputStream().write(content);
  107. response.getOutputStream().flush();
  108. }
  109. } catch (Exception ex) {
  110. throw new ServletException(ex);
  111. }
  112. }
  113. /**
  114. * Converts a String parameter to a JAXP Source object.
  115. * @param param a String parameter
  116. * @return Source the generated Source object
  117. */
  118. protected Source convertString2Source(String param) {
  119. return new StreamSource(new File(param));
  120. }
  121. /**
  122. * Renders an XSL-FO file into a PDF file. The PDF is written to a byte
  123. * array that is returned as the method's result.
  124. * @param fo the XSL-FO file
  125. * @return byte[] the rendered PDF file
  126. * @throws FOPException If an error occurs during the rendering of the
  127. * XSL-FO
  128. * @throws TransformerException If an error occurs while parsing the input
  129. * file
  130. */
  131. protected byte[] renderFO(String fo)
  132. throws FOPException, TransformerException {
  133. //Setup source
  134. Source foSrc = convertString2Source(fo);
  135. //Setup the identity transformation
  136. Transformer transformer = this.transFactory.newTransformer();
  137. //Start transformation and rendering process
  138. return render(foSrc, transformer);
  139. }
  140. /**
  141. * Renders an XML file into a PDF file by applying a stylesheet
  142. * that converts the XML to XSL-FO. The PDF is written to a byte array
  143. * that is returned as the method's result.
  144. * @param xml the XML file
  145. * @param xslt the XSLT file
  146. * @return byte[] the rendered PDF file
  147. * @throws FOPException If an error occurs during the rendering of the
  148. * XSL-FO
  149. * @throws TransformerException If an error occurs during XSL
  150. * transformation
  151. */
  152. protected byte[] renderXML(String xml, String xslt)
  153. throws FOPException, TransformerException {
  154. //Setup sources
  155. Source xmlSrc = convertString2Source(xml);
  156. Source xsltSrc = convertString2Source(xslt);
  157. //Setup the XSL transformation
  158. Transformer transformer = this.transFactory.newTransformer(xsltSrc);
  159. //Start transformation and rendering process
  160. return render(xmlSrc, transformer);
  161. }
  162. /**
  163. * Renders an input file (XML or XSL-FO) into a PDF file. It uses the JAXP
  164. * transformer given to optionally transform the input document to XSL-FO.
  165. * The transformer may be an identity transformer in which case the input
  166. * must already be XSL-FO. The PDF is written to a byte array that is
  167. * returned as the method's result.
  168. * @param src Input XML or XSL-FO
  169. * @param transformer Transformer to use for optional transformation
  170. * @return byte[] the rendered PDF file
  171. * @throws FOPException If an error occurs during the rendering of the
  172. * XSL-FO
  173. * @throws TransformerException If an error occurs during XSL
  174. * transformation
  175. */
  176. protected byte[] render(Source src, Transformer transformer)
  177. throws FOPException, TransformerException {
  178. //Setup FOP
  179. Fop fop = new Fop(MimeConstants.MIME_PDF);
  180. //Setup output
  181. ByteArrayOutputStream out = new ByteArrayOutputStream();
  182. fop.setOutputStream(out);
  183. //Make sure the XSL transformation's result is piped through to FOP
  184. Result res = new SAXResult(fop.getDefaultHandler());
  185. //Start the transformation and rendering process
  186. transformer.transform(src, res);
  187. //Return the result
  188. return out.toByteArray();
  189. }
  190. }