users@glassfish.java.net

RE: Glassfish + JSF + stream pdf to output -- please sun guys...help!!!

From: Jason Lee <lee_at_iecokc.com>
Date: Wed, 15 Nov 2006 11:44:33 -0600

If I understand you correctly, we do the exact same thing. We have a
JSF app (JSF 1.2, GF V1UR1) that creates a PDF based on user selections,
then sends the PDF to the client's browser. Currently, the hack we use
is this: We store the PDF reference in our session-scoped backing bean.
On the page where we want the file to download, we have an iframe like
this:
 
<iframe src="<%=request.getContextPath()%>/tag.jsp" style="visibility:
hidden" width="0%" height="0" ></iframe>
 
and tag.jsp looks like this:
 
<%@ page language="java" %>
<%@ page import="com.iecokc.pentitulus.view.bean.*"%>
<%@ page import="com.iecokc.pentitulus.pdf.*"%>
<%@ page import="java.io.ByteArrayOutputStream"%>
<%@ page import="java.io.IOException"%>
 
<%
 FivePartTagBean fptBean =
(FivePartTagBean)session.getAttribute(BeanNames.FIVE_PART_TAG_BEAN);
 
 ByteArrayOutputStream baos = fptBean.getBaos();
 
    if(baos == null){
        TestPDF pdf = new TestPDF();
        baos = pdf.generateTestPDF("Testing - baos was null.");
    }
 
 ServletOutputStream sos;
 
  try {
       
   // write the baos to the Servlet Output Stream
      //String cacheControl = "Cache-Control";
      // String maxAge = "max-age=0";
      String contentType = "application/pdf";
   response.setHeader("Content-Disposition", "attachment;
filename=\"Tag.pdf\"");
   response.setContentType(contentType);
 
   sos = response.getOutputStream();
   //sos.write(baos.toByteArray());
   baos.writeTo(sos);
   sos.flush();
 

  }
  catch (IOException ioe){
   System.out.println("IO Exception: " + ioe.toString());
  }
  finally
  {
   if (baos != null) {
       baos.reset();
   }
  }
  
  out.clear();
  out = pageContext.pushBody();
%>
 
Granted, that's all very ugly and not very "JSFy", which we plan to fix
when we find the time, but it works. I hope that helps.
 
-----
Jason Lee, SCJP
JSF RI Dev Team
Programmer/Analyst
http://www.iec-okc.com <http://www.iec-okc.com/>
 


  _____

        From: Derick Potgieter [mailto:derick.potgieter_at_gmail.com]
        Sent: Wednesday, November 15, 2006 11:37 AM
        To: users_at_glassfish.dev.java.net
        Subject: Glassfish + JSF + stream pdf to output -- please sun
guys...help!!!
        
        
        Hi,
         
        Has any one done this...streamed a dynamic pdf to output on
browser?
        I`ve done this on 8.2 by sending the page to a servlet on the
page navigation. Then called the request.getOutputStream().write(...).
         
        Now this seemed to work perfectly...until i upgraded to
glassfish where i think JSF 1.2 is screwing me up....now i havent
selected it to be included in Netbeans 5.5 + web pack. But i recon the
server obviously includes it because of enterprise 5 compatibility.
         
        Now i`ve tested it by creating a new web app without JSF support
and streaming from there in a servlet....and this works like a dream.
        But using the same library and code in the JSF app gives me the
pdf 19 pages big....but nothing in it.
         
        I`ve compared the file from the servlet without JSF
framework....and it is different to the one (some characters are
different, but the general layout is the same.)
         
        Is there maybe a rule in the new JSF 1.2 specification that
doesnt allow you to swap from your action class to a servlet....or mabe
doesnt allow you to stream out....or mabe changes the bytes in a
teleporter and then doesnt output write.
         
        Please....can anyone look at this....please??
         
        I`m now at the point of just making a small JSF app and
streaming out via a servlet to check if it is the JSF stuff causing
crap....this same approach works in a plain jsp+servlet app.
         
        Thanks for all the reading..
         
        Rgds
        Derick