java - XHTML 到 PDF 使用 Flying Saucer 如何缓存 css

标签 java xhtml pdf-generation itext flying-saucer

在我的生产流程中,我需要从 HTML 生成几百个 PDF。对于这种情况,我首先将 HTML 转换为 XHTML。 比我将“清理过的”XHTML 和 uri 传递给渲染器。

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

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;
    }
}

最佳答案

如果有人在这里遇到同样的问题,我就是这样解决的。 由于我无法在我的 CustomUserAgent 中缓存 *.css 文件,我不得不寻找另一种方法。我的解决方案使用 Squid作为 http 代理来缓存所有常用资源。

在我的 CustomUserAgent 中,我只需要通过传递代理对象来访问这个代理。

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;
}
}

缓存:

resolving css took 74 ms
resolving images took 225 ms

未缓存:

resolving css took 15466 ms
resolving images took 11236 ms

如您所见,缓存资源和未缓存资源之间的差异非常显着

关于java - XHTML 到 PDF 使用 Flying Saucer 如何缓存 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11633236/

相关文章:

html - 为什么 Opera 将我的网页解析为 XML?

java - Jasper 报告 PDF 不带汉字

objective-c - 生成 PDF 表格

java - 传递参数与从函数返回参数

java - 如何实现 flyway-play Java 迁移

java - Android 服务被 android V 2.3 ( Gingerbread ) 杀死

ios - 使用 UIKit/Coregraphics 渲染为 PDF 时图像模糊

java - 如何让用户选择一个音频文件并用 Java 播放它

excel - Excel有语法高亮器吗

html - 如何在支持 IE 6 的纯 CSS 中删除最后一个 li 的边距?