用于通过模板生成PDF,在项目中生成个人授权协议函、个人电子保单、流水报表,数据报表等,将HTML静态模板写出来后,将数据替换成动态数据即可。
<!-- html2pdf -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.16</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<!-- docx4j docx2pdf -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>6.1.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>6.0.0</version>
</dependency>
templates.ftl 放入resource/templates文件目录中
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Title</title>
<style>
@page {
size: 210mm 290mm; /* size: 210mm 297mm; 设置纸张大小:A4(210mm 297mm)、A3(297mm 420mm) 横向则反过来 */
/*margin-bottom: 1cm;*/
/*padding: 1em;*/
/*页眉*/
/*@top-center {*/
/* content: counter(page);*/
/* font-family: SimSun;*/
/* font-size: 15px;*/
/* color:#000;*/
/*};*/
/*@top-right{*/
/* background: url("https://dpxdata.oss-cn-beijing.aliyuncs.com/upload/kbt/null_1660715685801.png") top right no-repeat;*/
/* width: 195px;height: 50px;*/
/* content: "logo";*/
/* font-family: SimSun;*/
/* font-size: 15px;*/
/* !*color:#000;*!*/
/*};*/
/*页脚*/
@bottom-center{
content: counter(page);
font-family: SimSun;
font-size: 15px;
color:#000;
};
/*@bottom-right{*/
/* content: "第" counter(page) "页 共" counter(pages) "页";*/
/* font-family: SimSun;*/
/* font-size: 15px;*/
/* color:#000;*/
/*};*/
}
* {
page-break-inside: always;
}
</style>
</head>
<body style="font-family: SimSun; font-size: 14px">
<div style="">
${text!''}
</div>
</body>
</html>
simsun.ttc 为字体文件
1.HTML 生成 PDF
HttpHeaders 写法
@GetMapping("/pdf")
public ResponseEntity<?> export(HttpServletResponse response) {
try {
HttpHeaders headers = new HttpHeaders();
Map<String, Object> dataMap = new HashMap<>(16);
dataMap.put("text", "张三");
String htmlStr = FreeMarkUtils.freemarkerRender(dataMap, "templates.ftl");
byte[] pdfBytes = FreeMarkUtils.createPDF(htmlStr, "simsun.ttc");
if (pdfBytes != null && pdfBytes.length > 0) {
String fileName = System.currentTimeMillis() + (int) (Math.random() * 90000 + 10000) + ".pdf";
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(pdfBytes, headers, HttpStatus.OK);
}
} catch (Exception e) {
e.printStackTrace();
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return new ResponseEntity<>("{ \"code\" : \"404\", \"message\" : \"not found\" }", headers, HttpStatus.NOT_FOUND);
}
Response 写法
@GetMapping("/pdf")
public void export(HttpServletResponse response) {