Embed an embedded image in a PDF using Flying-Saucer from html

I have an xhtml document that I convert to PDF using flyingsaucer. Xhtml has several tags that have base64 encoded images. The xhtml source is dynamic, so the structure in which image tags are subject to change. This is an example of what the tag looks like:

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

When I look at the html in the browser, the image is displayed correctly, however the img element is not displayed in the final PDF file. This is how I create a PDF file.

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

Can someone tell me which approach should I take for this? I saw this post , however I use inline images, so I don’t see how I can do this using Edd's solution.

Thanks in advance

+7
source share
2 answers

Yes, you can use the approach given here: Send flingsaucer servlet image generated pdf

Where Edd has:

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

In the case of Edd, the image comes from a remote source (it skips this bit with input = ...; ). In your case, you just want to read it from Base64 encoded data (text after base64, First use a Base64 decoder to get binary data, in byte[] or Stream, you can use Java ImageIO to create an image from your bytes and follow the Edd approach To get the image in PDF Kudos to Edd here (upvote for sure!).

+8
source

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

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

No servlets needed.

+4
source

All Articles