Blog-like notes

Using TTF fonts with FOP

Apache™ FOP (Formatting Objects Processor) is, I believe, the best available tool to generate PDF output from XML files (especially DocBook documents, when used with the DocBook XSL stylesheets. However, its default configuration doesn’t use the best-looking fonts, and so the resulting output is not really nice to read.

FOP can make use of any TrueType font available on the system, but a little work is needed: for each TrueType font, one must ① generate a FOP metrics file, and ② add an appropriate stanza in FOP’s configuration file to make him aware of that font. FOP comes with a program called TTFReader (in the org.apache.fop.fonts.apps.TTFReader class) to generate a FOP metrics file from a TTF file, but the second step is left to the user, and the whole process can be quite tiresome.

To facilitate the use of TrueType fonts with FOP, I wrote a small shell script ttf2fop that takes care of calling TTFReader and generate the required lines to insert into FOP’s configuration file. Calling that script with a list of TTF files will automatically generate all the corresponding FOP metrics files and print to standard output the configuration stanza.

Here is the script 1: (text/x-shellscript, 2.8K)

For example, to make all the DeJaVu fonts available to FOP:

$ ttf2fop /usr/share/fonts/DeJaVu*.ttf > dejavu.xml

That command would generate all the metrics files in /usr/share/fop/fonts/ (you can use the -d flag to install them elsewhere, if you don’t have write access to that directory), and a XML configuration fragment in the dejavu.xml file. Insert the contents of that file in FOP’s configuratin file, in the fop/renderers/renderer[mime="application/pdf"]/fonts node. (If you don’t have a FOP configuration file already, use the default file conf/fop.xconf bundled inside the FOP’s archive.)

To use the newly available fonts when processing a DocBook document, either use command-line parameters when calling your XSLT processor, or use a customization layer such as the following:

<?xml version="1.0" encoding="utf-8"?>
  <xsl:stylesheet xmlns:xsl=""

  <xsl:import href="" />

  <xsl:param name="">DejaVuSerifCondensed</xsl:param>
  <xsl:param name="">DejaVuSansCondensed</xsl:param>
  <xsl:param name="">DejaVuSansMono</xsl:param>

  1. For Slackware users, I provide a SlackBuild for FOP which already includes that script.