简体   繁体   English

使用来自html的Flying-Saucer渲染PDF格式的嵌入图像

[英]Render embedded image in PDF using Flying-Saucer from html

I have an xhtml document that I'm turning into a PDF using flyingsaucer. 我有一个xhtml文档,我正在使用flyingsaucer变成PDF。 The xhtml has several tags that have a base64 encoded images inline. xhtml有几个标签,内嵌base64编码图像。 The source of the xhtml is dynamic so the structure of where the image tags are can vary. xhtml的来源是动态的,因此图像标签所在的结构可能会有所不同。 This is a sample of what the tag looks like: 这是标签的样子:

<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. 当我在浏览器中查看html时,图像显示正确,但是,img元素不会在最终的PDF中呈现。 Here is how I'm rendering it out to create the 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 提前致谢

Yes, you can use the approach given here: Render image from servlet in flyingsaucer generated pdf 是的,您可以使用此处给出的方法: 在flyingsaucer生成的pdf中从servlet渲染图像

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 = ...; ). 在Edd的情况下,图像来自远程源(他使用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!). 在您的情况下,您只想从Base64编码数据( base64,之后的文本base64, 。首先使用Base64解码器获取二进制数据,到byte[]或流中读取它,然后您可以使用Java ImageIO创建来自你的字节的图像,并按照Edd的方法将图像转换为PDF。感谢Edd在这里(肯定是upvote!)。

Flying-Saucer supports the data: protocol natively. Flying-Saucer支持数据:本地协议。 All you have to do is register a protocol handler: 您所要做的就是注册一个协议处理程序:

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

No need for servlets whatsoverver. 无论如何都不需要servlet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM