Search code examples
javapdfresolveflying-saucerxhtmlrenderer

Resolving protected resources with Flying Saucer (ITextRenderer)


I'm using Flying Saucer to create a pdf from xhtml, hosted on a tomcat server. Most of the images included in the pdf are publicly available (logos and so on), but some of them are protected behind a login (that is, they are streamed through a servlet if the user is logged in).

When I paste the url in the browser, the image is of course displayed fine, because the browser sends the session with the request. But when Flying Saucer renders the pdf, it doesn't include the protected image because it doesn't know anything about the session.

So, my question is; is there any way to include the byte streams for Flying Saucer to resolve, just as it is possible to add resolvable fonts? I have tried something like this, but there is no easy way to set the UAC on the ITextRenderer, and it complained every time i tried.


Solution

  • You can set the UserAgentCallback this way, and Flying Saucer will use it to resolve the urls (tested, works with Release 8):

    ITextRenderer renderer = new ITextRenderer();
    renderer.getSharedContext().setUserAgentCallback(new MyUAC());
    

    MyUAC should extend the NaiveUserAgent, and override the resolveAndOpenStream method as the other page suggests.