【发布时间】:2019-11-25 17:05:45
【问题描述】:
我正在使用 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()
时,我也会收到此消息file:/path/to/conf.xml:1:20 中的配置元素“fop”没有值关联
我也尝试将配置重命名为 .xconf,但这并没有改变任何东西。
【问题讨论】:
标签: apache pdf svg apache-fop