aspose file tools*
The moose likes Servlets and the fly likes Generate pdf file and display in a browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Generate pdf file and display in a browser" Watch "Generate pdf file and display in a browser" New topic
Author

Generate pdf file and display in a browser

vinitha simon
Ranch Hand

Joined: Dec 13, 2007
Posts: 64
Hi,
I have a J2ee application, in which on the click of a link, a pdf file needs to be generated and displayed on a new tab or window.

Currently, the pdf is generated, but, when written to the output stream, and redirected to the view, the screen just hangs.. I assume there is an exception of some sort which is not being logged for some reason.

I am not sure how to go about debugging this..

Any help is appreciated,

TIA
Aneesh Vijendran
Ranch Hand

Joined: Jun 29, 2008
Posts: 125
Without even giving a code snippet, it's tough to debug. It could be, if you are reading to a byte array, the byte array might have been initilized to accomodate more memory.

Try debugging your code in eclipse using breakpoints. You can run your tomcat inside or outside eclipse and then debug.

http://www.ibm.com/developerworks/library/os-ecbug/


Cheers
Aneesh
hayden wangg
Greenhorn

Joined: Apr 25, 2007
Posts: 11
Add try{}catch to the top method and log the exception information.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
vinitha simon wrote:Hi,
I have a J2ee application, in which on the click of a link, a pdf file needs to be generated and displayed on a new tab or window.

Currently, the pdf is generated, but, when written to the output stream, and redirected to the view, the screen just hangs.. I assume there is an exception of some sort which is not being logged for some reason.

I am not sure how to go about debugging this..

Any help is appreciated,

TIA


Did you set the MIME type correctly? Something like 'application/pdf'
Additionally you can try clearing you browser cache and also redeploy the webapp on the server after cleanly undeploying the app from the server(like removing the web app's folder from work directory in tomcat)


Thanks and Regards
amarshi mohanty
Ranch Hand

Joined: Jul 01, 2008
Posts: 110
Hope this will help you..

Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos);
document.setPageCount(1);
document.open();
document.add(Add Your Object);
document.close();
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");

response.setContentType("application/pdf");
response.setContentLength(baos.size());
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();



Thanks
Amarshi



SCJP (97%), SCWCD (95%))
vinitha simon
Ranch Hand

Joined: Dec 13, 2007
Posts: 64
I think the pdf file is not being generated correctly, I did verify the mimetype as well.

But, the java code does not throw any exceptions. Is that normal?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18881
    
    8

Yes, it's quite normal for Java code to not throw exceptions. It's still possible that your code has bugs, though, even if it doesn't throw an exception. In fact I would guess it's quite likely, since it isn't doing what you want it to do. That's the definition of "bug".
vinitha simon
Ranch Hand

Joined: Dec 13, 2007
Posts: 64
Thanks everyone for the help, I think I'm just going to rewrite my code and try to get it working.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generate pdf file and display in a browser