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.