Some notes on font embedding using FOP's PDF Transcoder.
Here's the Java code to convert an SVG file to a PDF:
//Create transcoder
Transcoder transcoder = new PDFTranscoder();
//Configure the transcoder
try {
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.buildFromFile(new File("pdf-renderer-cfg.xml"));
ContainerUtil.configure(transcoder, cfg);
} catch (Exception e) {
throw new TranscoderException(e);
}
final int dpi = 300;
transcoder.addTranscodingHint(ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER,
new Float((float)(25.4 / dpi)));
transcoder.addTranscodingHint(XMLAbstractTranscoder.KEY_XML_PARSER_VALIDATING, Boolean.FALSE);
transcoder.addTranscodingHint(PDFTranscoder.KEY_STROKE_TEXT, Boolean.FALSE);
//Setup input
InputStream in = new java.io.FileInputStream(svg);
try {
TranscoderInput input = new TranscoderInput(in);
input.setURI(svg.toURL().toString());
//Setup output
OutputStream out = new java.io.FileOutputStream(pdf);
//Buffer the OutputStream for better performance
out = new java.io.BufferedOutputStream(out);
try {
TranscoderOutput output = new TranscoderOutput(out);
//Do the transformation
transcoder.transcode(input, output);
} finally {
out.close();
}
} finally {
in.close();
}
The {{pdf-renderer-cfg.xml}} file referenced in the code looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<pdf-renderer>
<fonts>
<font metrics-url="file:C:/Dev/FOP/temp/fonts/FUTURAL.ttf.xml" kerning="no" embed-url="file:C:/Dev/FOP/Temp/fonts/FUTURAL.ttf">
<font-triplet name="Futura Lt BT" style="normal" weight="normal"/>
</font>
</fonts>
</pdf-renderer>
The full source code of this example can be downloaded from
http://cvs.apache.org/~jeremias/PDFTranscoderTrueTypeEmbedding.zip
To run the example put the fop-transcoder-allinone.jar and batik.jar from FOP's lib directory (from CVS HEAD) where you extract the ZIP file and run doit.bat (on Windows).
Note: If FOP can use the embedded font to paint text depends on a number of variables. Here's a number of reasons why FOP can't paint text as text but will delegate to Batik to paint the text as shapes in which case the font needs to be available from your operating system:
font size too big (PDF restriction)
special writing modes
word and letter spacing
text on paths
special effects on fonts
and a few other things