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

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

问题描述

我有一个使用Apache FOP生成PDF文件的应用程序。除了生成的PDF中的西里尔字母之外,一切都很好。据我所知,我应该将具有西里尔字母的字体与应用程序绑定在一起。

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

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

fileLoader是我自己的文件读取工具,XSLT由它加载,一切正常。我尝试了另一种方式来做,没有运气。



在配置中,我有:

 <渲染> 
< renderer mime =application / pdf>
< filterList>
< value> flate< / value>
< / filterList>
< fonts>
< directory recursive =true> ./< / directory>
< auto-detect />
< / fonts>
< /渲染器>
< /渲染器>

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



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



我看到两个问题:


  • 我没有看到fop运行时,当我改变配置,即使我把它弄坏了。

  • 字体当然不会被加载。



可能我错过了一些非常明显的东西,希望有人能指导我。现在你使用你的配置文件 not ,所以字体没有配置,而且没有嵌入在PDF输出中。
$ b

FopFactoryBuilder URI 参数c>构造函数用于解析相对URI ,而不是加载配置文件。

根据嵌入说明,您的代码应该有类似于这样的内容:

  import org.apache.avalon.framework.configuration.Configuration; 
导入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();


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.

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 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.

In config I have:

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

Fonts are in subdirectory under conf/.

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.

I see two problems:

  • I do not see any reaction from fop runtime, when I change the config, even if I make it broken.
  • Fonts, of course, are not loaded.

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

解决方案

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.

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天全站免登陆