友 我已经实现了一个jsp表单,它接受pdf文件的标题,描述和内容等输入。提交jsp表单时,在servlet的帮助下使用itext创建pdf,名为'pdfGenServlet.java'。
我的jsp表单是
<form action="pdfGenServlet" method="post" enctype="application/x-www-form-urlencoded">
<!-- input notes title-->
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" placeholder="Title of the notes" name="title">
</div>
</div>
<!-- input notes description-->
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" placeholder="Enter short description" name="description">
</div>
</div>
<div class="form-group">
<textarea name="content" id="myEditor"></textarea>
<div id="button-panel" class="panel panel-default">
<p>
<button type="submit" class="btn btn-primary "><span class="glyphicon glyphicon-plus"></span><strong> Create Note</strong></button>
<button class="btn btn-primary" type="reset"><strong>Reset</strong></button>
</p><!-- buttons -->
</div><!-- panel Button -->
</div>
</form>
servlet'pdfGenServlet.java'
@WebServlet("/pdfGenServlet")
public class pdfGenServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// Get the text that will be added to the PDF
request.setCharacterEncoding("UTF-8");
//Font for using with itext
Font bfBold18 = new Font(FontFamily.TIMES_ROMAN, 18, Font.BOLD, new BaseColor(0, 0, 0));
Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
String title = request.getParameter("title");
String description = request.getParameter("description");
String notes_content = request.getParameter("content");
Date date = new Date();
System.out.println(title);
System.out.println(description);
System.out.println(notes_content);
System.out.println(date.toString());
if (description == null || description.trim().length() == 0) {
description = "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(title));
document.addCreationDate();
document.add(new Paragraph("TITLE: ", bfBold18));
document.add(new Paragraph(title,bfBold12));
document.add(new Paragraph("\n"));
document.add(new Paragraph(String.format("Created on: " + date.toString())));
document.add(new Paragraph("DESCRIPTION: ", bfBold18));
document.add(new Paragraph("\n"));
document.add(new Paragraph(notes_content,bfBold12));
// 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 content length
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());
}
}
}
上面的servlet和表单(提交后)的输出是嵌入到我的页面中的chrome的pdf查看器,显示pdf但是当我点击右下角的pdfviewer中的保存按钮时。 pdf文件会保存,但显示失败。
另外正如您在内容'示例示例中看到的那样......'
标签来自froala编辑器应用的段落,即我的textarea输入。它不是应用段落,而是显示为“
和
”。请帮助。
如何将生成的pdf文件下载到目录中请帮助。
答案 0 :(得分:0)
要处理HTML标记,请使用以下库将HTML / CSS转换为PDF格式:
https://github.com/flyingsaucerproject/flyingsaucer
它使用了封面下的iText,可以省去以编程方式构建文档的麻烦。
要下载PDF而不是在浏览器中显示,请执行以下操作:
response.setHeader("Content-disposition", "attachment; filename=" + name + ".pdf");
答案 1 :(得分:0)
确定。因此,我能够使用完全正确的代码成功完成此操作,但需要进行这两项更改。
method="get"
放入jsp <form>
标记中
doPost
更改为doGet
它应该有效,因为它也适用于我。
告诉我它是否有效。
PDF文档确实显示在PdfViewer中也成功保存。