需要使用itext pdf在客户端计算机中下载pdf文件

jgzswidk  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(204)

我正在使用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;
wswtfjt7

wswtfjt71#

您需要将fileoutputstream写入响应对象,而不是仅仅返回文档。此外,您还需要将响应的内容类型设置为“application/pdf”。

相关问题