Enabling Secure Resources with the Flying Saucer (ITextRenderer)

I use Flying Saucer to create a pdf file from xhtml hosted on a tomcat server. Most of the images included in pdf are publicly available (logos, etc.), but some of them are protected for registration (that is, they are transmitted through the servlet if the user is logged in).

When I insert the URL in the browser, the image, of course, is displayed in order, because the browser sends the session with the request. But when the Flying Saucer provides a pdf file, it does not include the protected image because it knows nothing about the session.

So my question is: is there a way to enable byte streams for the Flying Saucer to solve, just as you can add decidable fonts? I tried something like this , but there is no easy way to set UAC to ITextRenderer, and it complained every time I tried.

+6
java pdf flying-saucer xhtmlrenderer resolve
source share
2 answers

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

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

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

+6
source share

I am also redefining ITextUserAgent - from the source, it looks like it is using ITextRenderer. You must provide an output device in the constructor, which you can get from the render object. Another problem was that you must explicitly set the “general context” using the setter method, otherwise you will get NPE during rendering. Here is the code to configure the object:

 ITextRenderer renderer = new ITextRenderer(); MyUserAgentCallback uac = new MyUserAgentCallback(renderer.getOutputDevice()); uac.setSharedContext(renderer.getSharedContext()); renderer.getSharedContext().setUserAgentCallback(uac); 

In addition, here is the basic idea of ​​MyUserAgentCallback using basic authentication:

 private static class MyUserAgentCallback extends ITextUserAgent { public MyUserAgentCallback(ITextOutputDevice outputDevice) { super(outputDevice); } @Override protected InputStream resolveAndOpenStream(String uri) { if (_isProtectedResource(uri)) { java.io.InputStream is = null; uri = resolveURI(uri); try { URL url = new URL(uri); String encoding = new BASE64Encoder().encode ("username:password".getBytes()); URLConnection uc = url.openConnection(); uc.setRequestProperty ("Authorization", "Basic " + encoding); is = uc.getInputStream(); Log.debug("got input stream"); } catch (java.net.MalformedURLException e) { Log.error("bad URL given: " + uri, e); } catch (java.io.FileNotFoundException e) { Log.error("item at URI " + uri + " not found"); } catch (java.io.IOException e) { Log.error("IO problem for " + uri, e); } return is; } else { return super.resolveAndOpenStream(uri); } } private boolean _isProtectedResource(String uri) { // does this require authentication? } } 
+2
source share

All Articles