Forums Register Login

Generate pdf file and display in a browser

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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/

+Pie Number of slices to send: Send
Add try{}catch to the top method and log the exception information.
+Pie Number of slices to send: Send
 

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)
+Pie Number of slices to send: Send
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


+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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".
+Pie Number of slices to send: Send
Thanks everyone for the help, I think I'm just going to rewrite my code and try to get it working.

For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4475 times.
Similar Threads
edit pdf file by using itext
iText: Digital signature
Publishing directory in Websphere App Server
PDF creation
how to change the font-family in XSLT file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:17:12.