看来我的帖子为时已晚,但可能对其他人有用。[java 8, fop 2.1]
import lombok.SneakyThrows;
...
@SneakyThrows
private FopFactory getFopFactory(){
InputStream fopConfigStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("/fop/config.xml");
FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI(), new CustomPathResolver());
FopFactory factory = builder.setConfiguration(new DefaultConfigurationBuilder().build(fopConfigStream)).build();
fopConfigStream.close();
return factory;
}
...
private static final class CustomPathResolver implements ResourceResolver {
@Override
public OutputStream getOutputStream(URI uri) throws IOException {
return Thread.currentThread().getContextClassLoader().getResource(uri.toString()).openConnection()
.getOutputStream();
}
@Override
public Resource getResource(URI uri) throws IOException {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("fop/" + FilenameUtils.getName(uri));
return new Resource(inputStream);
}
}
配置.xml:
<fop version="1.0">
<renderers>
<renderer mime="application/pdf">
<fonts>
<font kerning="yes" embed-url="font.ttf" embedding-mode="subset">
<font-triplet name="Font name" style="normal" weight="normal"/>
</font>
</fonts>
</renderer>
</renderers>
</fop>