PDF viewers have a set of standard fonts like Times, Helvetica, Courier. It is guaranteeed, that PDFs with the fonts are shown correctly on any platform. It would be nice use them in full, but unfortunately there is no easy way to deal with the fonts metrics from Java. So for ISO-8859-1 charset we hardcoded the metrics tables into PD4ML, but for other characters (the rest of UNICODE space) it would be too bulky.
In order to output characters, not belong to ISO-8859-1 charset, PD4ML requires you to configure and to use TTF embedding feature. The feature is available in PD4ML Pro
The way TTF embedding is implemented by PD4ML may look overcomplicated 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 may easily instantiate Font object for any font face name and to use it for text output. But for PDF generation PD4ML needs an access not only to java.awt.Font
objects, but to the corresponding physical .ttf files (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
For that reason we introduced the font face -> font file
mapping appraoch (with pd4fonts.properties
The needed actions are trivial:
- create fonts/ directory (i.e /path/to/my/fonts/) and copy needed TTFs into it.
- run pd4font.properties generation command
java -jar pd4ml.jar -configure.fonts /path/to/my/fonts/
(as a result it should produce /path/to/my/fonts/pd4font.properties)
- reference /path/to/my/fonts/ directory from your Java/JSP/... code.
If you want to avoid binding to a local directory, you may pack the fonts/ directory into a JAR, place it to classpath and access them via classloader.http://pd4ml.com/examples.zip
(~2MB) contains chinese_ttf
sample, which illustrates how to do that.
Very often there is no necessity to support multiple font faces, but missing of special characters (like δ, « ...) or charsets (like Cyrillic, Arabic) support is critical.
For the case we created a "quick hack" solution with easy-to-use TTF embedding.
There is a JAR with 3 fonts for serif, sansserif and monospaced types (the fonts do not contain CJK glyphs): http://pd4ml.com/i/easyfonts/fonts.jar
Add the JAR to application's classpath (or put to WEB-INF/lib
in webapp scenarios), address the fonts via Java classloader and specify, that the 3 fonts should be used as defaults:
pd4ml.useTTF( "java:fonts", true );
pd4ml.setDefaultTTFs("Times New Roman", "Arial", "Courier New");
(Full Java API example: http://pd4ml.com/i/easyfonts/EasyFonts.java
<pd4ml:usettf from="java:fonts" serif="Times New Roman" sansserif="Arial" monospace="Courier New">
The same for the PHP wrapper (assuming that fonts.jar
is in the same dir where pd4ml(_demo).jar
passthru('java -Xmx512m -Djava.awt.headless=true -cp .:pd4ml_demo.jar:fonts.jar Pd4Php \'' .
$_POST['url'] . '\' 800 A4 -ttf java:fonts 2>>stderr.txt');
// Win32 version
// passthru('java -Xmx512m -cp .;pd4ml_demo.jar;fonts.jar Pd4Cmd ' .
// $_POST['url'] . ' 800 A4 -ttf fonts:jar');