views:

71

answers:

2
+2  Q: 

RTF to PDF in Java

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?

+1  A: 

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.

Thorbjørn Ravn Andersen
+2  A: 

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 covert several thousand rtf documents to pdf in the past.

Tao
This is perfect! works just I want it to work. However, have You maybe managed to convert from stream to stream? I can't get this one working but beyond that it works perfectly.
jjczopek