File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Downloading a PDF using JSP-JS-Java Servlet

 
Kimon Pi
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Pie
Posts: 20166
24
MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.

  •  
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic