开发者

RTF to PDF in Java

开发者 https://www.devze.com 2023-01-14 12:38 出处:网络
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

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 rtfPars开发者_开发技巧er = 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消