2013-07-19 74 views
4

我的應用程序在JAR的fonts目錄中有一個TTF字體。從JAR加載FOP的字體

myapp.jar/
    fop/
     config.xml 
     font.ttf 

創建我FOP是這樣的:

FopFactory fopFactory = FopFactory.newInstance(); 
    fopFactory.setStrictValidation(false); 
    fopFactory.setUserConfig(getClasspathFile("/fop/config.xml")); 
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 
    ... 

如何配置​​3210在PDF文件中我渲染embeddd font.ttf

回答

4

似乎我的帖子太晚了,但可能會對其他人有用。 [java的8,FOP 2.1]

import lombok.SneakyThrows; 
... 
     @SneakyThrows 
      private FopFactory getFopFactory(){ 
       InputStream fopConfigStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("/fop/config.xml"); 
       FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI(), new CustomPathResolver()); 
       FopFactory factory = builder.setConfiguration(new DefaultConfigurationBuilder().build(fopConfigStream)).build(); 
       fopConfigStream.close(); 
       return factory; 
      } 
    ... 
     private static final class CustomPathResolver implements ResourceResolver { 
      @Override 
      public OutputStream getOutputStream(URI uri) throws IOException { 
       return Thread.currentThread().getContextClassLoader().getResource(uri.toString()).openConnection() 
         .getOutputStream(); 
      } 

      @Override 
      public Resource getResource(URI uri) throws IOException { 
       InputStream inputStream = ClassLoader.getSystemResourceAsStream("fop/" + FilenameUtils.getName(uri)); 
       return new Resource(inputStream); 
      } 
     } 

config.xml中:

<fop version="1.0"> 
    <renderers> 
     <renderer mime="application/pdf"> 
      <fonts> 
       <font kerning="yes" embed-url="font.ttf" embedding-mode="subset"> 
       <font-triplet name="Font name" style="normal" weight="normal"/> 
       </font> 
      </fonts> 
     </renderer> 
    </renderers> 
</fop>