我正在使用Spring MVC.我必须编写一个服务,它将从请求体中获取输入,将数据添加到pdf并将pdf文件返回给浏览器.使用itextpdf生成pdf文档.我怎么能用Spring MVC做到这一点.我试过用这个
@RequestMapping(value="/getpdf", method=RequestMethod.POST)
public Document getPDF(HttpServletRequest request , HttpServletResponse response,
@RequestBody String json) throws Exception {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment:filename=report.pdf");
OutputStream out = response.getOutputStream();
Document doc = PdfUtil.showHelp(emp);
return doc;
}
Run Code Online (Sandbox Code Playgroud)
showhelp生成pdf的函数.我暂时将一些随机数据放在pdf中.
public static Document showHelp(Employee emp) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("C:/tmp/report.pdf"));
document.open();
document.add(new Paragraph("table"));
document.add(new Paragraph(new Date().toString()));
PdfPTable table=new PdfPTable(2);
PdfPCell cell = new PdfPCell (new Paragraph ("table"));
cell.setColspan (2);
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setPadding (10.0f);
cell.setBackgroundColor (new BaseColor (140, 221, 8));
table.addCell(cell);
ArrayList<String[]> …
Run Code Online (Sandbox Code Playgroud) 我想用iText做以下事情:
(1)解析现有的PDF文件
(2)在文档的现有单页上添加一些数据(例如时间戳)
(3)写出文件
我似乎无法弄清楚如何用iText做到这一点.在伪代码中,我会这样做:
Document document = reader.read(input);
document.add(new Paragraph("my timestamp"));
writer.write(document, output);
Run Code Online (Sandbox Code Playgroud)
但由于某些原因,iText的API非常复杂,我无法绕过它.PdfReader实际上保存文档模型或其他东西(而不是吐出文档),你需要一个PdfWriter来读取它的页面......呃?
我知道之前已经问过这个问题,但是我还没有确定哪个PDF生成框架可用于我当前的项目.
我的要求
很多人似乎都在使用iText,但我对关注点分离有一些担忧(除了更改的许可证):在HTML上下文中有很好的MVC支持,我通常坚持使用Spring MVC和FreeMarker来分离逻辑和布局.我有点担心,使用iText,你最终会混合代码和布局.
我知道,Apache FOP在这里可能是一个解决方案,但是我再次发现XSLT很繁琐,而且我读到FOP在许多文档的大量组合中可能会很慢?
我也考虑过JasperReports,但根据我的理解,这更适合包含表格数据集的报告而不是单个文档,例如需要大量布局格式的发票?
有什么想法吗?
有谁知道是否可以使用iText将HTML页面(url)转换为PDF?
如果答案是'不',那也没关系,因为我将不再浪费时间试图解决这个问题而只是在我认识的许多组件中花费一些钱:)
在此先感谢您的回复!
有人提出了这方面的问题,但最近并没有这方面的问
要求:
假设的选项是iText,PDFBox,FOP,还有其他什么?基于上述要求的建议是什么?
我刚开始使用iText(5.4.2,最新版本),有两件事我还没有完成.
我有下面的代码.
Document d = new Document(PageSize.A4_LANDSCAPE,0,0,0,0);
PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream("C:/test.pdf"));
document.open();
document.newPage();
Image img = Image.getInstance(String.format("C:/file.png"));
img.scaleToFit(400,240);
document.left(100f);
document.top(150f);
document.add(img);
document.close();
Run Code Online (Sandbox Code Playgroud)
但页面以纵向(非横向)渲染,图像放在左上角(根据要求不是10和15个单位).我究竟做错了什么?
itext ×10
java ×6
pdf ×5
apache-fop ×2
asp.net ×1
c# ×1
html ×1
response ×1
spring ×1
spring-mvc ×1