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

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

我正在使用itext-pdf来生成我的pdf,它正确地保存在服务器中,但应该在客户端下载(保存)。我调用方法从jsp页面生成pdf。这是我的密码:

  1. calling:
  2. Document b = new JavaPdfHelloWorld().Generate_pdf(con.getCon(), file_no, pdt);
  3. definition:
  4. public class JavaPdfHelloWorld {
  5. public Document Generate_pdf(Connection con, String file_no, PensionDataDao pdt) throws IOException, IOException {
  6. Document document = new Document();
  7. String home = System.getProperty("user.home");
  8. System.out.println(home);
  9. File file = new File(home + "/Downloads/dcrg-diff/" + name_of_pensioner + " " + space + " "
  10. + updated_file_no + ".pdf");
  11. FileOutputStream pdfFileout = new FileOutputStream(file);
  12. PdfWriter.getInstance(document, pdfFileout);
  13. document.open();
  14. document.add(new Paragraph("CALCULATION SHEET AS PER GRATUITY ACT 1972",
  15. FontFactory.getFont(FontFactory.COURIER_BOLD, 18, Font.BOLD, BaseColor.RED)));
  16. document.close();
  17. writer.close();
  18. return document;
wswtfjt7

wswtfjt71#

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

相关问题