gpt4 book ai didi

java - Apache FOP 嵌入远程字体

转载 作者:行者123 更新时间:2023-11-30 06:53:21 25 4
gpt4 key购买 nike

我正在使用嵌入在 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 File 对象时,它会给出错误“URI 方案不是文件”。

加载远程字体时我缺少任何步骤吗?

最佳答案

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

// 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...

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

关于java - Apache FOP 嵌入远程字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42301849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com