Apache FOP - 不接受自定义字体

标签 apache fonts apache-fop

在尝试使用 Apache FOP 来使用自定义字体(Google Lato 字体)时,我收到以下错误。我已按照文档将 ttf 字体 转换为 xml 并将 ttf 和 xml 文件保留在同一目录resource

WARNING: Font "Lato,normal,700" not found. Substituting with "any,normal,700"
WARNING: Font "Lato,normal,400" not found. Substituting with "any,normal,400".

配置文件:

<?xml version="1.0"?>
<fop version="1.0">
    <base>.</base>
    <source-resolution>72</source-resolution>
    <target-resolution>72</target-resolution>
    <default-page-settings height="11.00in" width="8.50in"/>
    <renderers>
        <renderer mime="application/pdf">
            <filterList>
                <!-- provides compression using zlib flate (default is on) -->
                <value>flate</value>
            </filterList>
            <fonts>
                <font metrics-url="Lato-Regular.xml" kerning="yes" embed-url="Lato-Regular.ttf">
                    <font-triplet name="Lato" style="normal" weight="400"/>
                </font>
                <font metrics-url="Lato-Bold.xml" kerning="yes" embed-url="Lato-Bold.ttf">
                    <font-triplet name="Lato" style="normal" weight="700"/>
                </font>
            </fonts>
        </renderer>
    </renderers>
</fop>

执行代码:

public static void main(String[] args) throws SAXException, IOException, TransformerException, URISyntaxException {
        File fopConf = new File("\\fop.xconf");
        FopFactory fopFactory = FopFactory.newInstance(fopConf);

        OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("D:/Hello.pdf")));

        try {
            // Step 3: Construct fop with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

            TransformerFactory factory = TransformerFactory.newInstance();
            Source xslt = new StreamSource(new File(ClassLoader.getSystemResource("resources/foo.xsl").toURI()));
            Transformer transformer = factory.newTransformer(xslt);

            // Resulting SAX events (the generated FO) must be piped through to FOP
            Result res = new SAXResult(fop.getDefaultHandler());

            Source src = new StreamSource(new File(ClassLoader.getSystemResource("resources/name.xml").toURI()));
            // Step 6: Start XSLT transformation and FOP processing

            transformer.transform(src, res);

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

有什么建议吗?我浪费了一整天的时间在谷歌上寻找,但找不到相关的解决方案。我见过 stackoverflow 帖子提到使用真实/绝对路径、子字体等,但它们似乎都不起作用

  • FOP 2.2
  • 将在网络上使用此代码来动态生成 pdf
  • JDK 1.8

最佳答案

遵循@Eduard方法并使用独立FOP(命令行)执行代码并发现我的执行代码没有读取conf文件。

  • 更改了执行代码以将其作为文件读取,并且它有效。上面的执行代码已编辑且工作正常。

  • FOP 2.2 中不需要 XML 转换

关于Apache FOP - 不接受自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50251219/

相关文章:

java - 为什么使用 Apache poi 读取时日期格式会发生变化?

python - plt.show() 和 fig.savefig() 的不同绘图大小

java - org.apache.xmlgraphics fop + org.lesscss lesscss 冲突

image-processing - 为什么imagemagick的字体列表是空的?

css - 连字图标如何在 Material Icons 中工作?

java - 在 Java 中使用 Apache FOP 的新字体

java - Apache FOP AFP 到 XML 的转换

apache - 从 LAN 连接到 tomcat :

apache - 构建容器Ubuntu 14.04并安装apache2时如何解决 "invoke-rc.d: policy-rc.d denied execution of start."?

apache - .htaccess 重定向添加哈希,同时保留查询字符串