我在 XMLWorker 中使用一个非常简单的 CSS 将 HTML 渲染为 PDF。这是CSS:
body { font-family: "Times New Roman;" }
p.testoJustify {
text-align:justify;
text-justify:inter-word;
}
问题出在“.testoJustify”选择器上。当我使用这个选择器时,生成的 PDF 没有对齐文本,而没有选择器,所以只有 p {...},它工作正常!
java代码如下:
FontFactory.registerDirectories();
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider();
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
CSSResolver cssResolver =XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
cssResolver.addCss(Context.pdfCss, true);
Pipeline<?> pipeline = new CssResolverPipeline(cssResolver,
new HtmlPipeline(htmlContext,
new PdfWriterPipeline(pdfDocument, pdfWriter)));
这是指向 JSFiddle 的链接,显示 CSS 已更正应用于 HTML 页面,但不是 XMLWorker: