File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Display PDF in IE from byte array using Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Display PDF in IE from byte array using Servlet" Watch "Display PDF in IE from byte array using Servlet" New topic

Display PDF in IE from byte array using Servlet

Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
I am using a Servelt to display byte array of PDF into IE browser. Correct number of bytes are returned from existing PDF. Any help with code will be appreciated. I am using code below:

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

resp.setHeader("Expires", "0");
resp.setHeader("Cache-Control","must-revalidate, post-check=0,
resp.setHeader("Pragma", "public");

resp.setHeader("Pragma", "no-cache"); //HTTP 1.0
resp.setDateHeader("Expires", 0); //prevents caching at the proxy server
resp.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
resp.setHeader("Cache-Control", "max-age=0");

resp.setHeader("Content-disposition", "inline; filename=stuff.pdf");

byte[] inputBytes = MyTest.getBytesFromFile(new File("C:\\TR.pdf"));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

if(inputBytes !=null){
Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81

Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
I only get Base64 encoded String that makes up PDF. I do not get the PDF itself as argument. So, my input argument is String. I decode that String into byte array.

So, my goal is to convert byte array back to PDF for display purpose.

Any help with sample code?
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294

Note that Sun doesn't recommend to use any classes from misc. However if your app isn't going to last forever, then you can use the package.

Tough in space?, <a href="" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="" target="_blank" rel="nofollow">Check it here.</a><br /><a href="" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
I agree. Here's the link:
subject: Display PDF in IE from byte array using Servlet
It's not a secret anymore!