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

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
______________________________________________________

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format";>
<fo:layout-master-set>
<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"
master-name="simple">
<fo:region-body margin-top="3cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simple">
<fo:flow flow-name="xsl-region-body">
<fo:block line-height="10pt" font-family="arial" font-size="8pt">
<fo:block>John</fo:block>
<fo:block>Doe</fo:block>
<fo:block>5130</fo:block>
<fo:block>B27</fo:block>
<fo:block>2819</fo:block>
<fo:block>XPT</fo:block>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>

-----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 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]>
To: <[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
before
> and now get the following error:
>
> [localhost:~/docbook] carlosar% /usr/local/fop/fop.sh  webct-boot-camp.xml
> boot.fo
> [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                 http://www.cvc1.org/ (work)
>                     http://www.silverwolf-net.net (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
and
> now' without any possible postponement. Life is  fired at us point blank.
> Jose Ortega Y Gasset
>

Reply via email to