javaitextflying-saucerxhtmlrenderer

flying saucer (xhtmlrenderer) requests image 4 times


in my xhtml i have the following:

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

and I render like so:

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(XMLResource.load(in).getDocument(), url);
renderer.layout();
renderer.createPDF(out);

the resulting PDF is as expected, however I notice that the image (which is included only once in the xhtml and renders only once) is being requested 4 times.

now, besides the obvious problem of the extra data download this wouldn't really be a problem for most people.

however, i need to implement an 'expire on use' image cache for dynamic images and this is becoming a real headache...

why does flying saucer need to make 4 requests for the image if it only renders it once?


Solution

  • I've just gone through the code and there is no solution here (without a re-write of itext & flying saucer).

    the first time the stream is open is just to test whether it can be opened, the data is not read.

    the second time is itext reading the header to determine the file type, only the first 4 bytes are read.

    the third time is itext determining the dimensions of the image it seems - i'm not sure but i don't think much other than headers is read here either.

    the last read is to render the image.

    so the download impact is not great, 4 url connections - yes, but the entire stream is only transferred once

    and my 'expire on use' cache will have to be 'expire on 4th use' instead.