使用来自HTML的Flying-Saucer渲染PDF中的嵌入式图像 [英] Render embedded image in PDF using Flying-Saucer from html

查看:210
本文介绍了使用来自HTML的Flying-Saucer渲染PDF中的嵌入式图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xhtml文档,我正在使用flyingsaucer转换为PDF。 xhtml有几个带有内联base64编码图像的标签。 xhtml的源是动态的,因此图像标记所在的结构可以变化。这是标签外观的一个示例:

I have an xhtml document that I'm turning into a PDF using flyingsaucer. The xhtml has several tags that have a base64 encoded images inline. The source of the xhtml is dynamic so the structure of where the image tags are can vary. This is a sample of what the tag looks like:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAagAAAEuCAYAAADbW4YFAAAgAElEQVR4Aex9CYBdRZ ...

当我在浏览器中查看html时,图像正确显示,但是, img元素不会在最终的PDF中呈现。这是我如何呈现出来以创建PDF。

When I look at the html in a browser, the image appears correctly, however, the img element doesn't get rendered in the final PDF. Here is how I'm rendering it out to create the PDF.

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(builder.parse(source), "");
renderer.layout();
renderer.createPDF(response.getOutputStream(),true);

任何人都可以让我知道我应该采用哪种方法来实现这一目标吗?一个href = https://stackoverflow.com/questions/10316607/render-image-from-servlet-in-flyingsaucer-generation-pdf>此信息,但是,我使用的是嵌入式图像,所以我无法看到我如何使用Edd的解决方案来做到这一点。

Can anyone let me know what approach I should take to accomplish this? I saw this posting, however, I'm using inline images so I can't see how I can accomplish this using Edd's solution.

预先感谢

解决方案

是的,您可以使用此处提供的方法:在flyingsaucer生成的pdf中从servlet渲染图像

Yes, you can use the approach given here: Render image from servlet in flyingsaucer generated pdf

Edd在哪里:

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

在Edd的情况下,图像来自远程源(他跳过 input = ...; )。在您的情况下,您只想从Base64编码数据( base64,之后的文本)中读取它。首先使用 Base64解码器,将二进制数据转换为 byte [] 或流,然后可以使用 Java ImageIO

In Edd's case the image is coming from a remote source (he skips over that bit with input = ...;). In your case you just want to read it from your Base64 encoded data (the text after the base64,. First use a Base64 decoder to get the binary data, into a byte[] or Stream, you can then use Java ImageIO to create the image from your bytes and follow Edd's approach to get the image into the PDF. Kudos to Edd here (upvote for sure!).

这篇关于使用来自HTML的Flying-Saucer渲染PDF中的嵌入式图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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