简体   繁体   中英

How to show a Save As dialog for a iText generated PDF?

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?

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM