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?
解决方案
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.