Differences between revisions 3 and 4
Revision 3 as of 2008-08-13 08:51:11
Size: 2914
Comment: Fixed URL
Revision 4 as of 2009-09-20 23:52:22
Size: 2914
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

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;
                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);
            //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 {
        } finally {

The pdf-renderer-cfg.xml file referenced in the code looks like this:

<?xml version="1.0" encoding="UTF-8"?>
    <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"/>

The full source code of this example can be downloaded from http://people.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

SvgNotes/PdfTranscoderTrueTypeEmbedding (last edited 2009-09-20 23:52:22 by localhost)