Generate HTML and PDF from DocBook in Fedora

DocBook is a widely-used format for writing documentation, articles, books, etc. For my purposes, I needed to generate XHTML and PDF files from documentation in DocBook format on a Fedora 16 server.


You need to install the following packages.

sudo yum install libxslt docbook5-style-xsl docbook-utils

Convert single DocBook file to XHTML

Now comes the conversion. Run xsltproc as below and it will create an HTML file (mybook.html in this case) in the current directory.

xsltproc -o mybook.html /usr/share/sgml/docbook/xsl-ns-stylesheets/xhtml-1_1/docbook.xsl mydocbook.xml

You can explore the /usr/share/sgml/docbook/xsl-ns-stylesheets/ path for more options.

Convert modular DocBook file to XHTML

You can create a modular DocBook document (a book in my case) by separating out chapters of the book into separate files and including them in the main file. For example, there’s only one chapter in my book so I’ll have two files: and docbook.chapter.xml. These two files would look something like the following:

An example of file

<?xml version="1.0" encoding="UTF-8"?>
<book xml:id="wikiply_doc" xmlns="" version="5.0" xmlns:xi="">
    <title>Sample Book</title>
            <para>Copyright 2011-2012 Code Ghar. All rights reserved.</para>
            <para>Redistribution and use in source (SGML DocBook) and 'compiled' forms (SGML, HTML, PDF, PostScript, RTF and so forth) with or without modification, are permitted.</para>
    <copyright><year>2012</year><holder>Code Ghar</holder></copyright>
    <xi:include href="docbook.chapter.xml" />

An example of file docbook.chapter.xml

<?xml version="1.0" encoding="UTF-8"?>
<chapter xml:id="installation" xmlns="" version="5.0" >
<title>Sample Chapter</title>
    <section xml:id="sample_chapter">
        <title>Sample Chapter</title>
        <para>This is example text in sample chapter</para>

Run xsltproc as below and it will create an HTML file (mybook.html in this case) in the current directory from both files.

xsltproc -xinclude -o mybook.html /usr/share/sgml/docbook/xsl-ns-stylesheets/xhtml-1_1/docbook.xsl

Note the use of the -xinclude flag in the command and the xi:include XML tag in the file. These two things make the magic of modular DocBook possible.

bash alias

Since I work with a DocBook book often, I have created a bash alias as below:

alias dbtohtml="xsltproc -xinclude -o /home/codeghar/book/mybook.html /usr/share/sgml/docbook/xsl-ns-stylesheets/xhtml-1_1/docbook.xsl /home/codeghar/book/; sed -e 's/</\n</g' -e 's/<meta name/\n<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/> \n <meta name/g' -i /home/codeghar/book/mybook.html"

The generated file does not have the HTML meta tag to identify it as UTF-8 and so space characters display as the character  in the web browser. Therefore, sed is used to enter the appropriate meta tag in the file.

Convert DocBook to PDF

Using the same example files ( and docbook.chapter.xml), we will create a PDF instead of an XHTML file.

You need to install Apache FOP.

sudo yum install fop

Next you need to create an intermediate file ( as below.

xsltproc -xinclude -o /usr/share/sgml/docbook/xsl-ns-stylesheets/fo/docbook.xsl

Finally, run the following command to create the PDF file:

fop -pdf mybook.pdf

Hat Tips

DocBook Ubuntu Documentation; How to generate pdf from docbook 5.0; Getting Started with Docbook Book Authoring on Ubuntu; Writing Documentation; Playing With DocBook 5.0


Comments are closed.

%d bloggers like this: