How to show a Save As dialog for a iText generated PDF?
I want to show a 开发者_JAVA技巧Save As dialog when I send the PDF file which is generated by iText in a servlet. How can I achieve this?
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.
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...
精彩评论