Back Forum Reply New

Sending PDF content in VIEW...

Hi -

I'm using Spring 2.5.6 MVC modules.  In my web application, when the user clicks on a PDF icon I display, I need to return and let the browser display the PDF document that the user is requesting.   I've seen examples on how to use the AbstractPdfView to create a PDF object, and insert all desired attributes to it...   but I'm not sure I need all that....

I have a custom DAO component that I can use to fetch a byte[] containing complet PDF content from the back end.  All I have to do is send it back to the user with the proper content designator - is there an example on how to do that?!

Thanks,
James

just stream your byte[] through the ServletOutputStream and return null from the controller to indicate to the dispatcher servlet that the controller completed view processing.

Code:
/* proper exception handling not shown */
public String getPdf(fromServletResponse response) {   byte[] pdf = pdfService.getBytes(quot;somefile.pdfquot;);   ServletOutputStream out = response.getOutputStream();   response.setContentType(quot;application/pdfquot;);
   /* while byte[] is not empty; out.write(moreBytes) */
   out.flush();   return null;
}
If your PDF's are large, you may consider returning an InputStream from the back end and simply connect it to the ServletOutputStream in the controller.  This would be more memory efficient.


Originally Posted by davisonIf your PDF's are large, you may consider returning an InputStream from the back end and simply connect it to the ServletOutputStream in the controller.  This would be more memory efficient.

Thanks very much - yes, that did work just fine!

Just out of curiosity, how would you 'connect' the InputStream to the ServletOutputStream - sounds interesting...
¥
Back Forum Reply New