View Full Version : Converting Word docs, XML, and HTML to PDF

Aug 7, 2007, 08:48 AM

I am developing a cross platform (Windows+Mac) desktop app using C++. I'm looking for a tool to programmatically convert any sort of document (.doc, .txt, .rtf, .xml, .html) to a PDF format.

I have found many tools, but they are almost all Windows only. Does anybody know of any tool/API that will allow me to do this cross platform?

Aug 7, 2007, 09:45 AM
Well remember that OS X has PDF Support build in. I am sure you can make an Apple Script that opens Office (or text edit with some sucess), or Safari then have it do a print and there is a print option to PDF. I know you may be looking for a more direct C++ using some shared libs but the best way may just make an apple script (or have your C++ program generate the apple script) and execute it. C++ is not a good language for creating cross platform application. Finding cross platform libaries for converting these files to PDF will be very difficult because espectially .DOC format requires a fare amount of work to aproximate and Microsoft isn't helpful in releasing the specs. .xml and .html too even with the full specs are considered hard to render, just becasue of all the specs that need to be followed. The best you can probably do is use some pre-procesor definations and part of the make file say the code is either for OS X or Windows and do the different methods for both of them. Remember these are pre-processor definanitions so it essentionally doesn't compile the code if it isn't in the #IFDEF .

Aug 7, 2007, 09:47 AM
Yes, you'll find it in the PRINT function of OS X. Print, PDF, Save as PDF. It works flawlessly, and surely there is a way to access this function in an automated way.

Aug 7, 2007, 09:59 AM
Thanks for the responses, but I need a way to do this in a way such that it will work in both Windows and on a Mac without having to write platform specific code. I know that a lot of the tools out there need a copy of Word installed, which is fine by me. The tool doesn' have to ahve reverse engineered the .doc format :P

Any other suggestions?

Thanks again.

Aug 7, 2007, 10:18 AM
[QUOTE=ehayut;4011461]but I need a way to do this in a way such that it will work in both Windows and on a Mac without having to write platform specific code.[QUOTE]

That will be the hard part... OS X and Windows are actually very different OS's while a lot of code usually the logic can be cross compatible the easiest way of doing what you need to be done can't really be by cross platform programming, You will need to have a Windows way and an OS X way. The time that it will take you to do it both ways will be probably less then searching for a solution that will work both ways, wich may or may not work as well. If you do find a cross platform library it will probably just do the same thing having two different platform independant code segments and will choose wich segment to compile depending on what OS you are compiling on. This sounds like an ambishious project and I know you want to do it the best way idologically, and I can commend the effort. But sometimes you need to suck it up and and do it the platform independent route. You can still have both options in your code base and depending on the compiler you compile one version of you convert module vs the other one. It really isn't all that bad and it is done more often then not on cross platform apps.

Aug 7, 2007, 10:47 AM
You will probably want to write a separate Cocoa CLI app for the Mac that does the conversion, and another for Windows. Here's a tutorial to get you started: Create a PDF (http://cocoadevcentral.com/articles/000074.php)

Combine that with an NSTextView that displays an NSAttributedString (which can be made from RTF, Word, HTML, etc), should be pretty simple to create if you're familiar with Cocoa.

Edit: if you don't want to go down the Cocoa road, check out this article, which has sample C code:
PDF Document Creation, Viewing, and Transforming (http://developer.apple.com/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_pdf/chapter_14_section_1.html)

Aug 7, 2007, 12:25 PM
Thank you very much, you guys have been very helpful. I will look into all your recommendations.