Announcement Announcement Module
Collapse
No announcement yet.
SWF & JSF - PDF downloads Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SWF & JSF - PDF downloads

    Dear SWF users,

    I have trolled the forum but can't find an answer to my problem, so here it is.

    Simple thing I want: In a view where the user can edit some information I also want a print button, which transforms the information into a PDF and sends it to the user for download. After that he resumes editing the information or goes back to an overview view.

    I implemented this according to the spring documentation under "Streaming actions" in the SWF documentation.

    The problem is though that the response which holds the PDF is followed by SWF's redirect which concludes in the behaviour that the user can't download the file, but just gets the same view presented again.

    Here's part of the flow.xml:
    Code:
    <view-state id="Details">
       <transition on="save">
            ....
       </transition>
       <transition on="back" to="Overview">
           ...
       </transition>
    
       <transition on="print" >
          <evaluate expression="printAction"/>
       </transition>
    </view-state>
    Here's the Action code:
    Code:
    public class PrintAction extends AbstractAction {
    
    	private DocumentService documentService;
    	
    	protected Event doExecute(RequestContext context) throws Exception {
    		
    		LoadDetailBean detail = (LoadDetailBean ) context.getViewScope().get("load");
    		documentService.sendPdf(detail.getId(), context);
    		
    		return success();
    	}
    
            public DocumentService getDocumentService() {
    		return documentService;
    	}
    
    	public void setDocumentService(DocumentService documentService) {
    		this.documentService = documentService;
    	}
    
    
    }
    And here's the service code which holds the mark response complete call.
    Code:
    public class DocumentService {
    
    	private static Logger log = Logger.getLogger(DocumentService.class);
    	
    	private ILoadDao loadDao = new LoadDao();
    	
    	public void sendPdf(Integer loadId, RequestContext context) throws IOException, DocumentException {
    		Validate.notNull(loadId);
    		Validate.notNull(context);
    		
    		LoadPdfData loadPdfData = loadDao.getLoadPdfData(loadId);
    		
    		// get response
    		HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getNativeResponse();
    		response.reset();
    		response.setCharacterEncoding("UTF-8");
    		response.setContentType("application/force-download");
    		response.setHeader("Content-Disposition","attachment; filename=\"test.pdf\"");
    		OutputStream out = response.getOutputStream();
    		
    		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    		this.createPdf(loadPdfData , byteOut);
    		
    		response.setContentLength(byteOut.size());
    		byteOut.writeTo(out);
    		out.flush();
    		
    		// mark response complete
    		context.getExternalContext().recordResponseComplete();
    		
    	}
    	
    	private void createPdf(AnnexVIIData annexVIIData, OutputStream out) throws IOException, DocumentException {
    		[...]
    	}
    }
    The only way I got the dowload to work is to have the transition change to the flows end-state:
    Code:
    <view-state id="Details">
       <transition on="save">
            ....
       </transition>
       <transition on="back" to="Overview">
           ...
       </transition>
    
       <transition on="print" to="finish">
          <evaluate expression="printAction"/>
       </transition>
    </view-state>
    
    <end-state id="finish"/>
    But this is no option since the user should be able to continue editing information on the Detail view or go back to the Overview view and continue working.

    Can you please help me out? How do I tell SWF to not redirect on this transition?

    cheers
    remraf
    Last edited by remraf; Jul 15th, 2009, 07:32 AM.

  • #2
    If anyone has the same problem, I found the answer to the above problem. During further research on the web I found this JIRA, which pointed me in the right direction - I was using SWF 2.0.5 which didn't have the check (whether the responce complete flag was set) implemented accordingly. IN SWF 2.0.7 this was fixed and "Streaming actions" work now as intended and described in the SWF documentation linked above.

    hth

    cheers
    remraf

    Comment


    • #3
      I have a similiar Proble mwith webflow 2.0.8

      I've implementes an excel export with this streaming actions.

      The URL starts with https://.. and on Internet Explorer the dialog for open or save appears but then an error message appears. On Firefoy it works perfectly.

      Can anyne help me?

      Comment


      • #4
        Hello, I had a similar problem with IE and SSL.

        I resolve the problem adding:

        Code:
        response.setHeader("Pragma", "private");
        response.setHeader("Cache-Control", "max-age=0");

        Comment


        • #5
          Thank you migelct1983,

          It works perfectly

          Best Regards re79

          Comment

          Working...
          X