• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pdf opens blank in browser

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic