当前位置: 首页 > 知识库问答 >
问题:

Apache FOP embedded,如何使用配置文件

柏高洁
2023-03-14

我正在使用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()时,我也会收到这条消息

文件/path/to/conf.xml:1:20中没有与配置元素“fop”关联的值

我还尝试将配置重命名为。xconf,但这并没有改变什么。

共有2个答案

陆寒
2023-03-14

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

FopFactory fopFactory;
fopFactory = FopFactory.newInstance();



File configFile = new File("path/to/config.xml");
fopFactory.setUserConfig(configFile);
乌靖
2023-03-14

这适用于版本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

 类似资料:
  • 问题内容: 我有一个使用maven作为构建工具的应用程序。 我正在使用Maven配置文件从不同的配置文件设置不同的属性。 我想做的是将maven中的所有活动配置文件也移植到spring活动配置文件中,以便我可以在bean签名()中引用它们。但我不确定该怎么做。 例如:考虑以下Maven设置 假设我在未指定任何其他配置文件的情况下运行maven,而我希望spring具有和 配置为活动配置文件。 问题

  • 我有、和 我正在使用maven命令它不起作用,并且我无法使用选择开发配置文件。如何选择? 文档中说可以工作,我尝试了但没有工作。在我的项目中,我使用它运行,但是如果我使用选择dev配置文件,控制台会打印这么多日志并警告我从来没有看到使用过,并告诉我 那么如何解决两个问题呢?谢谢~

  • 我正在尝试将我的应用程序配置为具有两个构建配置文件:开发和生产。为此,我在src/main/resources文件夹下创建了两个子目录:src/main/resources/development和src/main/resources/production。每个子目录都有自己的。属性文件。 我使用命令mvn install-P${profile_here}构建应用程序 Maven将与所选配置文件相

  • 问题内容: 我想使用带有以下条目的application.properties文件设置配置文件: 如何在我的context.xml文件中设置spring.profiles.active?init-param仅在web.xml上下文中有效。 问题答案: 有几种更改活动配置文件的方法,这些方法都不直接取自属性文件。 您可以像在问题中一样使用。 您可以在应用程序启动时提供系统参数 你可以得到从你和编程方

  • 在Windows上,是否可以将Atom配置为使用系统默认代理,而不是在.apmrc文件中手动指定代理设置? 我最近开始使用atom-editor for Windows(Windows7 Pro),到目前为止我很喜欢它。我已经搜索了一段时间,试图让编辑能够连接到外部世界,但没有遇到任何成功。 我们的网络(我不是网络专家)配置为使用代理服务器(内部)访问Internet。在我的系统中,这是通过“自动

  • 我有两个环境来设置我的API,所以我有生产和开发。我首先需要的是找到一种方法来自动完成这一任务,例如,在运行时,不发送任何参数(-d)应用程序找到了一种方法来识别环境,但我没有找到任何方法来完成这一任务。 所以我看了一个教程,在同样有一个方法放了一个环境变量,并定义了我的application.properties。所以在下面的步骤中: > 我在我的应用程序中定义了3个文件,application