Image not displayed on pdf

五迷三道 提交于 2019-12-25 03:59:05

问题


I'm trying to include an image into a pdf wich is generated via XSL using Apache Cocoon. I've read about the tag <fo:external-graphic> wich works fine when a image is stored on the local drive or when you request an image wich resides physically on any server. But what i need is to show in the pdf an image (codebar image) wich is generated dinamically by a jsp, that is to say, java generates the image and it will never be stored on the hard drive.

The image is generated correctly by the jsp since it is showed on my browser. The problem is that the image is not shown in the pdf there is only big white space instead of the codebar image.

That is, something like these works fine:

<fo:external-graphic src="http://website.com/codebar.jpg" content-width="3cm" content-height="3cm"></fo:external-graphic>

<fo:external-graphic src="c:\Archivos de Programa\codebar.jpg" content-width="3cm" content-height="3cm"></fo:external-graphic>

But what i need to work is this:

<fo:external-graphic src="http://website.com/codebarGenerator.jsp" content-width="3cm" content-height="3cm"></fo:external-graphic>

The response headers are set correctly:

response.setContentType("image/jpg"); 

EDIT

I think the problem, may be, is that i´m trying to insert html instead of an image and that is why the pdf does not display the image. But, how can i get only the image generated by http://website.com/codebarGenerator.jsp without storing it in the hard drive?


回答1:


I am also facing the same issue I posted about this Here. I solved this by using URIResolver.

You can try for this also:

public class PdfURIResolver implements URIResolver{
@Override
public Source resolve(String href, String base) throws TransformerException {
    Source source = null;       
    try 
    {       
        //Image to InputStream conversion code here,
       //assign that InputStream to 'source' using source = new StreamSource(InputStream );         

    } 
    catch (Exception e) 
    {
        //exception handling
    }
    finally
    {
        //resourse closing if needed.
    }
    return source;
}
}

You have to tell FOP factory to use this Resolver using fopFactory.setURIResolver(new PdfURIResolver());



来源:https://stackoverflow.com/questions/24768480/image-not-displayed-on-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!