Java 中的 RTF 转 PDF
我们正在构建一个与其他系统部分交互的应用程序。我们正在从其他系统中提取一些数据,这些数据以 RTF 文档的形式返回。但我们必须阻止用户编辑这个文件,所以我们考虑用 iText 将其转换为 PDF。代码片段:
// moving the rtf data into input stream to be used in RTF parser
ByteArrayInputStream rtfInputStream = new ByteArrayInputStream(rtfStream.toByteArray());
// set headers
resp.setHeader("Cache-Control", "no-store");
resp.addHeader("Content-Type", "application/pdf");
resp.addHeader("Content-Disposition", "inline; filename=Karta.pdf");
resp.setStatus(HttpServletResponse.SC_OK);
// pdf output stream
ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();
Document pdfDoc = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(pdfDoc, pdfStream);
pdfDoc.open();
RtfParser rtfParser = new RtfParser(null);
rtfParser.convertRtfDocument(rtfInputStream, pdfDoc);
pdfDoc.close();
pdfWriter.close();
resp.getOutputStream().write(pdfStream.toByteArray());
rtfInputStream.close();
pdfStream.close();
is.close();
Pdf 已创建,但字体大小错误、样式错误且编码错误。也许您遇到了类似的问题并且您解决了一些问题?也许有更好的解决方案?
We are building an application which partially interacts with other system. We are pulling some data from the other system which is returned as RTF document. But we have to prevent users from editing this file, so we thought about converting it with iText into PDF. Code snippet:
// moving the rtf data into input stream to be used in RTF parser
ByteArrayInputStream rtfInputStream = new ByteArrayInputStream(rtfStream.toByteArray());
// set headers
resp.setHeader("Cache-Control", "no-store");
resp.addHeader("Content-Type", "application/pdf");
resp.addHeader("Content-Disposition", "inline; filename=Karta.pdf");
resp.setStatus(HttpServletResponse.SC_OK);
// pdf output stream
ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();
Document pdfDoc = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(pdfDoc, pdfStream);
pdfDoc.open();
RtfParser rtfParser = new RtfParser(null);
rtfParser.convertRtfDocument(rtfInputStream, pdfDoc);
pdfDoc.close();
pdfWriter.close();
resp.getOutputStream().write(pdfStream.toByteArray());
rtfInputStream.close();
pdfStream.close();
is.close();
Pdf is created but font sizes are wrong, styling is wrong and encoding is wrong. Maybe You had similar problems and You worked something out? Maybe there are better solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据此帖子,Itext 正在放弃 RTF 。
我使用过的一个很好的解决方案是JODCoverter Library。它利用 OpenOffice,我过去能够将数千个 RTF 文档转换为 PDF。
Itext is abandoning RTF according to this post.
One good solution I have used is JODCoverter Library. It leverages OpenOffice and I was able to convert several thousand RTF documents to PDF in the past.
考虑使用真正的文字处理器来生成 PDF。一种可能是 OpenOffice,它具有解决此类问题的 API - http://api.openoffice.org/ - 我会根据你的情况进行调查。
稍后可以使用其他开源软件来保护 PDF。
Consider using a real word processor to generate the PDF. One posibility could be OpenOffice which has an API for this kind of problems - http://api.openoffice.org/ - which I would look into in your situation.
PDF's can be protected later using other open source software.