aspose file tools*
The moose likes Servlets and the fly likes Downloading a PDF using JSP-JS-Java Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Downloading a PDF using JSP-JS-Java Servlet" Watch "Downloading a PDF using JSP-JS-Java Servlet" New topic
Author

Downloading a PDF using JSP-JS-Java Servlet

Kimon Pi
Greenhorn

Joined: Apr 11, 2011
Posts: 3
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
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

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

Joined: Apr 11, 2011
Posts: 3
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kimon Pi
Greenhorn

Joined: Apr 11, 2011
Posts: 3
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

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.

  •  
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Downloading a PDF using JSP-JS-Java Servlet
     
    Similar Threads
    zip file to client
    open pdf inside browser
    Problem in downloading file from server using Http Response
    open a pdf file from a servlet
    .pptx and .docx problem