Here's a simple servlet that will work. Like the FOP servlet, you give it a
fo file or an xml/xsl combination in a get parameter. You have to have the
latest xalan and xerces in addition to fop, because I handle the xml
transformation within the servlet as opposed to using the fop methods for
transformation.
If you're using tomcat, your url to hit the servlet will be something like,
http://localhost:8080/app/simple?fo=../webapps/app/xslfo/test.fo
Make sure you're element has a master-reference attribute
and not a master-name attribute. This was a recent change, I guess. Here's
the code for the simple servlet as well as a test fo document.
Mike Z
SERVLET
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.InputSource;
import org.apache.fop.apps.Driver;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;
public class SimpleServlet extends HttpServlet {
private static final String FO_REQUEST_PARAM = "fo";
private static final String XML_REQUEST_PARAM = "xml";
private static final String XSL_REQUEST_PARAM = "xsl";
// you might want to override init() and set up a log4j category for the
servlet so you can easily add debugging
// code
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
try {
// you might want to set up a constant to represent the
directory path or pull the directory path
// from a configuration file, otherwise you have to
supply the path in the get parameter
String foParam = request.getParameter(FO_REQUEST_PARAM);
String xmlParam = request.getParameter(XML_REQUEST_PARAM);
String xslParam = request.getParameter(XSL_REQUEST_PARAM);
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputSource input = null;
// set content type as pdf so browser will know it's getting pdf
content and display it properly
response.setContentType("application/pdf");
if ((request.getParameter(FO_REQUEST_PARAM)) != null) {
FileInputStream fo = new FileInputStream(foParam);
input = new InputSource(fo);
} else if(((request.getParameter(XML_REQUEST_PARAM)) != null) &&
((request.getParameter(XSL_REQUEST_PARAM)) != null)) {
File xml = new File(xmlParam);
File xsl = new File(xslParam);
Source xmlSource = new StreamSource(xml);
Source xslSource = new StreamSource(xsl);
StringWriter sw = new StringWriter();
TransformerFactory tFactory =
TransformerFactory.newInstance();
//create Templates so the xsl document will
function as a template and will be cached in
//order to optimize performance
Templates cachedXSLT =
tFactory.newTemplates(xslSource);
Transformer transformer =
cachedXSLT.newTransformer();
//transform the xml we've been given using
the cached xsl
transformer.transform(xmlSource, new
StreamResult(sw));
Reader r = new StringReader(sw.toString());
input = new InputSource(r);
} else {
// handle situation: get parameters are not
correct
}
if (input != null) {
Driver driver = new Driver(input, out);
driver.setRenderer(Driver.RENDER_PDF);
driver.run();
}
//send content to the browser now
byte[] content = out.toByteArray();
response.setContentLength(content.length);
response.getOutputStream().write(content);
response.getOutputStream().flush();
} catch (Exception e) {
// handle exceptions.
}
}
// pass post data received to get for processing
public void doPost(HttpServletRequest request,
HttpServletResponse
response)
throws IOException, ServletException
{
doGet(request, response);
}
}
FO
__
http://www.w3.org/1999/XSL/Format";>
John
Doe
5130
B27
2819
XPT
-Original Message-
From: Pedro Barco Bernal [mailto:[EMAIL PROTECTED]
Sent: Monday, February 04, 2002 11:20 PM
To: [EMAIL PROTECTED]
Subject: FOP in server
¿Someone have used