If I understand you correctly, in your servlet you wish to apply an xsl
style sheet (from a file) to an xml string in memory to generate a FO xml
string in memory to run through FOP to generate a PDF in memory which you
want to return to the browser.  If that is the case, here are the pieces for
your servlet...


These are class variables...
   TransformerFactory tFactory    = null;
   Transformer        transformer = null;

Place this code in your init method to save reading and parsing the xsl file
each time your servlet is called...
   tFactory = TransformerFactory.newInstance();
   String xslFile = "myfile.xsl";
   File theFile = new File(xslFile);
   Source xslSource = new StreamSource(theFile);
   transformer = tFactory.newTransformer(xslSource);

This code goes in your doGet / doPost method and replaces the code from the
previous email...
   try
   {
      String xmlString = ... function to generate your xml String.
      Writer out = new StringWriter();
      Source xmlSource = new StreamSource(new StringReader(xmlString));
      transformer.transform(xmlSource, new StreamResult(out));
      out.close();
      String fopstring = out.toString();
      InputSource foSource = InputSource(new StringReader(fopstring));
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      response.setContentType("application/pdf");
      Driver driver = new Driver(foSource, out);
      driver.setRenderer(Driver.RENDER_PDF);
      driver.run();
      byte[] content = out.toByteArray();
      response.setContentLength(content.length);
      response.getOutputStream().write(content);
      response.getOutputStream().flush();
   }

I hope this helps.  I wish this example was part of the examples provided
with FOP.  Maybe a FOP developer (I'm just a user) can add an example like
this to the FOP package.

Jim Urban


-----Original Message-----
From: Marc Jenzer [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, July 11, 2001 8:27 AM
To: [EMAIL PROTECTED]
Subject: RE: how starting FOP without XML-File


Thanks for the answer!

But with your solution, I can't pass the fop xsl file. This file is on the
filesystem. Could you help me?

Thanks

  -----Original Message-----
This code will read the XML from a String object and write the PDF output
back to the browser.:

      try
      {
         String fopstring = .... your function to generate the XML
         InputSource foSource = InputSource(new StringReader(fopstring));
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         response.setContentType("application/pdf");
         Driver driver = new Driver(foSource, out);
         driver.setRenderer(Driver.RENDER_PDF);
         driver.run();
         byte[] content = out.toByteArray();
         response.setContentLength(content.length);
         response.getOutputStream().write(content);
         response.getOutputStream().flush();
         response.flushBuffer();
      }

Jim Urban

  -----Original Message-----
  From: Marc Jenzer [mailto:[EMAIL PROTECTED]]
  Sent: Wednesday, July 11, 2001 7:14 AM
  To: [EMAIL PROTECTED]
  Subject: how starting FOP without XML-File


  Hallo

  I generate my PDF-File in a servlet:

          File xmlFile    = new File("c:/temp/test.xml");
          File xslFile    = new File("c:/temp/test.xsl");
          InputHandler inputHandler = new XSLTInputHandler(xmlFile,
xslFile);

          org.xml.sax.XMLReader parser = inputHandler.getParser();
          driver.buildFOTree(parser, inputHandler.getInputSource());
          driver.format();
          driver.setOutputStream(out);
          driver.render();

  In the above example the xml input source is  a file. But I would rather
pass the input source xml file (text.xml) in the memory. It's possible and
how can I do this?

  Thanks

  Marc


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

Reply via email to