在尝试使用 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/