Java – XHTML to PDF how to cache CSS using fly saucer
                                        
                    •
                    Java                                    
                In my production pipeline, I need to generate hundreds of PDFs from HTML In this case, I first convert HTML to XHTML
Due to * CSS and imagefiles are the same for all XHTML files, so I don't need to solve them when processing files I successfully used the following code to cache the image How to cache What about CSS files? I want to avoid putting all the files in my classpath
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;
    }
}
Solution
The person who meets the same problem here is how I solve it
In my customuseragent, I only need to pass proxy object to access this agent
public class ResourceLoaderUserAgent extends ITextUserAgent {
public ResourceLoaderUserAgent(ITextOutputDevice outputDevice) {
    super(outputDevice);
}
protected InputStream resolveAndOpenStream(String uri) {    
    HttpURLConnection connection = null;
    URL proxyUrl = null;
    try {
        Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("localhost",3128));
        proxyUrl = new URL(uri);
        connection = (HttpURLConnection) proxyUrl.openConnection(proxy);
        connection.connect();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    java.io.InputStream is = null;
    try {
        is = connection.getInputStream();
    } catch (java.net.MalformedURLException e) {
        XRLog.exception("bad URL given: " + uri,e);
    } catch (java.io.FileNotFoundException e) {
        XRLog.exception("item at URI " + uri + " not found");
    } catch (java.io.IOException e) {
        XRLog.exception("IO problem for " + uri,e);
    }
    return is;
}
}
Cache:
resolving css took 74 ms resolving images took 225 ms
Not cached:
resolving css took 15466 ms resolving images took 11236 ms
As you can see, there is a big difference between cached and uncached resources
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        