这是我的第一个问题,我希望我做对了。对不起,我的坏英语提前:)FOP:如何指定图片的src相对路径?
我使用JSF 2.0(Eclipse IDE),我试图用Apache FOP 1.0生成一些PDF文件。
我能够使用Apache Fop site上的说明制作简单的PDF文件,但我无法从我的应用程序文件夹插入任何图像。我的文件夹结构是这样的: 在我的应用程序的WebContent我有(其中包括其他) pdf_transform/XSLT/transformFile.xsl和 pdf_transform/XSLT/logo.jpg
在transformFile.xsl我有
<fo:block><fo:external-graphic src="url('logo.jpg')"/></fo:block>
,但是当我在短声我的servlet“showPDF”按钮,我得到的PDF文件没有图像(一切是存在的),并在控制台此消息:
严重:源即返回 FR om URI解析不包含 InputStream for URI:logo.jpg Nov 18, 2010 5:16:49 PM org.apache.fop.events.LoggingEventListener processEvent SEVERE:找不到图像。 URI:logo.jpg。 (无上下文信息 可用)
我试图用“logo.jpg”,而不是网址(“logo.jpg”),将在各个地方的图像WebContent文件夹内,并使用不同的导航(” ./标志.jpg“),但它没有奏效。
它工作正常,如果我设置绝对路径(例如“d:/fop/images/logo.jpg”),但我需要resurces美白我的应用程序。
在搜索时,我发现这与fopFactory.setURIResolver()和/或userAgent.setBaseURL()有关。尝试了一下,但没有成功。
我对JSF和FOP都很陌生,这种形象的状况一直困扰着我。有人可以帮我解决这个问题,或者至少可以直接给我一些关于“如何为相对路径使用配置FOP”的教程?
编辑:我不想要任何绝对路径和应用程序应该独立于其在计算机上的位置(可发布)工作。我的搜索告诉我这与配置FOP有关,但我不知道该怎么做:)
在此先感谢。
P.S.这是方法,该方法被调用来显示PDF:
public void printExchangeRateList(ActionEvent event) {
BufferedOutputStream output = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
String path = externalContext.getRealPath("/");
try {
response.reset();
response.setHeader("Content-Type", "application/pdf");
output = new BufferedOutputStream(response.getOutputStream(), 10240);
File xsltfile = new File(path+"/pdf_transform/xslt/transformFile.xsl");
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, output);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(new StreamSource(xsltfile));
Source src = new DOMSource(makeXML()); // my method
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
} finally {
if (output != null) output.close();
/*try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
} catch (Exception e) {
// TODO Auto-generated catch block
}
facesContext.responseComplete();
}
感谢您的回答。但那不是我想要的。当你制作xml时,“/ path/to/app /”字符串必须看起来像“C:\ Eclipse \ workspace \ myProject \ .. etc”(如果我正确的话)。我使这个应用程序是可发布的,所有XML数据和图像即时访问必须引用和白色应用程序范围(对不起,没有写在我的问题明确,我现在编辑它)。 – Nikola 2010-11-18 21:27:42