views:

91

answers:

1

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?

A: 

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.

pstanton
Maybe the http headers contain some information how to filter this? I would guess the first request is a HEAD request, which you could not count. Otherwise I would say sthg like "After the first request, the resource is available for another 10 seconds" or so.
ZeissS
zeiss, they don't set any different headers or parameters to identify the type of information they will pull from the stream. they just to a `new URL(uri).openStream()` each time. that would have been handy because i could just return a dummy stream that tells the api the relevant information only for each request and leave the real data for the last request. i could do this by request index though, ie request 1 returns 'JPEG', request 2 returns ... etc.. re the 10 second idea, my 4th request approach is better for a number of reasons.
pstanton