struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Todd G. Nist" <tn...@bellsouth.net>
Subject RE: Help - xml to pdf using struts
Date Wed, 21 Aug 2002 14:27:07 GMT
Mark,

I have tested this out on 5.0, 5.01, 5.5, 6.0.26 and have not encountered
any problem with the page being displayed, what release of IE are you using?

I have attached a small servlet which will render a page straight to the
browser, at least under those listed above.

Hope it helps.

Regards,

Todd G. Nist

<------- Servlet -------->

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.ByteArrayOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;

import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import org.apache.fop.apps.Driver;
import org.apache.fop.apps.Version;
import org.apache.fop.apps.XSLTInputHandler;
import org.apache.fop.apps.FOPException;

import org.apache.fop.messaging.MessageHandler;

import javax.xml.transform.*;
import javax.xml.transform.stream.*;


public class GeneratePDF extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse
response)
        throws ServletException, IOException {
        doPost(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse
response)
        throws ServletException, IOException {

		String xslPath = "./";
		String xmlFile = pdfxml.xml;  // your file name here
		String xslFile = pdfxsl.xsl;  // your file name here
		Logger log = null;

        try {
        	// Initialize Logger
	    	log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
	    	MessageHandler.setScreenLogger(log);
        	// Initialier Input Handler
        	XSLTInputHandler input = new XSLTInputHandler(new File(xmlFile),
new File(xslFile));

            ByteArrayOutputStream out = new ByteArrayOutputStream();

        	Driver driver = new Driver();
        	driver.setLogger(log);
        	driver.setRenderer(Driver.RENDER_PDF);
        	driver.setOutputStream(out);
        	driver.render(input.getParser(), input.getInputSource());

			// write the result out to the page
            byte[] content = out.toByteArray();
            response.setContentType( "application/pdf" );
			//------------------------------------------------------------
			// Content-disposition header - don't open in browser and
			// set the "Save As..." filename.
			// *There is reportedly a bug in IE4.0 which ignores this...
			//------------------------------------------------------------
			//response.setHeader("Content-disposition", "attachment; filename=" +
"example.pdf");

            response.setContentLength(content.length);
            response.getOutputStream().write(content);
            response.getOutputStream().flush();
        }
        catch (IOException e)
        {
            log.error("IOException caught while creating pdf using "+
xslPath, e);
            throw new ServletException("IOException caught while creating
pdf ", e);
        }
        catch (IllegalArgumentException e)
        {
            log.error("IllegalArgumentException caught while creating pdf
using "+ xslPath, e);
            throw new ServletException("IllegalArgumentException caught
while creating pdf ", e);
        }
        catch (FOPException e)
        {
            log.error("FOPException caught while creating pdf using "+
xslPath, e);
            throw new ServletException("FOPException caught while creating
pdf ", e);
        }
    }
}

-----Original Message-----
From: mike.witt@honeywell.com [mailto:mike.witt@honeywell.com]
Sent: Tuesday, August 20, 2002 10:14 AM
To: struts-user@jakarta.apache.org
Subject: RE: Help - xml to pdf using struts


Yes.  The problem with IE is that it doesn't show the file and you get a
blank page.  This note at the fop site says:

"Some browsers have problems handling the PDF result sent back to the
browser. IE is particularly bad and different versions behave differently.
Having a ".pdf" on the end of the url may help. "

http://xml.apache.org/fop/embedding.html

-----Original Message-----
From: Todd G. Nist [mailto:tnist@bellsouth.net]
Sent: Friday, August 16, 2002 1:34 PM
To: 'Struts Users Mailing List'
Subject: RE: Help - xml to pdf using struts


RE: Help - xml to pdf using strutsMike,

Did you set the content type to "application/pdf" and then return "null"
from you action once you successfully completed the transformation?  That is
what we do and it appears to work fine.

Regards,

Todd G. Nist

try {
// FO/Transformation code here....

ByteArrayOutputStream pdf = new ByteArrayOutputStream();
// show PDF
response.setContentType("application/pdf");
byte[] content = pdf.toByteArray();
response.setContentLength(content.length);
response.getOutputStream().write(content);
response.getOutputStream().flush();
}

// return null from action

return null;

  -----Original Message-----
  From: mike.witt@honeywell.com [mailto:mike.witt@honeywell.com]
  Sent: Friday, August 16, 2002 11:43 AM
  To: struts-user@jakarta.apache.org
  Subject: RE: Help - xml to pdf using struts


  Hi Lisa,

  That looks similar to my solution.  The biggest problem I had was with
getting IE to display the PDF inline without a PDF in the URL  Using a
Struts action, I wasn't able to get the URL to change at the appropriate
time with a redirect.  In the end, I sent the pdf as an attachment which
gives the open/save prompt.  Were you able to get around this problem?

  Thanks, Mike
    -----Original Message-----
    From: Lisa van Gelder [mailto:lisa.vangelder@digitalrum.com]
    Sent: Thursday, August 15, 2002 4:52 AM
    To: 'Struts Users Mailing List'
    Subject: 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:robinz@ssabsa.sa.gov.au]
    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:lisa.vangelder@digitalrum.com]
    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:robinz@ssabsa.sa.gov.au]
    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:draw@iquo.co.uk]
    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:robinz@ssabsa.sa.gov.au]
    Sent: 15 August 2002 08:13
    To: 'struts-user@jakarta.apache.org'
    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:struts-user-unsubscribe@jakarta.apache.org>
    For additional commands, e-mail:
    <mailto:struts-user-help@jakarta.apache.org>






    --
    To unsubscribe, e-mail:
    <mailto:struts-user-unsubscribe@jakarta.apache.org>
    For additional commands, e-mail:
    <mailto:struts-user-help@jakarta.apache.org>

    --
    To unsubscribe, e-mail:
    <mailto:struts-user-unsubscribe@jakarta.apache.org>
    For additional commands, e-mail:
    <mailto:struts-user-help@jakarta.apache.org>

    --
    To unsubscribe, e-mail:
<mailto:struts-user-unsubscribe@jakarta.apache.org>
    For additional commands, e-mail:
<mailto:struts-user-help@jakarta.apache.org>






--
To unsubscribe, e-mail:
<mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail:
<mailto:struts-user-help@jakarta.apache.org>


--
To unsubscribe, e-mail:
<mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail:
<mailto:struts-user-help@jakarta.apache.org>



--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>


Mime
View raw message