Quick-and-Dirty CHM to PDF Conversion in OS X For Free

I recently had the need to convert a CHM (Compiled HTML Help) file to PDF. A quick search suggested that all of the conversion utilities for OS X available online were non-free, which didn’t make sense to me for a one-off situation. So here’s a free solution, which could be rolled into a simple app pretty easily:
1. Download the CHM Library source code from here. Decompress the files somewhere and take note of the location.

2. If you don’t have Xcode/Developer Tools installed, download and install a copy. If you don’t have the discs that originally came with your Mac and you have Snow Leopard or Lion you can get Xcode 4 for about $5 from the App Store. Alternatively, register for a free developer account and download Xcode 3 from Apple Connect.

3. Open Terminal and cd into the directory that you saved the CHM Lib files. Enter the following 2 commands in the shell:

./configure --prefix /usr/local/ --enable-examples
make; sudo make install

4. Once finished, while still in terminal, cd into the directory containing your CHM file(s). You’re now going to de-compile the CHM and extract the HTML within. It would probably be wise to create a new subdirectory to store the files by issuing the command

mkdir thenameofthedirectory

Then run this command (substituting as needed) to extract the HTML:

/usr/local/bin/extract_chmLib thetargetchmfile.chm thenameofthedirectory

5. Finally we need to convert the html files to PDF. Fortunately OS X has a built-in utility called convert that we can use for this purpose. Find the subdirectory that contains the actual html and issue the following commands (assuming the html files are all suffixed .HTM):

cat *.HTM >> all.html
/System/Library/Printers/Libraries/convert -f all.html -o myconvertedchm.pdf 

Note that depending on the number of pages (not simply html files, as each file could span many pdf pages) this could take a while.

Kudos to macgeekery.com and geekology.co.za for the details needed for the above.