aspose file tools*
The moose likes JSP and the fly likes Pdf opens blank in browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Pdf opens blank in browser" Watch "Pdf opens blank in browser" New topic
Author

Pdf opens blank in browser

Neha Gujarathi
Greenhorn

Joined: Jul 23, 2008
Posts: 3
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

Joined: Jun 06, 2007
Posts: 2709
    
    5

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?


SCJP, SCWCD.
|Asking Good Questions|
Neha Gujarathi
Greenhorn

Joined: Jul 23, 2008
Posts: 3
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
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
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).


Ping & DNS - updated with new look and Ping home screen widget
Neha Gujarathi
Greenhorn

Joined: Jul 23, 2008
Posts: 3
hey... I did close the FileOutputStream... It did not help...
And the pdf getting generated on the server is valid..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
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

Joined: Jan 28, 2003
Posts: 3932
    
  17

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.


Steve
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pdf opens blank in browser
 
Similar Threads
file dialog does not close in IE 6
Problem in writing concatenated byteArray to a PDF file
iText Silent Print
Response Output as PDF
Response as PDF