1、pom文件首先要引用
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>1.0.2</version>
</dependency>
2、String html = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\"/>\n" +
" <title>标题</title>\n" +
"</head>\n" +
"<style>\n" +
" *{\n" +
" font-family: KaiTi_GB2312;\n" +
" }\n" +
"</style>\n" +
"<body>\n" +
" 这是带有中文的html格式内容hello world! <font color=\"red\">XMLWorkerHelper</font> [这是带有中文的html格式内容]\n" +
"</body>\n" +
"</html>";
ConverterProperties props = new ConverterProperties();
DefaultFontProvider defaultFontProvider = new DefaultFontProvider(false, false, false);
defaultFontProvider.addDirectory("src/main/resources/fonts");
props.setFontProvider(defaultFontProvider);
HtmlConverter.convertToPdf(html, new FileOutputStream("d://testPdf.pdf"), props);