java 使用来自 html 的飞碟渲染 PDF 中的嵌入图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10840959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:48:31  来源:igfitidea点击:

Render embedded image in PDF using Flying-Saucer from html

javapdf-generationembedded-resourceflying-saucerxhtmlrenderer

提问by It Grunt

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:

我有一个 xhtml 文档,我正在使用 flysaucer 将其转换为 PDF。xhtml 有几个标签,这些标签具有内联的 base64 编码图像。xhtml 的来源是动态的,因此图像标签所在的结构可能会有所不同。这是标签外观的示例:

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

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.

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

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

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.

谁能让我知道我应该采取什么方法来实现这一目标?我看到了这个帖子,但是,我使用的是内联图像,所以我看不到如何使用 Edd 的解决方案来完成此操作。

Thanks in advance

提前致谢

采纳答案by Malcolm Smith

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

是的,您可以使用此处给出的方法:Render image from servlet in flysaucer generated pdf

Where Edd has:

Edd 有:

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

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 decoderto get the binary data, into a byte[]or Stream, you can then use Java ImageIOto 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!).

在 Edd 的情况下,图像来自远程源(他用 跳过了那个位input = ...;)。在您的情况下,您只想从您的 Base64 编码数据(base64,. 首先使用Base64 解码器将二进制数据获取到byte[]或 Stream之后的文本中读取它,然后您可以使用Java ImageIO从您的字节和按照 Edd 的方法将图像放入 PDF。在这里感谢 Edd(肯定要投票!)。

回答by Kukeltje

Flying-Saucer supports the data: protocol natively. All you have to do is register a protocol handler:

Flying-Saucer 本身支持 data: 协议。你所要做的就是注册一个协议处理程序:

-Djava.protocol.handler.pkgs=org.xhtmlrenderer.protocols

No need for servlets whatsoverver.

不需要 servlet 什么的。