flying-saucer : An XML/XHTML/CSS 2.1 Renderer. Flying Saucer is an XML/CSS renderer, which means it takes XML files as input, applies formatting and styling using CSS, and generates a rendered representation of that XML as output. The output may go to the screen (in a GUI), to an image, or to a PDF file. Because we believe most people will be interested in re-using their knowledge of web layout, our main target for content is XHTML 1.0 (strict), an XML document format that standardizes HTML.
前言
flying-saucer是HTML转PDF库中比较优质的一个,虽然目前处于不活跃状态,但功能已能满足常规需要。最大的优势,是对css2.1的支持,页面渲染效果很好。
源码结构
- flying-saucer-core: 将html css和元素解析出来,这一层与iText无关,解析后的中间层可以与任何一个UI绘制系统对接。
- flying-saucer-pdf-itext5:将core解析出来的对象转为PDF
基本流程
把html转为PDF的基本流程:
// 针对多个html片段
List<String> htmlList;
for(String html: htmlList) {
org.jsoup.nodes.Document doc = Jsoup.parse(htmlContent);
org.w3c.dom.Document document = DOMBuilder.jsoup2DOM(doc);
renderer.setDocument(document, baseUrl);
renderer.layout();
renderer.createPDF();
}
renderer.finishPDF();
类
- ITextOutputDevice
一些问题
- addFont()方法就不支持从Jar包中获取资源文件