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.

FopPrintServlet.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.servlet;
  19. import java.io.IOException;
  20. import java.io.PrintWriter;
  21. import javax.servlet.http.HttpServletResponse;
  22. import javax.xml.transform.Transformer;
  23. import javax.xml.transform.TransformerException;
  24. import javax.xml.transform.Result;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.sax.SAXResult;
  27. import org.apache.fop.apps.FOPException;
  28. import org.apache.fop.apps.FOUserAgent;
  29. import org.apache.fop.apps.Fop;
  30. import org.apache.fop.apps.MimeConstants;
  31. /**
  32. * Example servlet to generate a fop printout from a servlet.
  33. * Printing goes to the default printer on host where the servlet executes.
  34. * Servlet param is:
  35. * <ul>
  36. * <li>fo: the path to a XSL-FO file to render
  37. * </ul>
  38. * or
  39. * <ul>
  40. * <li>xml: the path to an XML file to render</li>
  41. * <li>xslt: the path to an XSLT file that can transform the above XML to XSL-FO</li>
  42. * </ul>
  43. * <br/>
  44. * Example URL: http://servername/fop/servlet/FopPrintServlet?fo=readme.fo
  45. * <br/>
  46. * Example URL: http://servername/fop/servlet/FopPrintServlet?xml=data.xml&xsl=format.xsl
  47. * <br/>
  48. * <b>Note:</b> This servlet is derived from FopServlet. Most methods are inherited from the
  49. * superclass. Only the differences to the base class are necessary.
  50. *
  51. * @author <a href="mailto:fop-dev@xmlgraphics.apache.org">Apache FOP Development Team</a>
  52. * @version $Id$
  53. */
  54. public class FopPrintServlet extends FopServlet {
  55. private static final long serialVersionUID = 1645706757391617935L;
  56. /**
  57. * {@inheritDoc}
  58. */
  59. protected void render(Source src, Transformer transformer, HttpServletResponse response)
  60. throws FOPException, TransformerException, IOException {
  61. FOUserAgent foUserAgent = getFOUserAgent();
  62. //Setup FOP
  63. Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_PRINT, foUserAgent);
  64. //Make sure the XSL transformation's result is piped through to FOP
  65. Result res = new SAXResult(fop.getDefaultHandler());
  66. //Start the transformation and rendering process
  67. transformer.transform(src, res);
  68. //Return the result
  69. reportOK(response);
  70. }
  71. // private helper, tell (browser) user that file printed
  72. private void reportOK(HttpServletResponse response) throws IOException {
  73. String sMsg = "<html><title>Success</title>\n"
  74. + "<body><h1>FopPrintServlet: </h1>"
  75. + "<h3>The requested data was printed to the default printer.</h3></body></html>";
  76. response.setContentType("text/html");
  77. response.setContentLength(sMsg.length());
  78. PrintWriter out = response.getWriter();
  79. out.println(sMsg);
  80. out.flush();
  81. }
  82. }