嵌入式 Apache FOP,如何使用配置文件
Apache FOP embedded, how to use config file
我正在使用 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()
时也收到此消息
No value is associated with the configuration element "fop" at file:/path/to/conf.xml:1:20
我也尝试将配置重命名为 .xconf,但这并没有改变任何东西。
试试这个...它对我有用:
FopFactory fopFactory;
fopFactory = FopFactory.newInstance();
File configFile = new File("path/to/config.xml");
fopFactory.setUserConfig(configFile);
这适用于 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);
我正在使用 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()
时也收到此消息No value is associated with the configuration element "fop" at file:/path/to/conf.xml:1:20
我也尝试将配置重命名为 .xconf,但这并没有改变任何东西。
试试这个...它对我有用:
FopFactory fopFactory;
fopFactory = FopFactory.newInstance();
File configFile = new File("path/to/config.xml");
fopFactory.setUserConfig(configFile);
这适用于 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);