Title: RE: Help - xml to pdf using struts

Here is the bit of my action that deals with the pdf creation.

First I transform xml to fo using xsl (and Xalan). The result is written to an InputSteam, which is passed into FOP. The result from FOP is then written out the client and the content type is set to "application/pdf".

HTH

Lisa

-----Original Message-----
From: Zimmer, Robin (SSABSA) [mailto:[EMAIL PROTECTED]]
Sent: 15 August 2002 09:37
To: 'Struts Users Mailing List'
Subject: RE: Help - xml to pdf using struts


Lisa, That sounds can you provide an example action please.

-----Original Message-----
From: Lisa van Gelder [mailto:[EMAIL PROTECTED]]
Sent: Thursday, 15 August 2002 6:01 PM
To: 'Struts Users Mailing List'
Subject: RE: Help - xml to pdf using struts


I do the xml -> fo -> pdf transformation in an Action, and then stream the
result straight back to the browser.

Lisa

-----Original Message-----
From: Zimmer, Robin (SSABSA) [mailto:[EMAIL PROTECTED]]
Sent: 15 August 2002 09:28
To: 'Struts Users Mailing List'
Subject: RE: Help - xml to pdf using struts


Thanks for getting back to me. I appreciate that I have to have xsl:fo to
transform the xml, but how best to render this using jsp??? Or do I need a
servlet???

-----Original Message-----
From: David WIlkinson [mailto:[EMAIL PROTECTED]]
Sent: Thursday, 15 August 2002 5:57 PM
To: 'Struts Users Mailing List'
Subject: RE: Help - xml to pdf using struts


We had to address a similar issue of creating pdf's from xml and found
that it can be done in a simple way using xsl:fo to transform the xml.

Check out http://xml.apache.org/fop/index.html for more info.


-----Original Message-----
From: Zimmer, Robin (SSABSA) [mailto:[EMAIL PROTECTED]]
Sent: 15 August 2002 08:13
To: '[EMAIL PROTECTED]'
Subject: Help - xml to pdf using struts

I am in the last stages of implementating a Struts application. I now
have
to complete the reporting side. This basically consists of displaying
xml
output as HTML or PDF. I have the HTML side covered (I am using jakarta
XTags) but how do I render an xml to fo transformation to pdf by using
struts. I am aware of stxx but as far as I can see you have to override
the
actionservlet etc and this seems an overhead for what is a small part of
the
app. Can anyone please give some suggestions.

--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>





--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>

--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

 

 public void createPDF(XMLDocument doc, HttpServletResponse response, String 
tomcatHome) throws ServletException
    {
        // set up the stream result
        Document xmlSrc = doc.getDOM();
        Logger log = null;
        String xslPath = tomcatHome + "/webapps/mytool/xsl/reportgen-pdf.xsl";
        StreamResult streamResult = new javax.xml.transform.stream.StreamResult();
        ByteArrayOutputStream strToWrite = new ByteArrayOutputStream();
        streamResult.setOutputStream((ByteArrayOutputStream)strToWrite);
        
        try
        {
            // transform the xml into FO
            javax.xml.transform.TransformerFactory tFactory = 
                    javax.xml.transform.TransformerFactory.newInstance();
        
            javax.xml.transform.Transformer transformer = tFactory.newTransformer
                    (new javax.xml.transform.stream.StreamSource(xslPath));
       
            transformer.transform
                    (new javax.xml.transform.dom.DOMSource(xmlSrc), 
                     streamResult);
            
            // get the FO and turn it into an input stream
            if(log == null) 
            {
             log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
             MessageHandler.setScreenLogger(log);
            }
            ByteArrayOutputStream bos1 = 
(ByteArrayOutputStream)streamResult.getOutputStream();
            InputStream is = new ByteArrayInputStream( bos1.toByteArray());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.setContentType("application/pdf");
            
            // convert the FO into pdf
            Driver driver = new Driver(
               new InputSource( is ),
               out
           );
            driver.setLogger(log);
            driver.setRenderer(Driver.RENDER_PDF);
            driver.run();
            
            // write the result out to the page
            byte[] content = out.toByteArray();
            response.setContentLength(content.length);
            response.getOutputStream().write(content);
            response.getOutputStream().flush();
        }
        catch (TransformerFactoryConfigurationError e)
        {
            logClass.error("TransformerFactoryConfigurationError caught while creating 
pdf using "+ xslPath, e);
            throw new ServletException("TransformerFactoryConfigurationError caught 
while creating pdf ", e);
        }
        catch (TransformerException e)
        {
            logClass.error("TransformerException caught while creating pdf using "+ 
xslPath, e);
            throw new ServletException("TransformerException caught while creating pdf 
", e);
        }
        catch (IOException e)
        {
            logClass.error("IOException caught while creating pdf using "+ xslPath, e);
            throw new ServletException("IOException caught while creating pdf ", e);
        }
        catch (IllegalArgumentException e)
        {
            logClass.error("IllegalArgumentException caught while creating pdf using 
"+ xslPath, e);
            throw new ServletException("IllegalArgumentException caught while creating 
pdf ", e);
        }
        catch (FOPException e)
        {
            logClass.error("FOPException caught while creating pdf using "+ xslPath, 
e);
            throw new ServletException("FOPException caught while creating pdf ", e);
        }
    }
--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>


Reply via email to