使用https资源生成pdf(配合Flying Saucer/itext)

Use https ressources to generate pdf (with Flying Saucer / itext)

我想使用外部资源(jpg,css)从 html 创建一个 pdf。

这是用于生成 pdf 文件对象的代码,带有 Flying Saucer 和 itext:

ITextRenderer iTextRenderer = new ITextRenderer();
iTextRenderer.setDocumentFromString(xhtmlString);
iTextRenderer.layout();

File file = new File("test.pdf");
FileOutputStream os = new FileOutputStream(file);
iTextRenderer.createPDF(os);
os.close();

不幸的是,所有与 https 链接的资源都被忽略,而 http 资源工作正常。

编辑:

我扩展了 ITextUserAgent 来拦截 https 调用资源,但我得到了这个错误:

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

所以我需要有一个证书才能获取这些 https 资源。

Here和我完全一样的问题

Here 是 link 到 java 从 HTTPS 服务器加载数据的本机方式。

现在,您正在使用 Flying Saucer 的 XML 解析实现,它在其文档中似乎没有提及太多 HTTPS 支持。我会使用上面的解决方法,然后将 DOM 直接传递给渲染器。