views:

4104

answers:

6

How can I programmatically convert RTF documents to PDF?

+2  A: 

Use PDFCreator, a free pdf printer. Just print to pdf. You can control this through COM. Example code is in the COM folder of the install directory.

jle
This will, of course, only work on Windows.
vladr
+2  A: 

PDFCreator for windows is the easiest for single documents.

It's also possible to automate PDF creation for large sets of documents by converting them to XML and using XSLT and XSL-FO. There are lots of tutorials for this out there.

For a specific language, such as python, libraries exist to output to PDF fairly trivially.

The only advantage of XML over other simpler solutions is extensibility. You could also programmatically output your document in RTF, HTML, TXT, or just about any other text format.

Jweede
This will, of course, only work on Windows. Unfortunately it has not yet been made clear what platform the solution is required for.
vladr
naturally. Although the XSLT solution will work for just about any platform.
Jweede
+7  A: 

OpenOffice.org can be run in server mode (i.e. without any GUI), can read RTF files and can output PDF files.

Joachim Sauer
This is cross-platform, but unsuitable for desktop applications. Very suitable for running behind a web-server, though.
vladr
That's true. Since I dabble mostly in server-side Java "cross-platform" is a lot more important for me than "suitable for a desktop app" ;-)
Joachim Sauer
+5  A: 

You have a number of options depending on:

  • the platform(s) your application will be running on
  • whether your application will be a server application (e.g. a web service that you set up once and then it runs), or a widely-available desktop application (e.g. something that must be easily downloadable and installable by many people)
  • whether you are willing to put little or more programming effort into getting the solution to work
  • whether you are flexible as to the programming language you will use

Here are some options:

  1. PDFCreator + COM
    • Windows only
    • suitable for both desktop and server applications
    • medium programming effort
    • any language that allows you to speak COM
  2. OpenOffice ( + JODConverter - optional )
    • Cross-platform (Windows, Linux, etc.)
    • suitable for server applications, as OpenOffice is a 100MB+ download
    • low programming effort
    • Java (if using JODConverter), or any language that can interface with OpenOffice's UNO
  3. IText + Apache POI
    • Cross-platform (Windows, Linux, etc.)
    • suitable for both desktop and server applications
    • high programming effort
    • Java


EDIT

Here is an older post that has some commonality with your question.


EDIT 2

I see from your comments that you are on Linux and open to either C++ or Java. Definitely use option 2.

  • JODConverter (Java): the library takes care of spawning OpenOffice in headless mode and talking Uno to it on your behalf. You provide JODConverter with an input and output file name as well as the input and output types (e.g. rtf and pdf), and when it returns to you the output file is ready.
  • C++: you can fork+exec one (or more, for load balancing) OpenOffice instances in headless mode (soffice will listen for UNO requests on a socket e.g. port 8100.) From your application use Uno/CPP to instruct OpenOffice to perform the conversion the same way JODConverter does (see the JODConverter source code for how to do this.)

/opt/openoffice.org3/program/soffice.bin \
-accept=socket,host=127.0.0.1,port=8100;urp; \
-headless -nocrashreport -nodefault \
-nolockcheck -nologo -norestore

I am successfully using JODConverter from a Java app to convert miscellaneous document types (some documents dynamically generated from templates) to pdf.

vladr
how to i combine iText with poi to convert existent rtf ( with text allready inside with design )
You may want to ask a new question specific to combining the two. As I said, programming effort is high. I know that it has been done, see chsrinivas123's post on http://www.experts-exchange.com/Software/Server_Software/Application_Servers/Java/Q_23023116.html.
vladr
A: 

PrimoPDF. It acts as a virtual printer, so you just print to it, and out pops a PDF.

Steve Melnikoff
A: 

Look at PDF Printer