aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Downloading PDF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Downloading PDF" Watch "Downloading PDF" New topic
Forums: Web Component Certification (SCWCD/OCPJWCD) Servlets
Author

Downloading PDF

Pramod Kumar Pandey
Ranch Hand

Joined: Dec 05, 2013
Posts: 51

Dear Team, I did code for opening pdf , but the only loading image is coming instead of pdf content. Please help me .
my index.jsp is :
and my servlet is :


Thanks in advance
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

Hi,

You should either use
or
in the same Servlet.

Regards,
Frits
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 163

The problem is the missing Content-Type in the HTTP request header (so the browser is assuming it is HTML).

Try to put the ContentType as "application/pdf" (it is also recommended to use other headers such as Content-Disposition and Content-Length). This link provides a good example http://www.avajava.com/tutorials/lessons/how-do-i-serve-up-a-pdf-from-a-servlet.html

One more thing I would like to comment is if you use the right content-type, some browser like Chrome might want to intercept and show the preview in the page which you can then download it clicking on a button (instead of downloading it) this might not be desired, and if you want to just download it change the cntent-type to "application/octet-stream".


Please, visit me for some cool tech post at www.ourdailycodes.com
Pramod Kumar Pandey
Ranch Hand

Joined: Dec 05, 2013
Posts: 51

Frits Walraven wrote:Hi,

You should either use
or
in the same Servlet.

Regards,
Frits


Sir I've used already response.getOutputStream in line 26
Pramod Kumar Pandey
Ranch Hand

Joined: Dec 05, 2013
Posts: 51

Luan Cestari wrote:The problem is the missing Content-Type in the HTTP request header (so the browser is assuming it is HTML).

Try to put the ContentType as "application/pdf" (it is also recommended to use other headers such as Content-Disposition and Content-Length). This link provides a good example http://www.avajava.com/tutorials/lessons/how-do-i-serve-up-a-pdf-from-a-servlet.html

One more thing I would like to comment is if you use the right content-type, some browser like Chrome might want to intercept and show the preview in the page which you can then download it clicking on a button (instead of downloading it) this might not be desired, and if you want to just download it change the cntent-type to "application/octet-stream".



Sir I used already as you said in line 19.
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 163

Sorry but you didnt read and tried what I said. Take a close look as I mention more header parameters than just that one and also I sent you a link to an example. Try again with that example please.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

Pramod Kumar Pandey wrote:Sir I've used already response.getOutputStream in line 26

OK, and did you remove line 21?
Pramod Kumar Pandey
Ranch Hand

Joined: Dec 05, 2013
Posts: 51

Luan Cestari wrote:Sorry but you didnt read and tried what I said. Take a close look as I mention more header parameters than just that one and also I sent you a link to an example. Try again with that example please.


Sir that worked ! one more request please explain the meaning of response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName); in detail. because I'll have to tell trainee about this.
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 163

It is a definition made by w3c (which is the organization which rules the standards about HTML and other things, in an elevator speech). Which that header you are informing the web browser there is an attachment as payload and you are also informing its name (otherwise it doesnt know it).

http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html - 19.5.1 Content-Disposition


Pramod Kumar Pandey
Ranch Hand

Joined: Dec 05, 2013
Posts: 51

Thank You SIR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Downloading PDF