/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* $Id$ */ package servlets; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringReader; import java.net.URL; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.URIResolver; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamSource; import org.apache.commons.logging.impl.SimpleLog; import org.apache.commons.logging.Log; //FOP import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.FOPException; import org.apache.fop.apps.Fop; import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.MimeConstants; import org.apache.fop.servlet.ServletContextURIResolver; /** * @author Apache XML FOP Development Team * @version $Id: FopServlet.java,v 1.2 2003/03/07 09:48:05 jeremias Exp $ * (todo) Ev. add caching mechanism for Templates objects */ public class GeneratePDF extends HttpServlet { /** The TransformerFactory to use to create Transformer instances */ protected TransformerFactory transFactory = null; /** The FopFactory used to create Fop instances */ protected FopFactory fopFactory = null; /** * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse) */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException { HttpSession session = request.getSession(); try { // Get parameters String xml = session.getAttribute("xml").toString(); String xslUrl = session.getAttribute("xslUrl").toString(); // Analyze parameters and decide with method to use byte[] content = null; if ((xml != null) && (xslUrl != null)) { content = renderXML(xml, xslUrl); } else { PrintWriter out = response.getWriter(); out.println("Error\n" + "

FopServlet Error

No 'fo' " + "request param given."); } if (content != null) { //Send the result back to the client response.setContentType("application/pdf"); response.setContentLength(content.length); response.getOutputStream().write(content); response.getOutputStream().flush(); response.getOutputStream().close(); } } catch (Exception ex) { throw new ServletException(ex); } } /** * Renders an XML file into a PDF file by applying a stylesheet * that converts the XML to XSL-FO. The PDF is written to a byte array * that is returned as the method's result. * @param xml the XML file * @param xslt the XSLT file * @throws FOPException If an error occurs during the rendering of the * XSL-FO * @throws TransformerException If an error occurs during XSL * transformation * @throws IOException In case of an I/O problem */ protected byte[] renderXML(String xml, String xslt) throws FOPException, TransformerException, IOException { //Setup the XSL transformation StreamSource xslSrc = new StreamSource(xslt); System.out.println(xslSrc.toString()); StreamSource xslSrc2 = new StreamSource(new StringReader(xslt)); System.out.println(xslSrc2.toString()); byte buf[] = null; URL xslUrl = new URL(xslt); InputStream in = new BufferedInputStream(xslUrl.openStream()); ByteArrayOutputStream xslOut = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; for (int read=0; (read=in.read(buffer)) != -1; xslOut.write(buffer, 0, read)); System.out.println(xslOut.toString()); xslOut.close(); in.close(); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); FOUserAgent userAgent = fopFactory.newFOUserAgent(); //Setup output ByteArrayOutputStream out = new ByteArrayOutputStream(); //Setup FOP Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out); fop.setOutputStream(out); Source src = new StreamSource(new StringReader(xml)); //Make sure the XSL transformation's result is piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); //Start the transformation and rendering process transformer.transform(src, res); return out.toByteArray(); } }