如何从 itext7 中的类路径加载资源?

How to load resources from classpath in itext7?

如何使用 iText 7 从类路径加载图像、css 和字体等资源?

以前我使用 ITextRenderer 这样做:

ITextRenderer renderer = createITextRenderer();
renderer.setDocumentFromString(htmlContent, "classpath:/templates/");

我用 ConverterProperties 试过了,但没有用。

ConverterProperties properties = new ConverterProperties().setBaseUri("classpath:/templates/");
HtmlConverter.convertToPdf(htmlContent, outputStream, properties);

在 Spring 引导应用程序中,可以使用 TomcatURLStreamHandlerFactory 注册类路径协议处理程序。可以按照问题中的说明设置baseURI。

org.apache.catalina.webresources.TomcatURLStreamHandlerFactory.register();
ConverterProperties properties = new ConverterProperties().setBaseUri("classpath:/templates/");