FOP:如何指定图像 src 相对路径? [英] FOP: how to specify image src relative path?

查看:27
本文介绍了FOP:如何指定图像 src 相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里的第一个问题,我希望我做对了.抱歉我的英语不好:)

This is my first question here, i hope i'm doing it right. Sorry for my bad English in advance :)

我正在使用 JSF 2.0 (Eclipse IDE) 并且我正在尝试使用 Apache FOP 1.0 生成一些 PDF 文件.

I am using JSF 2.0 (Eclipse IDE) and i'm trying to generate some PDF files using Apache FOP 1.0.

我能够使用 Apache Fop 站点 上的说明制作简单的 PDF 文件,但我无法插入我的应用程序文件夹中的任何图像.我的文件夹结构是这样的:在我的应用程序 WebContent 我有(除其他外)pdf_transform/xslt/transformFile.xsl,和pdf_transform/xslt/logo.jpg

I was able to make simple PDF files using instructions on 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

在transformFile.xsl中我有

In transformFile.xsl i have

<fo:block><fo:external-graphic src="url('logo.jpg')"/></fo:block>

但是当我在我的 servlet 中点击showPDF"按钮时,我得到没有图像的 PDF 文件(其他一切都在那里),并且在控制台中显示此消息:

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:返回的源来自 URI 的解析不包含URI 的输入流:logo.jpg 11 月 18 日,2010 下午 5:16:49org.apache.fop.events.LoggingEventListenerprocessEvent 严重:未找到图像.URI:logo.jpg.(没有上下文信息可用)

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)

我尝试使用 'logo.jpg' 而不是 url('logo.jpg'),将图像放在 WebContent 文件夹内的各个位置并使用不同的导航 ("./logo.jpg") 但它没有用.

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.

如果我设置绝对路径(例如d:/fop/images/logo.jpg"),它工作正常,但我的应用程序需要资源.

It works fine if i set absolute path (for example "d:/fop/images/logo.jpg") but i need resurces whitin my application.

在搜索时,我发现这与 fopFactory.setURIResolver() 和/或 userAgent.setBaseURL() 有关.尝试了一些东西,但没有成功.

While searching, i found that this is related to fopFactory.setURIResolver() and/or userAgent.setBaseURL(). Tried something with that, but didnt succeed.

我是 JSF 和 FOP 的新手,这种图像情况一直困扰着我.有人可以帮我解决这个问题,或者至少指导我阅读一些关于如何为相对路径使用配置 FOP"的教程?

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

我不希望任何绝对路径和应用程序应该独立于其在计算机上的位置工作(可发布).我的搜索告诉我它与配置 FOP 有关,但我不知道如何去做 :)

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

提前致谢.

附言这是被调用以显示 PDF 的方法:

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

您可以使用从应用程序的 WebContent 文件夹开始的相对路径从您的 xslt 文件访问您的图像(和其他资源).就我而言,我可以像这样访问 logo.jpg

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

注意:在 FOP 2.0 中没有 setBaseURL() 方法.相反,您将基本 URL 作为参数传递给 FopFactory.newInstance().许多其他设置器已移至 FopFactoryBuilder.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆