fop xml配置中的相对路径

时间:2016-03-01 15:37:44

标签: java xml spring apache-fop

我有一个PDF导出,我使用外部conf文件链接到font-file(字体用于西里尔字形)

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

1 个答案:

答案 0 :(得分:0)

我在xsl文件中的<fo:external-graphic src="path/image_to_display.jpg">标记内引用了一个类似的问题。

根据Fop 2.2 documentation,(参见表格和脚注),用于加载图像和字体的基本网址由fop.xconf配置文件的位置定义。例如,如果您的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配置文件中,您将基本网址设置为三个目录级别,就像这样

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

然后,您的基本目录将位于您的结构的顶部,您将能够显示上面提到的图像:

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

由于字体也位于相对于基本目录的位置,因此该解决方案也适用于您的情况。