【问题标题】:Apache FOP embedded, how to use config fileApache FOP 嵌入式,如何使用配置文件
【发布时间】: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


    【解决方案1】:

    这适用于 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);
    

    文档:https://xmlgraphics.apache.org/fop/2.3/upgrading.html

    【讨论】:

      【解决方案2】:

      试试这个...它对我有用:

      FopFactory fopFactory;
      fopFactory = FopFactory.newInstance();
      
      
      
      File configFile = new File("path/to/config.xml");
      fopFactory.setUserConfig(configFile);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-15
        • 1970-01-01
        • 2017-01-05
        • 2012-10-23
        • 2022-07-06
        • 2022-11-25
        • 1970-01-01
        • 2017-03-16
        相关资源
        最近更新 更多