• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Downloading a PDF using JSP-JS-Java Servlet

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I am trying to create a PDF file on my server-side and then send it back via the response to be downloaded from the client browser.

What happens, is the following:
1.The user clicks on a button on the jsp, to create/download the pdf.
2. The servlet is called using js (jquery).
3. The PDF file is generated on the server side.
4. I get an alert box (using js) saying 'PDF generation succeeded'.
5. However, I get no download option from Mozzila (nor chrome, nor IE).

Here is the code I use in my doPost servlet method to create and then download the pdf file:

String pdfFilename = docManager.exportOrderToPDF(
Long.parseLong(orderId), Long.parseLong(userId));

response.setCharacterEncoding("utf-8");
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename="
+ pdfFilename);

System.out.println("PDF to download:" + pdfFilename);

String fullPathToPDF = docManager.PDF_DIRECTORY + File.separator
+ pdfFilename;
File pdf = new File(fullPathToPDF);
response.setContentLength((int) pdf.length());

System.out.println("Full path to PDF:" + fullPathToPDF);

ServletOutputStream sos = response.getOutputStream();
FileInputStream input = new FileInputStream(pdf);
BufferedInputStream buf = new BufferedInputStream(input);

System.out.println("Starting to write bytes to output stream.");
int readBytes = 0;
while ((readBytes = buf.read()) != -1) {
sos.write(readBytes);
}
System.out.println("Finished writing bytes to output stream.");

sos.flush();
sos.close();
input.close();



So the servlet runs fine, up to the point where it is supposed to send the PDF file for download by the user.

I realize this is a topic well-discussed in various occasions in the past, however the proposals I found did not solve my problem.

If you feel I have 'woken up any zombies' please feel free to refer me to older posts I might have missed.

Best regards,
K.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay... so that's your servlet code. Doesn't look too bad. But you said your web page was submitting the request via JQuery, didn't you? In which case it's your responsibility to handle the response which is sent back to you. Don't expect the browser to handle it -- the browser doesn't handle the responses for JQuery requests.

Of course now that you have the PDF data coming back to your Javascript, there's nothing you can do with it. Javascript doesn't have access to the client's file system. So you should take a step back and reconsider your design. If you want the browser to handle the download of the PDF -- and you do want that -- then you can't use an AJAX request to produce that download.
 
Kimon Pi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply, I will give it a shot with a more 'traditional' non-js way.

Still, does anyone know how I would be able to retrieve the response via jquery/js?

Rgds,
K.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the insistence on using jQuery and JavaScript? Unless we know why you want to do such an odd thing, how can we advise the best way to do it?
 
Kimon Pi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use jquery to validate and submit the forms throughout my app. So I wanted to use jquery for the purpose I mentioned earlier, just to maintain a level of consistency.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You still haven't explained what you are trying to accomplish, but here are a few points:

  • Just because you are using a tool, like jQuery, for some things, it doesn't mean you have to use it for everything. "Keep it simple" and don't used advanced tools where they are not needed.


  • If you are submitting the form as a full page reload, just use a submit button. I can think of no reason at all not to.


  • If you are submitting though Ajax, we need to know that.

  •  
    reply
      Bookmark Topic Watch Topic
    • New Topic