Freitag, 22. Oktober 2010

Fop and Fonts for FO2PDF

During XSL-FO Transformation we faced some problems in getting all the Unicode characters displayed within the generated XML file. Instead of the Lambda and Delta characters a "#" character was displayed.

To solve this problem you need to provide a Configuration for your FopFactory. Usually you can use the fop.xconf file for that. By providing the tag <auto-detect/>, which automatically tries to resolve the fonts specified within the FO file (works for FOP 0.95, in previous versions you had to manually create the font file in order to embed it into the PDF).

fopFactory.setUserConfig(createAutoDetect());

    /**
     * Creates the <auto-detect/> swtich to enable auto creation
     * of specific fonts like Arial for particular Unicode characters
     * like lambda, delta.
     *
     * Part of fop.xconf which is generated here:
     * <?xml version="1.0"?>
     *    <fop version="1.0">
     *      <renderers>
     *        <renderer mime="application/pdf">
     *          <fonts>
     *            <auto-detect/>
     *          </fonts>
     *        </renderer>
     *      </renderers>
     *    </fop>
     *
     *
     * @return auto-detection enabled
     */
    private Configuration createAutoDetect() {
        DefaultConfiguration fop = new DefaultConfiguration("fop");
       
        DefaultConfiguration renderers = new DefaultConfiguration("renderers");
        fop.addChild(renderers);
       
        DefaultConfiguration renderer = new DefaultConfiguration("renderer");
        renderer.addAttribute("mime", "application/pdf");
        renderers.addChild(renderer);
       
        DefaultConfiguration fonts = new DefaultConfiguration("fonts");
        renderer.addChild(fonts);
       
        DefaultConfiguration autoDetect = new DefaultConfiguration("auto-detect");
        fonts.addChild(autoDetect);
       
        return fop;
    }

Keine Kommentare:

Kommentar veröffentlichen