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.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.Result;
  23. import javax.xml.transform.Source;
  24. import javax.xml.transform.Transformer;
  25. import javax.xml.transform.TransformerException;
  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
  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. public class FopPrintServlet extends FopServlet {
  52. private static final long serialVersionUID = 1645706757391617935L;
  53. /**
  54. * {@inheritDoc}
  55. */
  56. protected void render(Source src, Transformer transformer, HttpServletResponse response)
  57. throws FOPException, TransformerException, IOException {
  58. FOUserAgent foUserAgent = getFOUserAgent();
  59. //Setup FOP
  60. Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_PRINT, foUserAgent);
  61. //Make sure the XSL transformation's result is piped through to FOP
  62. Result res = new SAXResult(fop.getDefaultHandler());
  63. //Start the transformation and rendering process
  64. transformer.transform(src, res);
  65. //Return the result
  66. reportOK(response);
  67. }
  68. // private helper, tell (browser) user that file printed
  69. private void reportOK(HttpServletResponse response) throws IOException {
  70. String sMsg = "<html><title>Success</title>\n"
  71. + "<body><h1>FopPrintServlet: </h1>"
  72. + "<h3>The requested data was printed to the default printer.</h3></body></html>";
  73. response.setContentType("text/html");
  74. response.setContentLength(sMsg.length());
  75. PrintWriter out = response.getWriter();
  76. out.println(sMsg);
  77. out.flush();
  78. }
  79. }