This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Display byte array of PDF in browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Display byte array of PDF in browser" Watch "Display byte array of PDF in browser" New topic
Author

Display byte array of PDF in browser

Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
I am using a Servelt to display byte array of PDF into IE browser.

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

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

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

if(inputBytes !=null){
// inputBytes are not null, checked its correct size too.
outputStream.write(inputBytes);
}
outputStream.flush();
}

PDF does not displayed. There is already pdf file under C directory which I can manually open. inputBytes are not null, checked its size too.

Is there a better way to display pdf in browser from bytes?

Any help with code will be appreciated.
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Originally posted by Sam Gehouse:

PDF does not displayed.


Please have a look at Notes on Microsoft Internet Explorer, its for FOP, but still you can use the tips for ur case.


Spritle Software Blogs
Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
No luck with code below:

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

resp.setContentType("application/pdf");
resp.setHeader("Expires", "0");
resp.setHeader("Cache-Control","must-revalidate, post-check=0,
pre-check=0");
resp.setHeader("Pragma", "public");
//from sam
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){
outputStream.write(inputBytes);
}
outputStream.flush();
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display byte array of PDF in browser
 
Similar Threads
HttpServletOutputStream doesnt seem to be working correctly on glassfish
Display PDF in IE from byte array using Servlet
Response as PDF
Want to create Image from byte [] from internet
HttpServletOutputStream doesnt seem to be working correctly on glassfish