I've been doing XML+XSL+FOP transforms in a servlet for months.  I have
a servlet that takes an xml and an xsl parameter (either a url to a file, a
url to a jsp page that dynamically generates the xml, or an ObjectID that
where the Object is taken from an Object database and transformed to xml in
memory).  It then passes it through xalan and if necessary fop and returns
either xml, html, or a pdf.

Here is some code adapted from that servlet:


String xmlData = "";     //--- This is your xml in memory
String xslData = "";     //--- Tis is your xsl in memory

StreamSource xmlSource = new StreamSource(new StringReader(xmlData));
StreamSource xslSource = new StreamSource(new StringReader(xslData));

StringWriter transformOut = new StringWriter();

StreamResult xsltOut = new StreamResult(transformOut);

     try {
      Transformer transformer = factory.newTransformer(xslSource);
      transformer.transform(xmlSource, xsltOut);
     }catch(Exception e) {System.out.println("XSLT Exception: " + e);
e.printStackTrace();}


  ByteArrayOutputStream pdfContent = new ByteArrayOutputStream();

   InputSource fopIn = new InputSource(new
StringReader(transformOut.toString()));

   response.setContentType("application/pdf");
   response.addHeader("Content-Disposition", "inline; filename=report.pdf");
   BufferedOutputStream out = new
BufferedOutputStream(response.getOutputStream());

   try {
    String parserClassName = System.getProperty("org.xml.sax.parser");
    if (parserClassName == null) parserClassName =
"org.apache.xerces.parsers.SAXParser";
    XMLReader parser =
(XMLReader)Class.forName(parserClassName).newInstance();

    parser.setFeature("http://xml.org/sax/features/namespace-prefixes";,
true);
    String version = Version.getVersion();
    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.addElementMapping("org.apache.fop.extensions.ExtensionElementMapping"
);
    driver.addPropertyList("org.apache.fop.fo.StandardPropertyListMapping");
    driver.addPropertyList("org.apache.fop.svg.SVGPropertyListMapping");

driver.addPropertyList("org.apache.fop.extensions.ExtensionPropertyListMappi
ng");

    driver.setOutputStream(pdfContent);
    driver.buildFOTree(parser, fopIn);
    driver.format();
    driver.render();
   }catch(Exception e) {System.out.println("FOP Exception: " + e);}

   byte[] pdfDoc = pdfContent.toByteArray();

   response.setContentLength( pdfDoc.length );
   out.write( pdfDoc);
  }
   out.flush();
   out.close();


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to