You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Apache Wiki <wi...@apache.org> on 2006/02/01 21:31:39 UTC

[Struts Wiki] Update of "ServingPdfDocuments" by FrankZammetti

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Struts Wiki" for change notification.

The following page has been changed by FrankZammetti:
http://wiki.apache.org/struts/ServingPdfDocuments

New page:
The subject of serving PDF documents comes up rather frequently on the Struts mailing lists.  This seems like an obvious candidate for a Wiki entry!

It should be noted right up front that serving PDFs, and the issues that frequently come up, are in no way specific to Struts (although there may indeed be some Struts-specific issues).  This is a general requirement which can be troublesome whether using Struts or not, so feel free to refernce this entry outside the Struts world as well.

First up, let's see an example of serving a PDF from a Struts Action:

{{{
public ActionForward execute(ActionMapping mapping, ActionForm inForm, HttpServletRequest request, HttpServletResponse response) throws Exception {

  // somePDFGenerator is some class that returns a ByteArrayOutputStream
  // representation of a PDF.  Could be iText, could be DataVision, could be your own
  // custom class, whatever.
  ByteArrayOutputStream pdfStream = somePDFGenerator.generatePDF(); 
  response.setContentType("application/pdf"); 
  response.setHeader("Content-Disposition", "inline; filename=myPDF.pdf"); 
  response.setContentLength(pdfStream.size()); 
  ServletOutputStream sos = response.getOutputStream(); 
  pdfStream.writeTo(sos); 
  sos.flush(); 
  sos.close(); 
  pdfStream.close();
  pdfStream = null; 
  return null; 

}
}}}

This is clearly not meant to be robust, production-quality code, but it should get the basic point across.  It should be noted that the StrutsFileDownload Wiki entry shows how to do this using the DownloadAction introduced with Struts 1.2.6.  In general, I suggest using the DownloadAction whenever you need to serve content like this if you want to do it directly from with a Struts apps.

So, what issues can arise?  Here are just a few, and please do feel free to add from your own experience:

* IE will not react well if you do not specify the contentLength properly, so always be sure to do so.

* If you use the nocache setting on the Struts request processor, you may get an error saying the file could not be retrieved, or text to that effect.  This may only happen in IE and may only happen under SSL.  Removing this setting fixes the problem.  If you want to control caching without experiencing this problem, a filter like the Cache Control Filter in Java Web Parts (http://javawebparts.sourceforge.net) will allow you to do so.

* In some cases, the contentType "application/pdf" will not work properly.  Try using "application/x-octet-stream" instead.

* If you want the client to save the file rather than try and open it, change the Content-Disposition header to "attachment;filename=myPDF.pdf".  In this case especially you may have to use the alternate contentType mentioned above for IE clients.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org