This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pdf opens blank in browser

 
Neha Gujarathi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to open a pdf file in IE. But the problem is that it opens up a blank pdf page... When I download the pdf, its size is not 0 and it does contain the pdf author name,and title but its empty..

The code snippet

response.reset();
response.setHeader("Expires", "0");
response.setHeader("Cache-Control","cache");
response.setHeader("Pragma", "cache");
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment;filename=neha.pdf");

ServletOutputStream so = response.getOutputStream();

ByteArrayOutputStream output = new ByteArrayOutputStream();
InputStream in = new BufferedInputStream(new FileInputStream("neha.pdf"));
byte bytebuff[] = new byte[500];

for(int lengthread = 0; (lengthread = in.read(bytebuff)) != -1;){
output.write(bytebuff, 0, lengthread);
}

byte data[] = output.toByteArray();
response.setContentLength(data.length);
so.write(data);
in.close();
so.close();

Thanks a lot,

Neha
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Neha Gujarathi welcome to Javaranch,

The code snippet you provided above is from a JSP or a servlet?

it does contain the pdf author name,and title but its empty..

And if the PDF document does not contain any text in it, what do you think the browser will display?
 
Neha Gujarathi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey..Thanks for the reply... Its JSP...

And about the data in the file...Sorry forgot to add post code

here is the whole code

Document pdfDocument = new Document();
File file=new File("neha.pdf");
FileOutputStream fos = new FileOutputStream( file );
PdfWriter writer = null;
writer = PdfWriter.getInstance(pdfDocument, fos);
writer.open();
pdfDocument.open();
pdfDocument.addTitle("A sample PDF Title");
pdfDocument.addAuthor("Neha");
pdfDocument.add(new Paragraph("This is a sample PDF document "));
pdfDocument.add(new Paragraph("This is second Paragraph"));
writer.flush();
pdfDocument.close();
writer.close();

response.reset();
response.setHeader("Expires", "0");
response.setHeader("Cache-Control","cache");
response.setHeader("Pragma", "cache");
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment;filename=neha.pdf");

ServletOutputStream so = response.getOutputStream();

ByteArrayOutputStream output = new ByteArrayOutputStream();
InputStream in = new BufferedInputStream(new FileInputStream("neha.pdf"));
byte bytebuff[] = new byte[500];

for(int lengthread = 0; (lengthread = in.read(bytebuff)) != -1;){
output.write(bytebuff, 0, lengthread);
}

byte data[] = output.toByteArray();
response.setContentLength(data.length);
so.write(data);
in.close();
so.close();

Thanks a ton
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to close the "fos" stream. Also check whether the file that's generated on the server is actually a valid PDF file.

Also, you should get in the habit of Using Code Tags when posting code of any length. You're making it unnecessarily hard for people to read your code (and thus less likely for them to want to help you).
 
Neha Gujarathi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey... I did close the FileOutputStream... It did not help...
And the pdf getting generated on the server is valid..
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try to remove some unnecessary complexity from the code by writing from the input stream directly to the servlet output stream (instead of using a ByteArrayOutputStream and byte[] in between).
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should put this in a servlet, not a JSP, JSPs have too many things happening behind the scenes (if you check your error logs you may find some of them).

Example: You probably get an error when you use response.getOutputStream() since for a typical JSP the response.getWriter() has already been called and you aren't allowed to call both. You tell IE that the content is a PDF but it gets an error page which is HTML and can't display it so it appears blank. Your error logs would display the problem though.

In Servlets everything that is going on is much more transparent, so this kind of operation happens with less issues and easier to debug code. For a real good example of a download servlet look at BalusC's blog: The FileServlet.
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In particular your JSP almost certainly includes whitespace which is being written to the output ahead of the actual PDF content. That would make it not look like a PDF. So what Steve Luke says, yes. Put the code in a servlet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic