You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by Julian Bright <ju...@brightsparc.com> on 2000/12/15 05:57:45 UTC

FOP Servlet Implementation

Hi, 

I am interested in developing a FOP servlet which will take a HTTP post data request containing a (.fo) formatting object definition.  It will then return the PDF response directly to the browser.  Essentially it would require modifying the CommandLine class to use some sort of Input & Output Stream i imagine. 

I am new to this mailing list... Has anyone investigated, or pursued this idea? 

Cheers,
Julian.
BrightSparc Technologies
julianb@brightsparc.com

RE: FOP Servlet Implementation

Posted by Fernando Lopez Carballeda <fe...@lopez.org>.
Hi,

I think it's easy just convert the string with the .fo to an XML document
For example ( NOT TESTED !! ):

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import oracle.xml.parser.v2.*;
import org.apache.fop.apps.*;

public class procesar extends HttpServlet {

  /**
   * Initialize global variables
   */
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }

public class procesar extends HttpServlet {

  /**
   * Initialize global variables
   */
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }

  /**
   * Process the HTTP Post request
   */
  public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
    //
    String sxml = "";
    try { sxml = request.getParameter("xmlfo"); } catch (Exception e)
 e.printStackTrace(); }

    try{
        //Parseamos el string a un documento XML
        XMLDocument xml = null;

        // Create an oracle.xml.parser.v2.DOMParser to parse the document.
        DOMParser theParser = new DOMParser();

        // Open an input stream on the string
        ByteArrayInputStream theStream =
                 new ByteArrayInputStream( sxml.getBytes() );

        // Set the parser to work in non-Validating mode
        theParser.setValidationMode(false);

        try {

            // Parse the document from the InputStream
            theParser.parse( theStream );

            // Get the parsed XML Document from the parser
            xml = theParser.getDocument();

        } catch (SAXParseException s) {
                // Some code to process exception
        }catch (SAXException e) {
                // Some code to process exception
        }

// From Cocoon  FO2PDFFormatter

        String parserClassName = "org.apache.xerces.parsers.SAXParser";

        try {
            String version = Version.getVersion();
            XMLReader saxParser = (XMLReader)
Class.forName(parserClassName).newInstance();

            // setting the parser features
            try {

saxParser.setFeature("http://xml.org/sax/features/namespace-prefixes",
true);
            } catch (SAXException e) {
                // Some code to process exception
            }

            Driver driver = new Driver();
            driver.setRenderer("org.apache.fop.render.pdf.PDFRenderer",
version);

driver.addElementMapping("org.apache.fop.fo.StandardElementMapping");

driver.addElementMapping("org.apache.fop.svg.SVGElementMapping");

driver.addPropertyList("org.apache.fop.fo.StandardPropertyListMapping");

driver.addPropertyList("org.apache.fop.svg.SVGPropertyListMapping");

// For servlet response Not in Cocoon
            response.setContentType("application/pdf"); // <---- Must test
this.
            PrintWriter out = new PrintWriter (response.getOutputStream());
// End.

            driver.setWriter(out);
            driver.buildFOTree(xml);
            driver.format();
            driver.render();

// For servlet response Not in Cocoon
            out.close();
// End.
        } catch (Exception e) {
            // Some code to process exception
        }

}

  /**
   * Get Servlet information
   * @return java.lang.String
   */
  public String getServletInfo() {
    return "Example_servlets.example1";
  }
}



-----Mensaje original-----
De: Julian Bright [mailto:julianb@brightsparc.com]
Enviado el: viernes, 15 de diciembre de 2000 5:58
Para: FOP
Asunto: FOP Servlet Implementation


Hi,

I am interested in developing a FOP servlet which will take a HTTP post data
request containing a (.fo) formatting object definition.  It will then
return the PDF response directly to the browser.  Essentially it would
require modifying the CommandLine class to use some sort of Input & Output
Stream i imagine.

I am new to this mailing list... Has anyone investigated, or pursued this
idea?

Cheers,
Julian.
BrightSparc Technologies
julianb@brightsparc.com