FOP:如何指定图片src相对路径? [英] FOP: how to specify image src relative path?
问题描述
这是我在这里的第一个问题,我希望我做得对.对不起,我的英语不好,谢谢:)
我正在使用JSF 2.0(Eclipse IDE),并且试图使用Apache FOP 1.0生成一些PDF文件.
我能够使用 Apache Fop网站上的说明制作简单的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文件中没有图像(其他所有内容),并且此消息在控制台中显示:
严重:返回的源 URI解析中不包含 URI的InputStream:logo.jpg 11月18日, 2010 5:16:49 PM org.apache.fop.events.LoggingEventListener processEvent SEVERE:找不到图像. URI:logo.jpg. (没有上下文信息 可用)
我尝试使用'logo.jpg'代替url('logo.jpg'),将图像放在WebContent文件夹内的各个位置,并使用不同的导航("./logo.jpg"),但是没有用. /p>
如果我设置了绝对路径(例如"d:/fop/images/logo.jpg"),则效果很好,但是我需要在我的应用程序中重新使用.
在搜索时,我发现这与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();
}
我找到了解决我问题的方法.我以为我尝试过,但是那时我似乎犯了一些小错误.无论如何,使用以下代码
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
String basePath = externalContext.getRealPath("/");
FopFactory fopFactory = FopFactory.newInstance();
fopFactory.setBaseURL(basePath);
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
foUserAgent.setBaseURL(fopFactory.getBaseURL());
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, output); // for some output
您可以使用从应用程序的WebContent文件夹开始的相对路径,从xslt文件访问图像(和其他资源).就我而言,我可以像这样访问logo.jpg
<fo:external-graphic src="url('pdf_transform/xslt/logo.jpg')"/>
花时间去弄清楚这个,我不明白为什么网上没有这样的基本例子(或者我找不到它们:)
注意:在FOP 2.0中没有setBaseURL()
方法.而是将基本URL作为参数传递给FopFactory.newInstance()
.许多其他设置器已移至Apache Fop site , but i can't insert any image from my application folder. My folder structure is like this:
In my application WebContent i have (among else)
pdf_transform/xslt/transformFile.xsl, and
pdf_transform/xslt/logo.jpg
In transformFile.xsl i have
<fo:block><fo:external-graphic src="url('logo.jpg')"/></fo:block>
but when i clik 'showPDF' button in my servlet, i get PDF file without image (everything else is there), and this messages in console:
SEVERE: The Source that was returned from URI resolution didn't contain an InputStream for URI: logo.jpg Nov 18, 2010 5:16:49 PM org.apache.fop.events.LoggingEventListener processEvent SEVERE: Image not found. URI: logo.jpg. (No context info available)
I tried to use 'logo.jpg' instead of url('logo.jpg'), putting image on various places inside WebContent folder and using different navigation("./logo.jpg") but it didnt work.
It works fine if i set absolute path (for example "d:/fop/images/logo.jpg") but i need resurces whitin my application.
While searching, i found that this is related to fopFactory.setURIResolver() and/or userAgent.setBaseURL(). Tried something with that, but didnt succeed.
I am new to both JSF and FOP, and this image situation has been bothering me quite a while. Can someone help me with this, or at least direct me to some tutorial on "how to configure FOP for relative path use"?
EDIT: I don't want any absolute paths and app should work independently of its location on computer (to be publishable). My search tells me it has something to do with configuring FOP, but i don't know how to do it :)
Thanks in advance.
P.S. This is method which is called to display 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();
}
i found solution to my problem. I thought i tried that, but it seems i made some little mistake back then. Anyway, with the following code
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
String basePath = externalContext.getRealPath("/");
FopFactory fopFactory = FopFactory.newInstance();
fopFactory.setBaseURL(basePath);
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
foUserAgent.setBaseURL(fopFactory.getBaseURL());
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, output); // for some output
you can access your images (and other resources) from your xslt file using relative path starting from your application's WebContent folder. In my case, i can access logo.jpg like this
<fo:external-graphic src="url('pdf_transform/xslt/logo.jpg')"/>
Took me time to figure out this, i don't get it why no examples with such basic thing on the net (or i can't find them :)
Note: In FOP 2.0 there is no setBaseURL()
method. Instead you pass the base URL as a parameter to FopFactory.newInstance()
. Many of the other setters have been moved to FopFactoryBuilder.
这篇关于FOP:如何指定图片src相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!