我正在使用Apache FOP将SVG渲染为PDF。这个SVG有一个特殊的字体,我想在结果PDF中使用。
我正在使用如下所示的配置文件:
<?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>
<value>flate</value>
</filterList>
<fonts>
<font
metrics-url="path/to/metrics.xml"
kerning="yes"
embed-url="path/to/font.ttf">
<font-triplet name="font name" style="normal" weight="normal" />
</font>
</fonts>
</renderer>
</renderers>
</fop>
我从示例配置中复制并粘贴了哪些内容,并删除了我不需要的所有渲染器。 现在我正在尝试使用这样的配置:
public void convertSVG2PDF(String svg, OutputStream out) throws IOException, TranscoderException {
// Create transcoder
AbstractFOPTranscoder transcoder = new PDFTranscoder();
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg;
try {
File configFile = new File("path/to/config.xml");
cfg = cfgBuilder.buildFromFile(configFile);
transcoder.configure(cfg);
System.err.println(cfg.getValue());
} catch (Exception e) {
System.err.println(e.getMessage());
}
// Setup input
Reader in = new java.io.StringReader(svg);
try {
TranscoderInput input = new TranscoderInput(in);
// Setup output
try {
TranscoderOutput output = new TranscoderOutput(out);
// Do the transformation
transcoder.transcode(input, output);
} finally {
out.close();
}
} finally {
in.close();
}
}
这会呈现完美的PDF,但不使用该字体。 当我尝试cfg.getValue()
时,我也收到此消息没有值与文件中的配置元素“fop”相关联:/ path/to/conf.xml:1:20
我也尝试将配置重命名为.xconf,但这并未改变任何内容。
答案 0 :(得分:0)
这适用于fop 2.3版本
File xconf = new File(this.fopConfigFile.toURI());
FopConfParser parser = null;
try {
//parsing configuration
parser = new FopConfParser(xconf);
} catch (SAXException e) {
throw new UncheckedIOException(new IOException(e));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
//building the factory with the user options
FopFactoryBuilder builder = parser.getFopFactoryBuilder();
this.fopFactory = builder.build();
Fop fop = this.fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);