FlyingSaucer:将HTML文档转换为PDF而忽略外部CSS?

时间:2011-03-14 20:13:42

标签: html css pdf groovy flying-saucer

我正在使用以下内容将HTML转换为PDF:

InputStream convert(InputStream fileInputStream) {

        PipedInputStream inputStream = new PipedInputStream()
        PipedOutputStream outputStream = new PipedOutputStream(inputStream)
        new Thread({
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(false);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(fileInputStream)

            ITextRenderer renderer = new ITextRenderer()
            renderer.setDocument(document, "")
             renderer.layout()
            renderer.createPDF(outputStream)
        }).start()

        return inputStream
    }

从文档中,显然我应该可以在某处设置“用户代理”解析器,但我不确定在哪里。任何人都知道如何忽略文档中的外部CSS?

1 个答案:

答案 0 :(得分:1)

不一样的问题,但我对这个问题的回答也适用于此:Resolving protected resources with Flying Saucer (ITextRenderer)

覆盖此方法:

public CSSResource getCSSResource(String uri) {
    return new CSSResource(resolveAndOpenStream(uri));
}

public CSSResource getCSSResource(String uri) {
    return new CSSResource(new ByteArrayInputStream([] as byte[]));
}