为什么';n在java servlet中创建pdf文档?
我使用iText/Pdfbox创建PDF文档。当我使用以下独立Java类创建PDF时,一切都正常:为什么';n在java servlet中创建pdf文档?,java,servlets,pdf-generation,itext,pdfbox,Java,Servlets,Pdf Generation,Itext,Pdfbox,我使用iText/Pdfbox创建PDF文档。当我使用以下独立Java类创建PDF时,一切都正常: public static void main(String[] args){ ... ... ... } 文档创建正确 但是我需要从Servlet创建一个PDF文档。我将代码粘贴到get或post方法中,在服务器上运行该servlet,但没有创建PDF文档 此代码作为独立应用程序工作: 此代码不起作用: 请阅读文档。比如这个问题的答案 您当前正在文件系统上创建文件。您没有使用响应对象,这意味
public static void main(String[] args){
...
...
...
}
文档创建正确
但是我需要从Servlet创建一个PDF文档。我将代码粘贴到get或post方法中,在服务器上运行该servlet,但没有创建PDF文档
此代码作为独立应用程序工作:
此代码不起作用:
请阅读文档。比如这个问题的答案 您当前正在文件系统上创建文件。您没有使用
响应
对象,这意味着您没有向浏览器发送任何字节。这就解释了为什么浏览器中什么都不发生
这是一个简单的例子:
public class Hello extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/pdf");
try {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, response.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());
}
}
}
但是,当您像这样直接发送字节时,某些浏览器会遇到问题。使用ByteArrayOutputStream
在内存中创建文件更安全,并告诉浏览器内容头中的预期字节数:
public class PdfServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// Get the text that will be added to the PDF
String text = request.getParameter("text");
if (text == null || text.trim().length() == 0) {
text = "You didn't enter any text.";
}
// step 1
Document document = new Document();
// step 2
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
// step 3
document.open();
// step 4
document.add(new Paragraph(String.format(
"You have submitted the following text using the %s method:",
request.getMethod())));
document.add(new Paragraph(text));
// step 5
document.close();
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());
// write ByteArrayOutputStream to the ServletOutputStream
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
}
catch(DocumentException e) {
throw new IOException(e.getMessage());
}
}
}
有关完整的源代码,请参阅。您可以在此处尝试以下代码:
你在评论中写道
此演示将PDF文件写入浏览器。我想将PDF保存在硬盘上
这个问题可以用两种不同的方式来解释:
response.setHeader(“内容处置”、“附件;文件名=“+”testPDF.pdf”)
如果希望在浏览器中打开PDF,可以将内容配置设置为内联,但在问题中,内容配置设置为附件,从而触发打开对话框
另请参见将代码添加为文本,而不是屏幕快照什么意思是
不起作用
?有什么例外吗?你有没有试过把它关闭再打开?首先选择技术:这是一个PdfBox问题,或者是一个iText问题。您正在使用PdfBox代码,因此您的问题不应标记为iText问题。请注意,iText代码通常比PdfBox代码更容易阅读。尽管如此,我还是尽力阅读了你的PdfBox代码。我看到您在文件系统上创建了一个文件。你为什么这么做?您不向响应
对象发送任何内容。这意味着您的代码不应该工作,您也不应该对它不工作感到惊讶。我的意思是代码没有保存在磁盘上。我想将pdf保存在硬盘上。此演示将pdf写入浏览器。我希望将pdf保存在硬盘上。我将更新我的答案,因为您的问题可能会有不同的解释。(你需要学习如何更准确地提问。)是的,我需要一个标准的对话框,询问用户他想要什么,在浏览器中显示PDF或保存到文件系统(zip)。非常感谢,它成功了。但我还不知道如何将pdf转换为zip并获取标准对话框,该对话框会询问用户希望在浏览器中显示pdf还是以zip格式保存到文件系统?没有标准对话框可以做到这一点。在发送响应之前,您需要知道用户想要什么。这需要心灵感应或魔法。请以更科学的方式提问。例如:发布一个新问题,询问如何提供包含PDF文档的ZIP文件。不要使用评论部分发布后续问题。如果你不接受正确的答案,不要期望得到答案。