I want to show a Save As dialog when I send the PDF file which is generated by iText in a servlet. How can I achieve this?
2 Answers
You need to let the servlet set the Content-Disposition
header to attachment
.
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
This will force a Save As dialogue where the enduser can choose the location.
Please keep in mind that the enduser might have changed its browser settings to take a default action on PDF files, for example to always show it in Reader or to always save it in some fixed location. In for example Firefox you can control this by Tools > Options > Applications. No, you cannot change this browser specific behaviour from the server side on.

- 1,082,665
- 372
- 3,610
- 3,555
-
thanks but when I have to send header and how can i get the selected path in a java var? – FAjir May 12 '11 at 13:03
-
You have to send it before you write any bit to the response body. So before you call `getOutputStream()`. You cannot get the selected path back in the server side due to security restrictions. This information is useless anyway. If you really need to get it, the only which can do that is a signed(!) Java applet which is served by your webpage. There you have the freedom to write Swing code the way you want. – BalusC May 12 '11 at 13:07
-
oulala haven't time to develop a gui, I can't imagine there is no simple way to let the user choose the place of the pdf download. Originally I create the pdf on my user home dir (because it works on localhost for the moment), I just want to redirect the user to the file in the way that navigator try to open the file or ask to save! It's sure there is a way. – FAjir May 12 '11 at 13:15
-
1There is certainly a way to let the user choose the place of the PDF download. I've even answered that. Just set `Content-Disposition` header to `attachment` and then write PDF to the response outputstream. Just add the `response.setHeader()` line to your servlet code before `response.getOutputStream()` line. That's all. There is only no way to get the selected path back in servlet. That's also completely useless. The PDF file is then also already saved. The server cannot access it anyway by `new File()` or something. It's a physically different local disk file system. – BalusC May 12 '11 at 13:22
Ok I solved my problem!! I found on this page : http://www.geek-tutorials.com/java/itext/servlet_jsp_output_pdf.php
The method is to write directly with getOutputStream() (not in file path) and send content type header!
response.setContentType("application/pdf");
Document document = new Document();
try{
PdfWriter.getInstance(document,
response.getOutputStream());
//pdf generate code
It was so simple...

- 4,384
- 2
- 26
- 30
-
2So you wasn't doing that at all?! Note that this does not necessarily show a *Save As* dialog. The browser may by default display it inline without asking where to save. I understood that this happened to you and that you want to pop a *Save As* instead. – BalusC May 12 '11 at 13:35