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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * $Id: FopPrintServlet.java,v 1.2 2003/03/07 09:48:05 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.servlet;
  52. import java.io.File;
  53. import java.io.IOException;
  54. import java.io.InputStream;
  55. import java.io.PrintWriter;
  56. import java.util.List;
  57. import java.awt.print.PrinterJob;
  58. import java.awt.print.PrinterException;
  59. import javax.servlet.ServletException;
  60. import javax.servlet.http.HttpServlet;
  61. import javax.servlet.http.HttpServletRequest;
  62. import javax.servlet.http.HttpServletResponse;
  63. import javax.xml.transform.TransformerFactory;
  64. import org.xml.sax.InputSource;
  65. // Avalon
  66. import org.apache.avalon.framework.logger.ConsoleLogger;
  67. import org.apache.avalon.framework.logger.Logger;
  68. // FOP
  69. import org.apache.fop.apps.Driver;
  70. import org.apache.fop.apps.FOPException;
  71. import org.apache.fop.area.PageViewport;
  72. import org.apache.fop.apps.XSLTInputHandler;
  73. import org.apache.fop.render.awt.AWTRenderer;
  74. /**
  75. * Example servlet to generate a fop printout from a servlet.
  76. * Printing goes to the default printer on host where the servlet executes.
  77. * Servlet param is:
  78. * <ul>
  79. * <li>fo: the path to a XSL-FO file to render
  80. * </ul>
  81. * or
  82. * <ul>
  83. * <li>xml: the path to an XML file to render</li>
  84. * <li>xslt: the path to an XSLT file that can transform the above XML to XSL-FO</li>
  85. * </ul>
  86. * <br/>
  87. * Example URL: http://servername/fop/servlet/FopPrintServlet?fo=readme.fo
  88. * <br/>
  89. * Example URL: http://servername/fop/servlet/FopPrintServlet?xml=data.xml&xsl=format.xsl
  90. *
  91. * @author <a href="mailto:fop-dev@xml.apache.org">Apache XML FOP Development Team</a>
  92. * @version $Id: FopPrintServlet.java,v 1.2 2003/03/07 09:48:05 jeremias Exp $
  93. * (todo) Doesn't work since there's no AWTRenderer at the moment. Revisit when
  94. * available.
  95. * (todo) Don't use XSLTInputHandler anymore
  96. * (todo) Ev. add caching mechanism for Templates objects
  97. */
  98. public class FopPrintServlet extends HttpServlet {
  99. /** Name of the parameter used for the XSL-FO file */
  100. protected static final String FO_REQUEST_PARAM = "fo";
  101. /** Name of the parameter used for the XML file */
  102. protected static final String XML_REQUEST_PARAM = "xml";
  103. /** Name of the parameter used for the XSLT file */
  104. protected static final String XSLT_REQUEST_PARAM = "xslt";
  105. /** Logger to give to FOP */
  106. protected Logger log = null;
  107. /** The TransformerFactory to use to create Transformer instances */
  108. protected TransformerFactory transFactory = null;
  109. /**
  110. * @see javax.servlet.GenericServlet#init()
  111. */
  112. public void init() throws ServletException {
  113. this.log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
  114. this.transFactory = TransformerFactory.newInstance();
  115. }
  116. /**
  117. * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
  118. */
  119. public void doGet(HttpServletRequest request,
  120. HttpServletResponse response) throws ServletException {
  121. if (log == null) {
  122. log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
  123. }
  124. try {
  125. String foParam = request.getParameter(FO_REQUEST_PARAM);
  126. String xmlParam = request.getParameter(XML_REQUEST_PARAM);
  127. String xsltParam = request.getParameter(XSLT_REQUEST_PARAM);
  128. if (foParam != null) {
  129. InputStream file = new java.io.FileInputStream(foParam);
  130. renderFO(new InputSource(file), response);
  131. } else if ((xmlParam != null) && (xsltParam != null)) {
  132. XSLTInputHandler input =
  133. new XSLTInputHandler(new File(xmlParam),
  134. new File(xsltParam));
  135. renderXML(input, response);
  136. } else {
  137. response.setContentType("text/html");
  138. PrintWriter out = response.getWriter();
  139. out.println("<html><title>Error</title>\n"
  140. + "<body><h1>FopServlet Error</h1>\n"
  141. + "<h3>No 'fo' or 'xml/xsl' "
  142. + "request param given.</h3></body>\n</html>");
  143. }
  144. } catch (ServletException ex) {
  145. throw ex;
  146. } catch (Exception ex) {
  147. throw new ServletException(ex);
  148. }
  149. }
  150. /**
  151. * Renders an FO inputsource to the default printer.
  152. * @param foFile The XSL-FO file
  153. * @param response Response to write to
  154. * @throws ServletException In case of a problem
  155. */
  156. public void renderFO(InputSource foFile,
  157. HttpServletResponse response) throws ServletException {
  158. try {
  159. Driver driver = new Driver(foFile, null);
  160. PrinterJob pj = PrinterJob.getPrinterJob();
  161. PrintRenderer renderer = new PrintRenderer(pj);
  162. driver.enableLogging(log);
  163. driver.setRenderer(renderer);
  164. driver.run();
  165. reportOK (response);
  166. } catch (Exception ex) {
  167. throw new ServletException(ex);
  168. }
  169. }
  170. /**
  171. * Renders an FO generated using an XML and a stylesheet to the default printer.
  172. * @param input XSLTInputHandler to use
  173. * @param response Response to write to
  174. * @throws ServletException In case of a problem
  175. */
  176. public void renderXML(XSLTInputHandler input,
  177. HttpServletResponse response) throws ServletException {
  178. try {
  179. Driver driver = new Driver();
  180. PrinterJob pj = PrinterJob.getPrinterJob();
  181. PrintRenderer renderer = new PrintRenderer(pj);
  182. pj.setCopies(1);
  183. driver.enableLogging(log);
  184. driver.setRenderer(renderer);
  185. driver.render(input.getParser(), input.getInputSource());
  186. reportOK (response);
  187. } catch (Exception ex) {
  188. throw new ServletException(ex);
  189. }
  190. }
  191. // private helper, tell (browser) user that file printed
  192. private void reportOK(HttpServletResponse response)
  193. throws ServletException {
  194. String sMsg = "<html><title>Success</title>\n"
  195. + "<body><h1>FopPrintServlet: </h1>"
  196. + "<h3>The requested data was printed</h3></body></html>";
  197. response.setContentType("text/html");
  198. response.setContentLength(sMsg.length());
  199. try {
  200. PrintWriter out = response.getWriter();
  201. out.println(sMsg);
  202. out.flush();
  203. } catch (Exception ex) {
  204. throw new ServletException(ex);
  205. }
  206. }
  207. // This is stolen from PrintStarter
  208. class PrintRenderer extends AWTRenderer {
  209. private static final int EVEN_AND_ALL = 0;
  210. private static final int EVEN = 1;
  211. private static final int ODD = 2;
  212. private int startNumber;
  213. private int endNumber;
  214. private int mode = EVEN_AND_ALL;
  215. private int copies = 1;
  216. private PrinterJob printerJob;
  217. PrintRenderer(PrinterJob printerJob) {
  218. super(null);
  219. this.printerJob = printerJob;
  220. startNumber = 0 ;
  221. endNumber = -1;
  222. printerJob.setPageable(this);
  223. mode = EVEN_AND_ALL;
  224. String str = System.getProperty("even");
  225. if (str != null) {
  226. mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD;
  227. }
  228. }
  229. public void stopRenderer() throws IOException {
  230. super.stopRenderer();
  231. if (endNumber == -1) {
  232. endNumber = getPageCount();
  233. }
  234. List numbers = getInvalidPageNumbers();
  235. for (int i = numbers.size() - 1; i > -1; i--) {
  236. //removePage(
  237. // Integer.parseInt((String) numbers.elementAt(i)));
  238. }
  239. try {
  240. printerJob.print();
  241. } catch (PrinterException e) {
  242. e.printStackTrace();
  243. throw new IOException("Unable to print: "
  244. + e.getClass().getName() + ": " + e.getMessage());
  245. }
  246. }
  247. public void renderPage(PageViewport page) throws IOException, FOPException {
  248. pageWidth = (int)((float) page.getViewArea().getWidth() / 1000f);
  249. pageHeight = (int)((float) page.getViewArea().getHeight() / 1000f);
  250. super.renderPage(page);
  251. }
  252. private List getInvalidPageNumbers() {
  253. List list = new java.util.ArrayList();
  254. int max = getPageCount();
  255. boolean isValid;
  256. for (int i = 0; i < max; i++) {
  257. isValid = true;
  258. if (i < startNumber || i > endNumber) {
  259. isValid = false;
  260. } else if (mode != EVEN_AND_ALL) {
  261. if (mode == EVEN && ((i + 1) % 2 != 0)) {
  262. isValid = false;
  263. } else if (mode == ODD && ((i + 1) % 2 != 1)) {
  264. isValid = false;
  265. }
  266. }
  267. if (!isValid) {
  268. list.add(Integer.toString(i));
  269. }
  270. }
  271. return list;
  272. }
  273. } // class PrintRenderer
  274. }