RTF to PDF in Java
Asked Answered
H

2

5

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?

Horick answered 28/8, 2010 at 15:9 Comment(2)
PDF files can be edited, so the whole basis of your question is mistaken. You either need to explain what you are trying to accomplish or rethink how you are doing it.Seminal
pdf files can be edited with ease, but MOST users open them with a Reader only so they are relatively safe & trusted. maybe wrongly so.Nilgai
Y
5

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.

Yeung answered 28/8, 2010 at 17:14 Comment(2)
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.Horick
JODConverter has a dependency on OpenOffice. Are there other libraries available which does not require installation of any software? I.e we can bundle all libraries to gather without any external installation.Hagerman
P
2

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.

Perforate answered 28/8, 2010 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.