2016-03-01 380 views
0

我有一個PDF導出,和我使用外部的conf文件,鏈接到字體文件(字體用於西里爾字型)相對路徑

<font metrics-url="file:////fonts/DejaVuSerif.xml" kerning="yes" embed-url="file:////fonts/DejaVuSerif.ttf"> 
<font-triplet name="DejaVu Serif" style="normal" weight="normal"/></font> 

但在這種情況下,導致java.io.FileNotFoundException: /fonts/DejaVuSerif.xml

的問題是如何找到正確的路徑 我知道本地路徑/usr/lib/tomcat/webapps/myProject/WEB-INF/fonts/DeJavuSerif.xml 但如何區域設置相對路徑,例如

<font metrics-url="file:///**${contextPath}**/fonts/DejaVuSerif.xml" kerning="yes" 
embed-url="file:///**${contextPath}**/fonts/DejaVuSerif.ttf"> 
<font-triplet name="DejaVu Serif" style="normal" weight="normal"/></font> 
+0

您使用的是哪種版本的FOP? – lfurini

+1

@lfurini版本的FOP是2.1 – zond

+0

您是否從FOP關於['ServletContextURIResolver'](https://xmlgraphics.apache.org/fop/1.0/servlets.html#uriresolver)的文檔中看到了此頁?它似乎提供了一種方法來實現你想要的,但我不知道它是否可以應用於你的情況。 – lfurini

回答

0

我在我的xsl文件中引用了一個<fo:external-graphic src="path/image_to_display.jpg">標記內的圖像時有類似的問題。

根據Fop 2.2 documentation(請參閱表格和腳註),用於加載圖像和字體的基礎url由配置文件的位置定義。舉例來說,如果你的fop.xconf並顯示圖像都位於以下目錄中(其中有相同的根):

/WEB-INF/classes/FOP/fop.xconf 
/resources/images/image_to_display.jpg 

然後,使用JSF,你可以加載配置文件爲您與FOP實例這個代碼(省略的try-catch):

private FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); 

public void yourMethod() { 
    ServletContext contexteServlet = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); 
    File baseDir = new File(contexteServlet.getRealPath("/WEB-INF/classes/FOP"));   
    File xconf = new File(baseDir, "fop.xconf"); 
    FopConfParser parser = new FopConfParser(xconf); //parsing configuration 
    FopFactoryBuilder builder = parser.getFopFactoryBuilder(); //building the factory with the user options 
    FopFactory fopFactory = builder.build(); 
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 
    OutputStream out = new ByteArrayOutputStream(); 
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); 

如果在fop.xconf配置文件中,您設置的基礎URL上去三個目錄的水平,這樣

<base>../../../</base> 

那麼,你的根目錄將在您的結構的頂部,你將能夠顯示上述mentionned圖像:

<fo:external-graphic src="resources/images/image_to_display.jpg"> 

由於字體也位於相對於基本目錄,這個解決方案應該是適用你的情況也。