Can pdfHTML render Base64 images to PDF?

Tags: pdfHtmlHTML to PDFBase64

This question was posted among others on Aug 22, '17 by IMarco and on Mar 22 '15 by Lion Bachar

Yes, iText supports external images as shown in different examples in the previous chapters, but it also supports inline images stored as Base64 strings.

See for instance the C07E03_Base64Image example. There's nothing special in the createPdf() method.

public void createPdf(String baseUri, String src, String dest) throws IOException {
    HtmlConverter.convertToPdf(new File(src), new File(dest));
}

The only thing that is special about this example, is the base64.html HTML file (note that the base64-encoded image was truncated to fit this page):

<html>
    <head><title>Test</title></head>
    <body>
        <h1>Test</h1>
        <p>Hello World</p>
        <img alt="Embedded Image" src="data:image/png;base64,iVBORw0...ErkJggg==" />
    </body>
</html>

The result is identical to what we had in chapter 1 when we used an external image.

Source code, browser view and resulting PDF of a file with a base64-encoded image
Source code, browser view and resulting PDF of a file with a base64-encoded image