1. flying-saucer-pdf自动引入了itext2.1.7 有漏洞
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.6</version>
</dependency>
2. flying-saucer-pdf-itext5引入了itext5.1.12
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.22</version>
</dependency>
POM.XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>freemarker-page-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version> 2.2.2.RELEASE</version>
</parent>
<dependencies>
<!--web组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok简化实体类的get set方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- itextpdf,导出pdf核心架包 -->
<!-- <dependency>-->
<!-- <groupId>com.itextpdf</groupId>-->
<!-- <artifactId>itextpdf</artifactId>-->
<!-- <version>5.5.12</version>-->
<!-- </dependency>-->
<!-- itextpdf工具包,用来解析html生成pdf -->
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>
<!-- flying saucer,支持对CSS高级特性的解析 -->
<!-- <dependency>-->
<!-- <groupId>org.xhtmlrenderer</groupId>-->
<!-- <artifactId>flying-saucer-pdf</artifactId>-->
<!-- <version>9.1.6</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf-itext5 -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.22</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- 支持css样式渲染 -->
<!-- <dependency>-->
<!-- <groupId>org.xhtmlrenderer</groupId>-->
<!-- <artifactId>flying-saucer-pdf-itext5</artifactId>-->
<!-- <version>9.0.3</version>-->
<!-- </dependency>-->
<!-- 支持中文 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--<dependency>
<groupId>commons.codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.16</version>
</dependency>-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
</dependencies>
<build>
<finalName>itextpdf</finalName>
</build>
</project>
CreatePdf1()
/**
* html转pdf。html 最大尺寸790,1120
* @param html
* @throws Exception
*/
public static byte[] htmlToPdf(String html) throws Exception{
ByteArrayOutputStream os = null;
try {
os = new ByteArrayOutputStream();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
// 解决中文问题
//Arial Unicode MS SimSun Microsoft YaHei
String freemarkerFontPath = "/home/weblogic/data/web/sfs/freemarker";
if (StringUtils.isBlank(freemarkerFontPath)) {
logger.error("freemarkerFontPath is blank!");
return null;
}
String msyhPath = "D:\\gitee\\font\\MSYH.TTF";
File msyhFile = new File(msyhPath);
if (!msyhFile.exists() || !FileUtil.isReadable(msyhFile)) {
logger.error("MSYH.TTF does not exist or permission denied !");
return null;
}
String msyhbdPath = "D:\\gitee\\font\\MSYHBD.TTF";
File msyhbdFile = new File(msyhbdPath);
if (!msyhbdFile.exists() || !FileUtil.isReadable(msyhbdFile)) {
logger.error("MSYHBD.TTF does not exist or permission denied !");
return null;
}
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont(msyhPath, BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
fontResolver.addFont(msyhbdPath, BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
//renderer.getSharedContext().setBaseURL("http://192.168.0.138:8081/saferycom/images/");
renderer.layout();
renderer.createPDF(os);
logger.info("转换成功!");
return os.toByteArray();
} catch(Exception e) {
logger.error("", e);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
logger.error("", e);
}
}
}
return null;
}
createPdf2()
public static void createPdf(String content,String dest) throws IOException, DocumentException {
ITextRenderer render = new ITextRenderer();
//设置字体
ITextFontResolver fontResolver = render.getFontResolver();
// fontResolver.addFont(FONT_S, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// fontResolver.addFont(FONT_C, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
fontResolver.addFont(FONT_S, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont(FONT_C, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 解析html生成pdf
render.setDocumentFromString(content);
render.layout();
render.createPDF(new FileOutputStream(dest));
render.finishPDF();
}