Working with Docbook on the Mac
Docbook allows you to prepare documentation by using XML markup. You can create PDFs or HTML exports of your work, and it’s really nice for collaborating with others, as you can work in a code repository easily. In this article, you’ll learn how to build a PDF using Docbook XSLT.
Getting the Stylesheets
In order to build a book, you need to have the XSLT stylesheets so you can transform your XML into a pretty-looking book with a table of contents and nicely formatted text.
Download the docbook-xml-ns files from sourceforge: http://sourceforge.net/project/showfiles.php?group_id=21935
Unpack to your home directory and rename the folder to ~/docbook-xsl
Generating PDFs with Apache FOP
In order to create a PDF, you have to first convert to the FO format and then use a Java library to convert the FO to a PDF. Apache FOP does this for you.
Get FOP. - http://www.uniontransit.com/apache/xmlgraphics/fop/fop-0.94-bin-jdk1.4.zip
Unzip to temp location and copy all .jar files in the build/ and lib/ folders to ~/Library/Java/Extensions. Create that folder if it isn’t there for you already.
Finally, download OFFO from http://offo.sourceforge.net/index.html and grab the offo-hyphenation-fop-stable.zip file from the downloads page and put the jar files in ~/Library/Java/Extensions. This enables hyphenation support.
Building your first book
Create a project folder called “my_book” and create a new file called “book.xml” in this folder.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> <book> <bookinfo> <title>My Simple Book</title> </bookinfo> <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="chapter1.xml"/> </book>
Then creaet a chapter for your book. Create the file chapter1.xml in your project folder with this content:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> <chapter id="chapter1"> <title>Introduction</title> <para>This is just a simple book.</para> </chapter>
Notice that the chapter and book each have their own doctype, This is really important. Each chapter file needs to have this structure in order to work properly.
Generating HTML from the document
The easiest way to use Docbook is to export to HTML. Execute this command to create an HTML version of your book:
xsltproc --xinclude --output book.html c:/docbook/xsl/html/docbook.xsl book.xml
Creating a makefile to build a PDF
The PDF creation process is similar to the HTML process but it does require two steps. You need to first convert the document to the FO file format. Then you use FOP to convert it to the PDF. We can automate this by using Ruby.
Create a Ruby makefile in your project folder called “make”. You’ll use this file to build the PDF of your book.
file = ARGV[0]
cmd1 = "xsltproc --xinclude --output #{file}.fo ~/docbook-xsl/fo/docbook.xsl #{file}.xml "
cmd2 = "java org.apache.fop.cli.Main -fo #{file}.fo -pdf #{file}.pdf"
puts "Building FO file"
`#{cmd1}`
puts "Building PDF"
`#{cmd2}`
puts "Cleaning up"
`rm #{file}.fo`
puts "Done"
`open #{file}.pdf`
Now, build your book. In your project folder, type
ruby make book
Summary
Now you have a good introduction to how to work with the Docbook format. You may want to use a Textmate bundle to make editing the XML a little easier, but the syntax really isn’t that hard.
Up next… creating CHM files using Docbook on Windows.

Nice tutorial!
Having used Docbook for a few years now, I confirm that it is a very powerful solution to single-source documentation that remains simple enough.
I’d like to point our online collaboration service for Docbook documentation at http://www.livetechdocs.com. You can register for free and use it to upload, share, review documentation within your company.