0

I'm parsing pdf from html with ITextRenderer as follows:

private void createPdf(File file, String content) throws IOException, DocumentException {
        OutputStream os = new FileOutputStream(file);
            content = tidyUpHTML(content);
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(content);
            renderer.layout();
            renderer.createPDF(os);
        os.close();
}

Now, if the html contains a local path to an image, it doesn't show in the pdf. However, if the img tag's src-value is an URL to an image online, it does work.

As follows: Doesn't show on the pdf:

<img src="C:\path\to\image\image.png" />

Does show correctly on the pdf:

<img src="http://flagpedia.net/data/currency/jpy/100jpy.jpg" />

The path to the local file is correct, as it shows the image if I copy paste that path to my web browser for example.

How to get it to show on the pdf?

UPDATE: This all is running in a JSF / Primefaces Web Application on a WildFly10 Application server. So it seems that direct paths to a file system won't work. Then, which directory in the war should I use to use static resources, such as this image. Currently it is in webapp/resources/images.

Steve Waters
  • 3,348
  • 9
  • 54
  • 94

1 Answers1

0

Here's how to do it when using JSF2.0 or newer, which has the <h:graphicImage> component.

I have the image myimage.png in the folder webapp/resources/images/.

Now I add the component as follows:

<h:graphicImage library="images" name="myimage.png" />

Now in the browser I inspected that element to get the url and inserted that url as the value of src-attribute in the <img>-tag as follows:

<img src="http://localhost:8080/myproject//javax.faces.resource/myimage.png.xhtml?ln=images"/>

And voila! Now I'm using a static image with an url in the html template, which will be parsed as the said pdf and the image works in the pdf as well. I hope this trick helps someone.

Steve Waters
  • 3,348
  • 9
  • 54
  • 94