java – XHTML到PDF使用fly-saucer如何缓存css

前端之家收集整理的这篇文章主要介绍了java – XHTML到PDF使用fly-saucer如何缓存css前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的生产管道中,我需要从 HTML生成几百个PDF.对于这种情况,我首先将HTML转换为XHTML.
比我将’已清理’的XHTML和uri传递给渲染器.

由于* .css和imageFiles对于所有XHTML文件都是相同的,所以我不需要在处理文件解决它们.
我成功使用以下代码缓存图像.如何缓存.css文件呢?我想避免将所有文件放入我的类路径中.

  1. ITextRenderer renderer = new ITextRenderer();
  2.  
  3. ResourceLoaderUserAgent callback = new ResourceLoaderUserAgent(renderer.getOutputDevice());
  4. callback.setSharedContext(renderer.getSharedContext());
  5.  
  6. for (MyObject myObject : myObjectList) {
  7.  
  8. OutputStream os = new FileOutputStream(tempFile);
  9.  
  10. final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  11. documentBuilderFactory.setValidating(false);
  12. DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
  13. org.w3c.dom.Document document = builder.parse(myObject.getLocalPath); // full path to .xhtml
  14.  
  15. renderer.getSharedContext().setUserAgentCallback(callback);
  16.  
  17. renderer.setDocument(document,myObject.getUri());
  18. renderer.layout();
  19. renderer.createPDF(os);
  20.  
  21. os.flush();
  22. os.close();
  23. }
  24. ...
  25.  
  26.  
  27. private static class ResourceLoaderUserAgent extends ITextUserAgent
  28. {
  29. public ResourceLoaderUserAgent(ITextOutputDevice outputDevice) {
  30. super(outputDevice);
  31. }
  32.  
  33. protected InputStream resolveAndOpenStream(String uri) {
  34. InputStream is = super.resolveAndOpenStream(uri);
  35. System.out.println("IN resolveAndOpenStream() " + uri);
  36.  
  37. return is;
  38. }
  39. }

解决方法

在这里遇到同样问题的人是我如何解决它.
由于我无法在我的CustomUserAgent中缓存* .css文件,我必须找到另一种方法.我的解决方案使用 Squid作为http-proxy来缓存所有常用资源.

在我的CustomUserAgent中,我只需要通过传递proxy-object来访问此代理.

  1. public class ResourceLoaderUserAgent extends ITextUserAgent {
  2.  
  3. public ResourceLoaderUserAgent(ITextOutputDevice outputDevice) {
  4. super(outputDevice);
  5. }
  6.  
  7. protected InputStream resolveAndOpenStream(String uri) {
  8.  
  9. HttpURLConnection connection = null;
  10. URL proxyUrl = null;
  11. try {
  12. Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("localhost",3128));
  13. proxyUrl = new URL(uri);
  14. connection = (HttpURLConnection) proxyUrl.openConnection(proxy);
  15. connection.connect();
  16.  
  17. } catch (Exception e) {
  18. throw new RuntimeException(e);
  19. }
  20.  
  21. java.io.InputStream is = null;
  22. try {
  23. is = connection.getInputStream();
  24. } catch (java.net.MalformedURLException e) {
  25. XRLog.exception("bad URL given: " + uri,e);
  26. } catch (java.io.FileNotFoundException e) {
  27. XRLog.exception("item at URI " + uri + " not found");
  28. } catch (java.io.IOException e) {
  29. XRLog.exception("IO problem for " + uri,e);
  30. }
  31.  
  32. return is;
  33. }
  34. }

缓存:

  1. resolving css took 74 ms
  2. resolving images took 225 ms

未缓存:

  1. resolving css took 15466 ms
  2. resolving images took 11236 ms

如您所见,缓存和未缓存资源之间的差异很大

猜你在找的Java相关文章