[英]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() 時,我也收到此消息
沒有值與 file:/path/to/conf.xml:1:20 處的配置元素“fop”相關聯
我也嘗試將配置重命名為 .xconf,但這並沒有改變任何東西。
這適用於 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);
試試這個......它對我有用:
FopFactory fopFactory;
fopFactory = FopFactory.newInstance();
File configFile = new File("path/to/config.xml");
fopFactory.setUserConfig(configFile);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.