title: XHTML to PDF using flying-saucer how to cache css
body: <p>In my production pipeline I need to generate a few hundred PDF from HTML. For this scenario I first convert the HTML into XHTML.
  Than im passing the 'cleaned' XHTML and the uri to the renderer.</p>
  
  <p>Since the *.css and imageFiles are equal for all the XHTML files I dont need to resolve them all the time I process a file.
  Im successfully using the following code for caching images. How can I cache .css files aswell? I want to avoid putting all files into my classpath.</p>
  
  <pre><code>ITextRenderer renderer = new ITextRenderer();
  
  ResourceLoaderUserAgent callback = new ResourceLoaderUserAgent(renderer.getOutputDevice());
  callback.setSharedContext(renderer.getSharedContext());
  
  for (MyObject myObject : myObjectList) {
  
      OutputStream os = new FileOutputStream(tempFile);
  
      final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
      documentBuilderFactory.setValidating(false);
      DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
      org.w3c.dom.Document document = builder.parse(myObject.getLocalPath); // full path to .xhtml
  
      renderer.getSharedContext().setUserAgentCallback(callback);
  
      renderer.setDocument(document, myObject.getUri());
      renderer.layout();
      renderer.createPDF(os);
  
      os.flush();
      os.close();
  }
      ...
  
  
  private static class ResourceLoaderUserAgent extends ITextUserAgent
  {
      public ResourceLoaderUserAgent(ITextOutputDevice outputDevice) {
          super(outputDevice);
      }
  
      protected InputStream resolveAndOpenStream(String uri) {
          InputStream is = super.resolveAndOpenStream(uri);
          System.out.println("IN resolveAndOpenStream() " + uri);
  
          return is;
      }
  }
  </code></pre>