我正在使用Flying Saucer API
withiText PDF
将 HTML 内容转换为 PDF。
这需要以下库:
- 核心渲染器.jar
- iText-2.0.8.jar
由于该库不支持input type checkbox
,所以我使用checkbox image
PDF 进行渲染。
但是,图像没有出现。它什么也没显示。
中的资源flyingsaucer-R8.zip
。
示例:
StringBuilder myHtml = new StringBuilder();
myHtml.append("<html><img src=\"images/invoice-bg.jpg\"></img></html>");
// if you have html source in hand, use it to generate document object
Document document = XMLResource.load( new ByteArrayInputStream(
myHtml.toString().getBytes() ) ).getDocument();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument( document, null );
renderer.layout();
String fileNameWithPath = "D:/Temp/PDF-XhtmlRendered.pdf";
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
renderer.createPDF( fos );
fos.close();
System.out.println( "File 1: '" + fileNameWithPath + "' created." );