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

If you're using tomcat, your url to hit the servlet will be something like, 

Make sure you're <fo:page-sequence> 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

import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.InputSource;
import org.apache.fop.apps.Driver;
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

            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 =
                                //create Templates so the xsl document will
function as a template and will be cached in
                                //order to optimize performance
                                Templates cachedXSLT =
                                Transformer transformer =
                                //transform the xml we've been given using
the cached xsl
                                transformer.transform(xmlSource, new
                                Reader r = new StringReader(sw.toString());
                                input = new InputSource(r);
            } else {
                                // handle situation:  get parameters are not
                        if (input != null) {
                                Driver driver = new Driver(input, out);

            //send content to the browser now
            byte[] content = out.toByteArray();
        } catch (Exception e) {
            // handle exceptions.

        // pass post data received to get for processing
    public void doPost(HttpServletRequest request,
                        throws IOException, ServletException
                        doGet(request, response);


<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="";>
<fo:simple-page-master margin-right="2.5cm" margin-left="2.5cm"
margin-bottom="2cm" margin-top="1cm" page-width="21cm" page-height="29.7cm"
<fo:region-body margin-top="3cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>
<fo:page-sequence master-reference="simple">
<fo:flow flow-name="xsl-region-body">
<fo:block line-height="10pt" font-family="arial" font-size="8pt">

-----Original Message-----
From: Pedro Barco Bernal [mailto:[EMAIL PROTECTED]
Sent: Monday, February 04, 2002 11:20 PM
Subject: FOP in server

¿Someone have used FOP in server?

We are calling FOP from Java Server Pages and the server send a ERROR
message like this:

exception sax : org.xml.sax.SAXParseException: The root element is required
in a well-formed document.
error The root element is required in a well-formed document.
ERROR: The root element is required in a well-formed document.

But when we call FOP from local there aren`t any problem!!! The document is
well-formed sure!!

Please help!!!

----- Original Message -----
From: "Carlos" <[EMAIL PROTECTED]>
Sent: Tuesday, February 05, 2002 1:20 AM
Subject: Problems with 0.20.3rc

> I'm trying to convert a document to PDF, having successfully done it
> and now get the following error:
> [localhost:~/docbook] carlosar% /usr/local/fop/  webct-boot-camp.xml
> [INFO]: FOP 0.20.3rc
> [INFO]: building formatting object tree
> [ERROR]: Unknown formatting object ^article
> [ERROR]: Root element must be root, not (none):article
> [localhost:~/docbook] carlosar%
> Any help is appreciated
> Carlos
> --
> Carlos E. Araya
> ---+ WebCT Administrator/Trainer
>  P | California Virtual Campus
>  - | C/O De Anza College
>  G | 21250 Stevens Creek Blvd
> ---+ Cupertino, CA 95014
> email               [EMAIL PROTECTED]
> web        (work)
>            (personal)
> phone               408 257 0420 (work)
> PGP Fingerprint:    E629 5DFD 7EAE 4995 E9D7  3D2F 5A9F 0CE7 DFE7 1756
> We cannot put off living until we are ready. The most salient
> characteristic of life is its  coerciveness: it is always urgent,  'here
> now' without any possible postponement. Life is  fired at us point blank.
> Jose Ortega Y Gasset

Reply via email to