在 java servlet 中嵌入 Apache fop 2.1 [英] Embedding Apache fop 2.1 in java servlet

查看:18
本文介绍了在 java servlet 中嵌入 Apache fop 2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Apache FOP 生成 PDF 文件的应用程序.除了生成的 PDF 中的西里尔字母外,一切都很好.据我了解,我应该将带有西里尔字母的字体与应用程序捆绑在一起.

I have an application that uses Apache FOP to generate PDF files. Everything works great, except Cyrillic letters in the generated PDF. As far as I understand, I am supposed to bundle font that has Cyrillic letters with the application.

因此,我将应用程序设置如下:我在 ../src/main/resources/conf/fop.xml 中有配置文件(PDF 渲染器的默认配置文件)并像这样初始化 FOP:

So, I set the application as follows: I have config file in ../src/main/resources/conf/fop.xml (pretty much default one for PDF renderer) and initialise FOP like this:

FopFactoryBuilder fopBuilder =
  new FopFactoryBuilder(fileLoader.getFile("conf/fop.xml").toURI(),
    new ClasspathResolverURIAdapter());
fopFactory = fopBuilder.build();

fileLoader 是我自己的用于读取文件的实用程序,XSLT 由它加载,一切正常.我尝试了另一种方法,但没有运气.Fop 本身效果很好,除了字体.

fileLoader is my own utility for reading files, XSLT is loaded by it and everything works great. I tried another ways to do so, with no luck. Fop itself works great, except for the fonts.

在配置中我有:

<renderers>
    <renderer mime="application/pdf">
      <filterList>
        <value>flate</value>
      </filterList>
      <fonts>
        <directory recursive="true">./</directory>
        <auto-detect/>
      </fonts>
    </renderer>
  </renderers>

字体位于 conf/下的子目录中.

Fonts are in subdirectory under conf/.

我的 XSLT 引用了字体,通过命令行一切正常,我得到了我想要的结果,所以我猜 XSLT 是正确的.

My XSLT references the fonts and via command-line everything works fine, I get the result I want, so I guess, that XSLT is correct.

我看到两个问题:

  • 当我更改配置时,我没有看到 fop 运行时的任何反应,即使我使其损坏.
  • 当然不会加载字体.

可能我在这里遗漏了一些非常明显的东西,希望有人能指导我.

Probably I am missing something very obvious here and hope that someone could guide me.

推荐答案

目前你没有使用你的配置文件,所以字体没有配置,它们未嵌入 PDF 输出中.

At the moment you are not using your configuration file, so the fonts are not configured and they are not embedded in the PDF output.

FopFactoryBuilder构造函数中的URI参数用于解析相对URI,而不是加载配置文件.

The URI parameter in the FopFactoryBuilder constructor is used to resolve relative URIs, not to load the configuration file.

根据嵌入说明,您的代码应该具有类似的东西:

As per embedding instructions, your code should have something similar to this:

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;

...

DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.buildFromFile(new File("conf/conf.xml"));
FopFactoryBuilder fopFactoryBuilder = new FopFactoryBuilder(new File(".").toURI()).setConfiguration(cfg);
FopFactory fopFactory = fopFactoryBuilder.build();

这篇关于在 java servlet 中嵌入 Apache fop 2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆