Adding an ImageProvider

We've used a relative path to our images in thoreau.html:

<img src="img/Henry_David_Thoreau_1861.jpg" />

If the HTML file you're parsing is stored in a directory that is different from the working directory, iText won't be able to create Image objects. We have to supply an implementation of the ImageProvider interface that tells iText what to do if an img tag is encountered. This interface has the following methods:

You can write your own class implementing these four methods, or you can subclass AbstractImageProvider. It is preferred to do the latter. XML Worker will use the store() method of the AbstractImageProvider class to cache all the Image objects that are encountered in a Map. These objects will be reused when the retrieve() method is called for an image with the same src. If you don't cache images, your PDF will be bloated. The same image bits and bytes will be written to the PDF more than once. The reset() method clears the cache; it is used when an ImageProvider is cloned. Finally, the getImageRootPath() method isn't implemented. You have to implement it yourself, as is done in the following snippet:

htmlContext.setImageProvider(new AbstractImageProvider() {
    public String getImageRootPath() {
        return "src/main/resources/html/";
    }
});

The relative path from our workdir to our thoreau.html file is "src/main/resources/html/". By using this ImageProvider in the HtmlPipelineContext, relative paths in the src attribute of an img tag will be adapted. iText will add src/main/resources/html/ to the src attribute of the tag (e.g. img/Henry_David_Thoreau_1861.jpg), resulting in the path src/main/resources/html/img/Henry_David_Thoreau_1861.jpg. This path is valid relative to the working directory.