Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Downloading PDF

 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2270
76
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You should either use
or
in the same Servlet.

Regards,
Frits
 
Luan Cestari
Ranch Hand
Posts: 163
C++ Redhat Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".

 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
C++ Redhat Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2270
76
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
C++ Redhat Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You SIR
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic