Output PDF using ServletOutputStream

I just want to ask if there is anyway to output PDFs in a webpage using the ServletOutputStream? For example I want to make the PDF still show if there is not Adobe plugin installed or Chrome is not used.

asked Aug 24, 2011 at 0:39 171 1 1 silver badge 8 8 bronze badges

3 Answers 3

The user will have to have some type of PDF viewer installed on their PC to open/read the PDF file, whether it is Adobe Reader or something else. You can send the user a PDF file for either opening in the browser or download (Save as attachment) simply by sending the correct HTTP headers. Specifically:

Content-type: application/pdf Content-Disposition: attachment; filename=downloaded.pdf Content-Length: XXX Content-Transfer-Encoding: base64 

The Content-Disposition header is the one that suggests a download vs "open in browser". Once you have sent the headers, send a blank line and then write out your data (often base64 encoded).