PD4ML PRO, DMS and UA allow you to use all UNICODE characters space of custom TTF/OTF fonts in PDF.

The way TTF embedding is implemented by PD4ML may look complicated at first glance. On practice it is not so; also there are reasons why TTF usage is not as transparent as in regular Java applications.

In Java you can easily instantiate java.awt.Font object for any font face name, obtain the font metrics and to set the font for text output. By PDF generation PD4ML needs an access not only to java.awt.Font object, but to the corresponding physical .ttf file (to parse them and to extract a subset of used glyphs). Unfortunately Java does not offer a way to locate TTF file for a particular java.awt.Font object.

The most straightforward solution was to use font face -> font file mapping file. PD4ML’s default file name for it is pd4fonts.properties

New since v4.0.16: Interactive pd4fonts.properties creation tool: https://pd4ml.com/pd4ml-fonts-tool/

Below are available options how to create and deal with the mapping file or how to avoid a creation of it.

Creation of pd4fonts.properties for a selected set of fonts

  • create fonts/ directory (i.e /path/to/my/fonts/) and copy desired TTF files into it.
  • run pd4fonts.properties generation command
    java -Xmx512m -jar pd4ml.jar -configure.fonts /path/to/my/fonts/
    

As a result it should produce /path/to/my/fonts/pd4fonts.properties.

Now you can refer to it from Java application

pd4ml.useTTF("/path/to/my/fonts/");
// or identically
pd4ml.useTTF("/path/to/my/fonts/pd4fonts.properties");

Creation of pd4fonts.properties for system fonts

In the example above pd4fonts.properties file is stored to the same folder where TTF files are. If you run the command to index system fonts, in most of the cases it fails, as it has no write permission to the system font folder.

A solution is to write pd4fonts.properties to another location:

  • run pd4fonts.properties generation command
    java -Xmx512m -jar pd4ml.jar -configure.fonts c:/windows/fonts/ c:/path/to/my/config
    

As a result it should produce c:/path/to/my/config/pd4fonts.properties with an internal reference to the original font folder c:/windows/fonts/.

Now you may refer to it from Java application

pd4ml.useTTF("c:/path/to/my/config");
// or identically
pd4ml.useTTF("c:/path/to/my/config/pd4fonts.properties");

Creation of pd4fonts.properties on-a-fly

Set generateFontMappingFileIfMissing parameter of useTTF() to true

pd4ml.useTTF("/path/to/my/fonts/", true);

Creation of in-memory font mapping on-a-fly

Typically the method is used, when there is no preconfigured fonts directory available, and a use of the system fonts directory seems to be a good option. An obvious drawback of the idea is a potentially long indexing time of a big number of system fonts.

PD4ML allows to reduce the indexing efforts by limiting a scope of used fonts. fontFileNameFilter parameter can be set to a comma-delimited list of font name patterns:

pd4ml.useTTF("c:/windows/fonts/", "arial,times,courier");

The above code forces PD4ML to index only fonts, whose names contain arial, times or courier.

Creation of a JAR file with fonts

As a rule in Web application contexts you are not allowed to refer local file system resources. That makes the above methods not usable. PD4ML’s solution is to pack the fonts to a JAR file and deploy it with the Web application resources.

  • create fonts/ directory (i.e /path/to/my/fonts/) and copy desired TTF files into it.
  • run pd4fonts.properties generation command
    java -Xmx512m -jar pd4ml.jar -configure.fonts /path/to/my/fonts/
    

    which produces /path/to/my/fonts/pd4fonts.properties

  • pack to JAR
    jar cvf fonts.jar /path/to/my/fonts/
    

After deployment you can refer to it from Java application

pd4ml.useTTF("java:fonts/");

The “java:fonts/” URL addresses fonts/ folder within the JAR.

Web fonts

The @font-face CSS at-rule adds a custom font to a list of available ones; the font can be loaded from either a remote server or a locally-installed font on the user’s own computer.

The approach requires no API calls. All configuration is to be done in HTML/CSS sources.

@font-face {
  font-family: "Consolas";
  src: url("java:/html/rc/FiraMono-Regular.ttf") format("ttf");
}
@font-face { 
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/lato/v11/qIIYRU-oROkIk8vfvxw6QvesZW2xOQ-xsNqO47m55DA.woff) format('woff');
}

Font kerning

Kerning is an addition or reduction of space between two characters (glyphs) of a proportional font. As a rule a rendered text is visually much more pleasing when the kerning is applied.

Font kerning can be enabled with PD4ML API call:

pd4ml.applyKerning(true);

If you run PD4ML as a standalone command line tool, you may force it to apply kerning with -kerning parameter.