2012-03-16 32 views
4

我用:沒有找到字體ARIAL.TTF與Apache FOP 1.0

  • Apache防1.0
  • 的Java

我需要有特殊字符,unicode是比U0100更高的處理。我應該使用的目標字體是Arial。因爲我不能指望Arial出現在目標平臺上(例如Linux),所以我在我的jar中嵌入了Arial.ttf。 我實際的配置文件看起來是這樣的:

<fop ...> 
<!-- Base URL for resolving relative URLs --> 
<base>./</base> 

<!-- Font Base URL for resolving relative font URLs --> 
<font-base>./</font-base> 

<renderers> 
    <renderer mime="application/pdf"> 
     <fonts> 

      <font kerning="yes" embed-url="arial.ttf" encoding-mode="auto"> 
       <font-triplet name="Arial" style="normal" weight="normal"/> 
       <font-triplet name="ArialMT" style="normal" weight="normal"/> 
      </font> 

      <!--<directory>C:/Windows/Fonts</directory>--> 

     </fonts> 
     <auto-detect/> 
    </renderer> 
... 
</fop> 

這樣做,這樣我得到一個錯誤加載我的配置文件,以FOP:

org.apache.fop.apps.FOPException: Failed to resolve font with embed-url 'arial.ttf' 

唯一的辦法我設法得到它的工作,直到現在是硬編碼文件夾路徑到配置文件:

  <font kerning="yes" embed-url="C:/myapp/arial.ttf" encoding-mode="auto"> 
       <font-triplet name="Arial" style="normal" weight="normal"/> 
       <font-triplet name="ArialMT" style="normal" weight="normal"/> 
      </font> 

但顯然這不能是解決方案!

使用「目錄」標籤在Windows上工作,只是因爲你有Arial。但是,如上所述,它也必須在Linux,Mac等上運行。

「自動檢測」標籤無法使用(即使不在Windows上) - 也不是解決方案,因爲我不能指望目標安裝了Arial的平臺。

任何建議來解決這個問題?

回答

0

你有<自動檢測<字體外/ > >標籤。爲了解決我們的字體問題,我們把<自動檢測/ >內的<字體>標籤。 我們不使用額外<字體>配置,但只使用與<檢測系統字體自動檢測/ >

1

This solution不會爲我工作。這是因爲ClassLoader.getSystemResourceAsStream(href);返回null

我發現another solution但它有一個不同的問題 - 如果你有你添加到FOP與絕對系統路徑(選擇用戶如圖像)的一些其它資源,比你的自定義URIResolver被使用,也可以爲他們感到他們解決不正確。

如果要糾正這些例子只是將它們混合和你得到工作的解決方案是這樣的:

import java.io.InputStream; 
import javax.xml.transform.Source; 
import javax.xml.transform.TransformerException; 
import javax.xml.transform.URIResolver; 
import javax.xml.transform.stream.StreamSource; 

public class ClasspathUriResolver implements URIResolver { 

    @Override 
    public Source resolve(String href, String base) throws TransformerException { 
     Source source = null; 
     InputStream inputStream = getClass().getResourceAsStream(href); 
     if (inputStream != null) { 
      source = new StreamSource(inputStream); 
     } 
     return source; 
    } 
} 

使用

[...] 

FopFactory fopFactory = FopFactory.newInstance(); 

FOURIResolver uriResolver = (FOURIResolver) fopFactory.getURIResolver(); 
uriResolver.setCustomURIResolver(new ClasspathUriResolver()); 

[...] 
0

不知道如果這是你的問題,但我發現,自某些版本相對路徑在Fop配置文件中不起作用。對我來說,如果我提供了一個絕對路徑(以'/'開頭)或者用'file:'(如'file :.')前綴路徑,它就起作用了。