从flyingsaucer生成的pdf中的servlet渲染图像 [英] Render image from servlet in flyingsaucer generated pdf

查看:292
本文介绍了从flyingsaucer生成的pdf中的servlet渲染图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用flyingsaucer通过servlet将xhtml文档呈现为pdf,该servlet返回生成的pdf文档。 xhtml文档具有从另一个servlet请求的图像。图像servlet在返回适当的图像之前检查谁登录。下面的代码显示了图像的请求方式:

I'm using flyingsaucer to render an xhtml document to pdf through a servlet which returns the generated pdf document. The xhtml document features an image which is requested from another servlet. The image servlet checks who is logged in before returning the appropriate image. The code below shows how the image is requested:

<img height="140" width="140" src="http://localhost:8080/myapp/servlet/DisplayPic" />

我的问题是图像的http请求来自pdf渲染器而不是登录用户因此图像servlet不知道谁登录,因此不会返回所需的图像。

My problem is that the http request for the image is from the pdf renderer and not the logged in user so the image servlet doesn't know who's logged in and therefore the desired image is not returned.

我目前正在使用下面的代码来渲染xhtml文档:

I'm currently using the code below to render the xhtml document:

ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(xhtmlDocumentAsString);
renderer.layout();
os = response.getOutputStream();
renderer.createPDF(os);

我需要在请求图像servlet时维护用户的会话,或者为渲染器提供图像用于特定的xhtml元素。我认为后者可以使用 ReplacedElementFactory 来完成,但我无法挖出任何可以帮助我的示例代码。

I need to either maintain the user's session when the image servlet is requested or provide the renderer with the image to use for that specific xhtml element. I think the latter can be done using a ReplacedElementFactory but I haven't been able to dig out any example code that can help me.

推荐答案

我现在已经很好地工作了。这是代码。

I've got this working very nicely now. Here's the code.

在我的xhtml文档中,我有:

In my xhtml document i have:

<div class="profile_picture" style="display:block;width:140px;height:140px;" />

(我正在使用 div 元素而不是 img 因为工厂仅用于块级元素。

(I'm using a div element instead of img as the factory is only used for block level elements)

我使用以下方式渲染文档:

I render my document using:

ITextRenderer renderer = new ITextRenderer();
renderer.getSharedContext().setReplacedElementFactory(new ProfileImageReplacedElementFactory(renderer.getSharedContext().getReplacedElementFactory()));
renderer.setDocumentFromString(xhtmlDocumentAsString);
renderer.layout();
os = response.getOutputStream();
renderer.createPDF(os);

我有自己的 ReplacedElementFactory ,如下所示:

And i have my own ReplacedElementFactory as below:

public class ProfileImageReplacedElementFactory implements ReplacedElementFactory {

    private final ReplacedElementFactory superFactory;

    public ProfileImageReplacedElementFactory(ReplacedElementFactory superFactory) {
        this.superFactory = superFactory;
    }

    @Override
    public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockBox blockBox,
            UserAgentCallback userAgentCallback, int cssWidth, int cssHeight) {

        Element element = blockBox.getElement();
        if (element == null) {
            return null;
        }

        String nodeName = element.getNodeName();
        String className = element.getAttribute("class");
        if ("div".equals(nodeName) && className.contains("profile_picture")) {

            InputStream input = null;
            try {
                input = ...;
                byte[] bytes = IOUtils.toByteArray(input);
                Image image = Image.getInstance(bytes);
                FSImage fsImage = new ITextFSImage(image);

                if (fsImage != null) {
                    if ((cssWidth != -1) || (cssHeight != -1)) {
                        fsImage.scale(cssWidth, cssHeight);
                    }
                    return new ITextImageElement(fsImage);
                }
            } catch (IOException e) {
                getLogger().error(ExceptionUtils.getStackTrace(e));
            } catch (BadElementException e) {
                getLogger().error(ExceptionUtils.getStackTrace(e));
            } finally {
                IOUtils.closeQuietly(input);
            }
        }

        return superFactory.createReplacedElement(layoutContext, blockBox, userAgentCallback, cssWidth, cssHeight);
    }

    @Override
    public void reset() {
        superFactory.reset();
    }

    @Override
    public void remove(Element e) {
        superFactory.remove(e);
    }

    @Override
    public void setFormSubmissionListener(FormSubmissionListener listener) {
        superFactory.setFormSubmissionListener(listener);
    }
}

这篇关于从flyingsaucer生成的pdf中的servlet渲染图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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