在尝试使用Apache FOP
使用自定义字体(Google Lato字体)时,我遇到了错误。我已将ttf font
转换为xml
根据文档&保持ttf和amp;同一目录resource
警告:
Font "Lato,normal,700" not found
。用“any,normal,700”代替 警告:Font "Lato,normal,400" not found
。用“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();
}
}
有什么建议吗?我浪费了一整天的时间在Google上,但无法找到相关的解决方案。我已经看到stackoverflow帖子提到使用实际/绝对路径,子字体等,但它们似乎都没有工作
FOP 2.2
Would use this code on web for dynamic pdf generation
JDK 1.8
答案 0 :(得分:0)
跟随@Eduard方法&amp;使用独立FOP(命令行)和执行代码执行代码弄清楚我的执行代码没有读取<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
文件。
更改了执行代码,将其作为文件读取,而不是&amp;有效。上面的执行代码被编辑&amp;工作良好。
FOP 2.2中不需要进行XML转换