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

时间:2014-05-26 07:53:05

标签: java xml spring xslt xsl-fo

我正在尝试使用字体" Frankling Gothic Medium"在我的pdf一代。
但我似乎无法将其纳入java / FOP工厂。

我试过没有设置autoconfig 但是现在我的配置看起来像这样:

"错误":

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定义示例:

                                        安装                                         拿督                                     

2 个答案:

答案 0 :(得分:2)

这样做的方法是添加完整的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

答案 1 :(得分:0)

我的方式是

<renderer mime="application/pdf">
  <fonts>
    <font embed-url="MSMINCHO.TTF" kerning="yes" sub-font="MS Mincho">
    <font-triplet name="MS Mincho" style="normal" weight="normal"/>
    <font-triplet name="MS Mincho" style="normal" weight="bold"/>
    </font>
  </fonts>
</renderer>