Working with Docbook on Windows
Setting up a toolchain for working with Docbook on Windows often requires setting things up using Cygwin. Many people are just simply not willing to do that. This tutorial will show you how to set up a native environment to work with Docbook, and show you how to make CHM and PDF files on Windows.
Thanks to http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/docbooksys/segmentedhtml/ch03s03.html#DocBookSys-Chapter3-XML-Install-libxml-Windows for much of this information.
Getting the tools
The tools you need to work with Docbook XML and XSTL are all available on Windows. The first thing you need to do is visit http://www.zlatkovic.com/pub/libxml/ (new window) and grab the latest versions of
- libxml2
- libxslt
- iconv
Download each and unzip the contents of the
iconv.exe libexslt.dll libxml2.dll libxslt.dll xmlcatalog.exe xmllint.exe xsltproc.exe
If you feel better about putting these in their own folder, that’s fine as long as you add the new folder to your path.
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 c:\ drive and then rename the extracted folder to c:\docbook-xsl
Generating PDFs
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. You’ll need to have a JRE (Java Runtime) installed though. Visit http://java.sun.com/ for that.
Get FOP to build PDFs
Download FOP at http://www.uniontransit.com/apache/xmlgraphics/fop/fop-0.94-bin-jdk1.4.zip and unzip it to a temp location. Copy all .jar files in build/ and lib/ to your Java installation’s lib/ext folder. On my system it’s C:\Program Files\Java\jre1.5.0_11\lib\ext. Your system will differ depending on your installed version of Java.
Next, download OFFO-hyphenation 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 the same folder as the FOP files.
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 create 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 the 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 file 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 c:/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"
`del #{file}.fo`
puts "Done"
Now, build your book:
ruby make book
Creating a Help File
Generating a Windows HTML Help file (CHM) is pretty similar to the way you make a PDF. You first need to make the HLP file using xsltproc, and then you use a commandline tool to build the CHM.
Grab a copy of Microsoft’s HTML Help Workshop here and install it. Open a command prompt and copy the hhc file to the c:\windows directory so that the file is on your path.
copy "c:Program FilesHTML Help Workshop"hhc.exe c:windows
Next, we can use Ruby to make a file to create another build file. Create a file called “make_chm” in your project folder.
file = ARGV[0]
cmd1 = "xsltproc --xinclude c:/docbook-xsl/htmlhelp/htmlhelp.xsl #{file}.xml"
cmd2 = "hhc htmlhelp.hhp"
puts "Building HLP temporary files"
`#{cmd1}`
puts "Building CHM"
`#{cmd2}`
puts "Cleaning up"
`rename htmlhelp.chm #{file}.chm`
`del *.hhp`
`del *.hhc`
`del *.html`
puts "Done"
Summary
Docbook is a really great way to create books, tutorials, and documentation in a format that can be transformed into various other formats. It’s extremly easy to work with in Windows too!
