iTextPDF pdfHTML add on. Unable to retrieve image

467 Views Asked by At

Im following the tutorial given here : (https://kb.itextpdf.com/home/it7kb/ebooks/itext-7-converting-html-to-pdf-with-pdfhtml/chapter-1-hello-html-to-pdf) to generate a PDF from HTML using IText's pdfHTML add on.

<itext.version>7.2.4</itext.version>
<itext.pdfhtml.version>4.0.4</itext.pdfhtml.version>

I get the following error: ERROR com.itextpdf.styledxmlparser.resolver.resource.ResourceResolver - Unable to retrieve image with given base URI (file:/C:/Users/User/Documents/study/pdfseven/src/main/resources/html/) and image source path (img/logo.png)

But the src folder and image definitely exist and its in the location defined by the baseURI as well.

Main method:

public static void main(String\[\] args) throws IOException {
String HTML = "\<h1\>Test\</h1\>\<p\>Hello World\</p\>\<img src="img/logo.png"\>";
String TARGET = "target/results/ch01/";
String DEST = String.format("%stest-03.pdf", TARGET);
String BASEURI = "src/main/resources/html";
File file = new File(TARGET);
file.mkdirs();
new PdfGenerator().createPdf(HTML, DEST, BASEURI);
}

createPdf method:

public void createPdf(String html, String dest, String baseUri) throws IOException, FileNotFoundException {
    ConverterProperties properties = new ConverterProperties();
    properties.setBaseUri(baseUri);
    HtmlConverter.convertToPdf(html, new FileOutputStream(dest), properties);
}

Any idea why the image cant be retreived? Thanks

1

There are 1 best solutions below

1
On

Replace:

String HTML = "\<h1\>Test\</h1\>\<p\>Hello World\</p\>\<img src="img/logo.png"\>";

with:

String HTML = "\<h1\>Test\</h1\>\<p\>Hello World\</p\>\<img src=\"img/logo.png\"\>";