Apache FOP 嵌入式远程字体

Apache FOP embedded remote fonts

我正在使用嵌入在 Java 程序中的 Apache FOP v2.1。我正在尝试使用类似于以下内容的配置文件来检索远程字体:

<?xml version="1.0"?>
<fop version="1.0">
<renderers>
    <renderer mime="application/pdf">
        <fonts>
            <font kerning="yes"
                  embed-url="http://localhost:8000/MyFont.ttf" embedding-mode="subset">
                <font-triplet name="MyFont" style="normal" weight="normal"/>
            </font>
        </fonts>
    </renderer>
</renderers>
</fop>

但是,当我 运行 应用程序从 XML 和 XSL 文件生成 PDF 文件时,它给我一个错误:

java.lang.IllegalArgumentException: URI scheme is not "file"
at java.io.File.<init>(File.java:421)
at org.apache.fop.fonts.FontCache.addFont(FontCache.java:335)
at org.apache.fop.fonts.DefaultFontConfigurator.getFontInfo(DefaultFontConfigurator.java:173)
at org.apache.fop.fonts.DefaultFontConfigurator.addFonts(DefaultFontConfigurator.java:136)
at org.apache.fop.fonts.DefaultFontConfigurator.configure(DefaultFontConfigurator.java:89)
at org.apache.fop.render.PrintRendererConfigurator.getCustomFontCollection(PrintRendererConfigurator.java:147)
at org.apache.fop.render.PrintRendererConfigurator.setupFontInfo(PrintRendererConfigurator.java:127)
at org.apache.fop.render.intermediate.IFUtil.setupFonts(IFUtil.java:170)
at org.apache.fop.render.intermediate.IFRenderer.setupFontInfo(IFRenderer.java:187)
at org.apache.fop.area.RenderPagesModel.<init>(RenderPagesModel.java:75)
at org.apache.fop.area.AreaTreeHandler.setupModel(AreaTreeHandler.java:135)
at org.apache.fop.area.AreaTreeHandler.<init>(AreaTreeHandler.java:105)
at org.apache.fop.render.RendererFactory.createFOEventHandler(RendererFactory.java:350)
at org.apache.fop.fo.FOTreeBuilder.<init>(FOTreeBuilder.java:107)
at org.apache.fop.apps.Fop.createDefaultHandler(Fop.java:104)
at org.apache.fop.apps.Fop.<init>(Fop.java:78)
at org.apache.fop.apps.FOUserAgent.newFop(FOUserAgent.java:182)
at org.apache.fop.apps.FopFactory.newFop(FopFactory.java:240)
at com.example.myapp.myFunction(MyApp.java:123)

通过查看 FOP 的源代码,我发现 FOP 正在尝试将字体添加到缓存中。但是,因为字体 URI 是 HTTP URL,当它试图将 URL 传递给 java 文件对象时,它给我错误 "URI scheme is not file".

是否缺少任何加载远程字体的步骤?

问题出在 FontCache。它使用 File Java class 来读取内容,由于配置文件包含 URL 的 http(s) 协议,它会抛出上述异常。要解决此问题,您需要像这样禁用字体缓存:

// load the config file with fonts from remote resources (HTTP)
File configFileWithRemoteFontUrl = new File("fop-config.xconf");

// create a factory instance and disable the font cache
FopFactory factory = FopFactory.newInstance(configFileWithRemoteFontUrl);
factory.getFontManager().disableFontCache();

// your code as usual...

通过禁用字体缓存,您的远程字体无需额外配置即可正常加载。