gpt4 book ai didi

java - XSL-FO 无法在 Java 中添加字体

转载 作者:行者123 更新时间:2023-11-30 09:07:46 25 4
gpt4 key购买 nike

我正在尝试在我的 pdf 生成中使用字体“Frankling Gothic Medium”。
但我似乎无法将其放入 java/FOP 工厂。

我试过不设置自动配置。
但是现在我的配置看起来像这样:

“错误”:

2014-05-26 09:39:53.577  WARN 7048 --- [nio-8080-exec-1] org.apache.fop.apps.FOUserAgent          : Font "Franklin Gothic Medium,normal,400" not found. Substituting with "any,normal,400".
2014-05-26 09:39:53.623 WARN 7048 --- [nio-8080-exec-1] org.apache.fop.apps.FOUserAgent : Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,400".
2014-05-26 09:39:53.623 WARN 7048 --- [nio-8080-exec-1] org.apache.fop.apps.FOUserAgent : Font "ZapfDingbats,normal,700" not found. Substituting with "ZapfDingbats,normal,400".
2014-05-26 09:39:53.625 WARN 7048 --- [nio-8080-exec-1] org.apache.fop.apps.FOUserAgent : Font "Franklin Gothic Medium,normal,700" not found. Substituting with "any,normal,700".

XML:

<fop version="1.0">
<renderers>
<renderer mime="application/pdf">
<fonts>
<!-- automatically detect operating system installed fonts -->
<auto-detect />
<!-- register all the fonts found in a directory -->
<directory>C:\workspace\pathToTTF</directory>
</fonts>
</renderer>
</renderers>
</fop>

Java:

    DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
URL dir_url = ClassLoader.getSystemResource("dk/print/services/mycfg.xml");
Configuration cfg = cfgBuilder.buildFromFile(new File(dir_url.toURI()));
FopFactory fopFactory = FopFactory.newInstance();
FopFactoryConfigurator fopFacConf = new FopFactoryConfigurator(fopFactory);
fopFacConf.setUserConfig(cfg);
User user = userRepository.getUser();
String quoteXML = new PrintQuote(quote, user).asXML();

try {
Fop fop = fopFactory.newFop("application/pdf", out);
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(getClass().getResourceAsStream("quote.xsl"));
Transformer transformer = factory.newTransformer(xslt);

Source src = new StreamSource(IOUtils.toInputStream(quoteXML));

Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);

} finally {
// Clean-up
IOUtils.closeQuietly(out);
}

建议的 XML(翻译成我的字体):

<fop version="1.0">
<renderers>
<renderer mime="application/pdf">
<fonts>
<font embed-url="framd.ttf" kerning="yes" sub-font="Franklin Gothic">
<font-triplet name="Franklin Gothic Medium" style="normal"
weight="normal" />
</font>
<font embed-url="FRAHV.TTF" kerning="yes" sub-font="Franklin Gothic">
<font-triplet name="Franklin Gothic Heavy" style="normal"
weight="bold" />
</font>
</fonts>
</renderer>
</renderers>
</fop>

还是一样的错误:

2014-05-26 10:34:12.440  WARN 7224 --- [nio-8080-exec-1] org.apache.fop.apps.FOUserAgent          : Font "Franklin Gothic Medium,normal,400" not found. Substituting with "any,normal,400".
2014-05-26 10:34:12.440 WARN 7224 --- [nio-8080-exec-1] org.apache.fop.apps.FOUserAgent : Font "Franklin Gothic,normal,400" not found. Substituting with "any,normal,400".
2014-05-26 10:34:12.485 WARN 7224 --- [nio-8080-exec-1] org.apache.fop.apps.FOUserAgent : Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,400".
2014-05-26 10:34:12.487 WARN 7224 --- [nio-8080-exec-1] org.apache.fop.apps.FOUserAgent : Font "ZapfDingbats,normal,700" not found. Substituting with "ZapfDingbats,normal,400".
2014-05-26 10:34:12.489 WARN 7224 --- [nio-8080-exec-1] org.apache.fop.apps.FOUserAgent : Font "Franklin Gothic Medium,normal,700" not found. Substituting with "any,normal,700".
2014-05-26 10:34:12.489 WARN 7224 --- [nio-8080-exec-1] org.apache.fop.apps.FOUserAgent : Font "Franklin Gothic,normal,700" not found. Substituting with "any,normal,700".

XSL 定义示例:

安装 拿督

最佳答案

方法是添加完整的 URL,在本例中是 windows 路径。以下删除了 Font "Symbol,normal,700"not found. 错误:

<fonts>
<font embed-url="C:\Windows\Fonts\symbol.ttf" kerning="yes" sub-font="Symbol">
<font-triplet name="Symbol" style="normal" weight="normal"/>
<font-triplet name="Symbol" style="normal" weight="bold"/>
</font>
</fonts>

还要确保 fop 正在读取您的 配置文件,而不是模板文件。

fop -c fop.xconf -fo simple.fo -pdf simple.pdf

关于java - XSL-FO 无法在 Java 中添加字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23865287/

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