gpt4 book ai didi

java - 在java中的HTTP请求中生成PDF作为输出流

转载 作者:行者123 更新时间:2023-12-02 05:21:34 24 4
gpt4 key购买 nike


我正在使用以下代码片段在 java
中的 HTTP 请求中生成 PDF 作为输出流 代码是:-

        webAppAccess.getHttpServletResponse().setContentType("application/pdf");

try {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, webAppAccess
.getHttpServletResponse().getOutputStream());
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World"));
document.add(new Paragraph(new Date().toString()));
// step 5
document.close();
} catch (DocumentException de) {
throw new IOException(de.getMessage());
}

我在屏幕上显示 pdf

%PDF-1.4 %���� 2 0 obj <>stream x�+�r �26S�00SI�2P�5�1���BҸ4>>>/MediaBox[0 0 595 842]/Parent 3 0 R/Contents 2 0 R/Type/Page>> endobj 1 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj xref 0 7 0000000000 65535 f 0000000331 00000 n 0000000015 00000 n 0000000419 00000 n 0000000174 00000 n 0000000470 00000 n 0000000515 00000 n trailer <]/Info 6 0 R>> %iText-5.4.0 startxref 668 %%EOF


请帮我解决同样的问题

最佳答案

您可能想要添加一个 header ,其中提到响应是附件 ->

response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);

关于java - 在java中的HTTP请求中生成PDF作为输出流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26468395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com