maven引用
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<!-- 支持中文 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- 支持css样式渲染 -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.16</version>
</dependency>
<!-- 转换html为标准xhtml包 -->
<dependency>
<groupId>net.sf.jtidy</groupId>
<artifactId>jtidy</artifactId>
<version>r938</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.13.2</version>
</dependency>
Java代码
/**
* 导出PDF
* @param jsonStr
* @return
* @throws IOException
*/
@RequestMapping(value = "/exportPDF")
public ResponseEntity<byte[]> exportPDF(String jsonStr) throws IOException {
// 查询数据
JSONObject key = JSONObject.parseObject(jsonStr);
JSONObject jo = projectService.loadPVReport(key);
JSONArray ja = jo.getJSONArray("data");
String tempPath = this.getClass().getResource("/templates").toString().substring(6);
String templateFileName = tempPath + File.separator + "PVReportTemplate.xlsx";
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
String fileName = year + "年度工程项目_" + (int)(Math.random()*1000) + ".pdf";
String filePath = FileUtils.getFilePath("/temp") + "/" + fileName;
try {
StringBuffer html = new StringBuffer();
html.append("<!DOCTYPE html>");
html.append("<html lang=\"en\">").append("<head>").append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")
.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;} @page {size: 297mm 210mm;} th, td { border: 1px solid #000; font-size: 10px;} .numberTD { width: 70px; text-align: right;}</style>")
.append("</head>")
.append("<body>").append("<table style=\"width: 100%; border-collapse: collapse;\" cellspacing=\"0\" cellpadding=\"5\" border-collapse=\"collapse\">")
.append("<tr><th colspan=\"11\" style=\"font-size: 20px; text-align: center;\">").append(year).append("年度工程项目</th></tr>");
html.append("<tr><td colspan=\"11\">部室:").append(key.getString("deptName")).append("</td></tr>");
html.append("<tr><td colspan=\"11\" style=\"color: red;\">备注:1、只统计在建项目和中标未开工项目;2、未开工项目备注预计开工时间;3、运维项目备注合同起止日期;4、备注栏简要说明项目性质,如:“安防监控机电设备安装”、“收费系统运营维护”等。</td></tr>");
html.append("<tr><td colspan=\"2\" style=\"text-align: center; border: 0;\">制表:</td><td style=\"border: 0;\"></td><td style=\"border: 0;\"></td>");
html.append("<td colspan=\"2\" style=\"text-align: center; border: 0;\">复核(签字):</td><td style=\"border: 0;\"></td><td style=\"border: 0;\"></td>");
html.append("<td style=\"border: 0;\"></td><td style=\"border: 0;\"></td><td style=\"border: 0;\"></td></tr></table>");
html.append("</body></html>");
OutputStream os = new FileOutputStream(filePath);
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.setDocumentFromString(html.toString());
renderer.layout();
renderer.createPDF(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
InputStream is = new FileInputStream(filePath);
byte[] body = new byte[is.available()];
is.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode(fileName, "utf-8"));
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> entity = new ResponseEntity<>(body, headers, statusCode);
return entity;
}