FOP Auto-Detecting Fonts

This section provides a tutorial example on how to configure Apache FOP to auto detect fonts from Windows system.

Earlier in the book, we learned that XSL processors are required to map XSL-FO generic fonts to specific fonts based output devices. In this tutorial, let's take a look at how Apache FOP does this for PDF output files:

1. By default, FOP only supports 3 XSL-FO generic fonts: sans-serif, serif and monospace. They are automatically mapped to Adobe Base-14 fonts: Helvetica Times and Courier.

2. FOP does support font definitions through a configuration file. But you have to create one and invoke it explicitly. A sample of FOP configuration file is located at \fop\conf\fop.xconf. The FOP command to invoke a configuration file looks like this:

C:\herong>\fop\fop -fo abc.fo -pdf abc.pdf -c \fop\conf\fop.xconf

3. FOP can be configured to auto-detect fonts from Windows system, by adding the following setting to the fop.xconf file:

<fop ...>
 <renderers>
  <renderer mime="application/pdf">
   <fonts>
    <auto-detect/>
   </fonts>
  </renderer>
  ...
 </renderers>
 ...
</fop>

The above setting tells FOP to auto-detect fonts from Windows system.

We can verify this with the following tutorial example, Apache-FOP-Auto-Detect-Fonts.fo, to verify the configuration:

<?xml version="1.0" encoding="utf-8"?>
<!-- Apache-FOP-Auto-Detect-Fonts.fo
 - Copyright (c) 2016, HerongYang.com, All Rights Reserved.
-->
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <fo:layout-master-set>
  <fo:simple-page-master master-name="page" 
   margin="0.1in" page-height="4.4in" page-width="3.4in">
   <fo:region-body region-name="body"/>
  </fo:simple-page-master>
 </fo:layout-master-set>
 <fo:page-sequence master-reference="page">
  <fo:flow flow-name="body" font-size="12pt">
<fo:block margin-top="6pt">sans-serif font:
 <fo:inline background-color="#ffddff" 
  font-family="sans-serif" font-weight="bold">
The tangram is a dissection puzzle consisting of seven flat shapes, 
called tans, which are put together to form shapes.
 </fo:inline>
</fo:block>
<fo:block margin-top="6pt">Tahoma font:
 <fo:inline background-color="#ffddff" 
  font-family="Tahoma" font-weight="bold">
The tangram is a dissection puzzle consisting of seven flat shapes, 
called tans, which are put together to form shapes.
 </fo:inline>
</fo:block>
  </fo:flow>
 </fo:page-sequence>
</fo:root>

If you process this example XSL-FO document with Apache FOP without the configuration file and with the configuration file, you will see the difference:

C:\herong>\local\fop\fop -fo Apache-FOP-Auto-Detect-Fonts.fo
   -pdf Apache-FOP-Auto-Detect-Fonts_fop.pdf
   
... org.apache.fop.events.LoggingEventListener processEvent
WARNING: Font "Tahoma,normal,700" not found. 
Substituting with "any,normal,700".

... org.apache.fop.events.LoggingEventListener processEvent
INFO: Rendered page #1.


C:\herong>\local\fop\fop -fo Apache-FOP-Auto-Detect-Fonts.fo
   -pdf Apache-FOP-Auto-Detect-Fonts_fop.pdf
   -c \fop\conf\fop.xconf

... org.apache.fop.events.LoggingEventListener processEvent
INFO: Rendered page #1.

As you can see, without the configuration file, FOP returns an error, because it can not find the Tahoma font. With the configuration file, FOP generates the PDF file with no problem, because it auto detected the Tahoma font on the Windows system.

If you open the PDF output file, Apache-FOP-Auto-Detect-Fonts_fop.pdf, in Adobe Acrobat Reader, you will see two blocks of text displayed in two fonts. Tahoma font is obviously thicker (or bolder) than sans-serif.

If you click File > Properties > Fonts, you will see 3 fonts are used in this PDF file:

The picture below shows you the font differences and the font property list in Arcobat Reader:

Apache FOP Auto-Detected Fonts

Last update: 2016.

Table of Contents

 About This XSL-FO Tutorial Book

 Introduction of XSL-FO

 Apache™ FOP (Formatting Objects Processor)

 RenderX XEP as an XSL-FO Tool

 Introduction of Area Model

 XSL-FO Document Basics and Examples

 Page Layout Masters

 Block-Level Formatting Objects

 Inline-Level Formatting Objects

 Including Graphics in XSL-FO document

 Table of Rows and Columns

 List, Item, Label, and Body

 Floating Blocks - "float" and "footnote"

 Hyperlinks, Table of Contents and Indexes

 Headers and Footers using "static-content"

 Font Attributes and Font Families

Apache FOP Font Configurations

FOP Auto-Detecting Fonts

 Using SimHei Font for Chinese Characters

 RenderX XEP Font Configurations

 References

 PDF Printing Version