我正在使用itext pdf生成我的pdf,并且已正确保存在服务器中,但应在客户端下载(保存)。我调用了从jsp页面生成pdf的方法。这是我的代码:
calling:
Document b = new JavaPdfHelloWorld().Generate_pdf(con.getCon(), file_no, pdt);
definition:
public class JavaPdfHelloWorld {
public Document Generate_pdf(Connection con, String file_no, PensionDataDao pdt) throws IOException, IOException {
Document document = new Document();
String home = System.getProperty("user.home");
System.out.println(home);
File file = new File(home + "/Downloads/dcrg-diff/" + name_of_pensioner + " " + space + " "
+ updated_file_no + ".pdf");
FileOutputStream pdfFileout = new FileOutputStream(file);
PdfWriter.getInstance(document, pdfFileout);
document.open();
document.add(new Paragraph("CALCULATION SHEET AS PER GRATUITY ACT 1972",
FontFactory.getFont(FontFactory.COURIER_BOLD, 18, Font.BOLD, BaseColor.RED)));
document.close();
writer.close();
return document;
您需要将FileOutputStream写入Response对象,而不仅仅是返回document。与此同时,您还需要将Response的Content-Type设置为“ application / pdf”。