pom文件
<flying-saucer-pdf.version>9.1.18</flying-saucer-pdf.version> <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>${flying-saucer-pdf.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
pdfutils
package com.ys.utils;
import com.lowagie.text.pdf.BaseFont;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.FileTemplateResolver;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PdfUtils {
public static void method() throws Exception{
// String path = request.getSession().getServletContext().getRealPath("");
// String filePath = path + env.getProperty("outputPath") + File.separator + UUIDUtil.getShortUuid() + ".pdf";
OutputStream out = new FileOutputStream(new File("F:/sss.pdf"));
Map<String, Object> locker = new HashMap<>();
locker.put("lockerName", "lockerName");
locker.put("exportDate", "exportDate");
locker.put("paymentModeName", "paymentModeName");
locker.put("paymentMothod", "paymentMothod");
locker.put("paymentTotal", "paymentTotal");
locker.put("boxSizeName", "boxSizeName");
locker.put("boxSize", "boxSize");
locker.put("boxSizeTotal", "boxSizeTotal");
locker.put("locationName", "locationName");
locker.put("location", "location");
locker.put("locationTotal", "locationTotal");
Map<String, Object> paymentList = new HashMap<>();
List<Map> list = new ArrayList<>();
paymentList.put("paymentMothod", "Alipay");
paymentList.put("paymentTotal", 90.0);
list.add(paymentList);
Map<String, Object> boxSizeList = new HashMap<>();
List<Map> boxlist = new ArrayList<>();
boxSizeList.put("boxSize", "smaller");
boxSizeList.put("boxSizeTotal", 30.0);
boxlist.add(boxSizeList);
Map<String, Object> locationList = new HashMap<>();
List<Map> locationlist = new ArrayList<>();
locationList.put("location", "New york");
locationList.put("locationTotal", 20.0);
locationlist.add(locationList);
Map<String, Object> map = new HashMap<>();
map.put("locker", locker);
map.put("paymentList", list);
map.put("boxSizeList", boxlist);
map.put("locationList", locationlist);
// String fileTemplatePath = path + env.getProperty("templatePath") + File.separator;
String htmlStr = generate("F:/project/sourceysProject/pdfModel/", "LockerModel", map);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(htmlStr.getBytes("UTF-8")));
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("F:/project/sourceysProject/pdfModel/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// fontResolver.addFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.setDocument(doc, null);
renderer.layout();
renderer.createPDF(out);
out.close();
// 下载文件
String fileName = "test.pdf";
// ExcelUtil.downloadFile(request, response, fileName, filePath);
}
public static String generate(String filePath, String template, Map<String, Object> variables) throws Exception{
//创建模板解析器
FileTemplateResolver templateResolver = new FileTemplateResolver();
templateResolver.setPrefix(filePath);
templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setTemplateMode("XHTML");
templateResolver.setCacheTTLMs(Long.valueOf(3600000L));
templateResolver.setCacheable(true);
//创建模板引擎并初始化解析器
TemplateEngine engine = new TemplateEngine();
engine.setTemplateResolver(templateResolver);
engine.isInitialized();
//输出流
StringWriter stringWriter = new StringWriter();
BufferedWriter writer = new BufferedWriter(stringWriter);
//获取上下文
Context ctx = new Context();
ctx.setVariables(variables);
engine.process(template, ctx, writer);
stringWriter.flush();
stringWriter.close();
writer.flush();
writer.close();
//输出html
String htmlStr = stringWriter.toString();
return htmlStr;
}
public static void main(String args[]) throws Exception {
method();
// String Str = generate("d:\\","test",new HashMap<String, Object>());
// System.out.println(Str);
}
}
html模板
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf!!!!!</title>
<meta http-equiv="Content-Type" content="text/html;" charset="UTF-8"/>
<style type="text/css">
@page {
size: 210mm 297mm; /*设置纸张大小:A4(210mm 297mm)、A3(297mm 420mm) 横向则反过来*/
margin: 0.25in;
padding: 1em;
@bottom-center{
content:"";
font-family: SimSun;
font-size: 12px;
color: red;
};
@top-center { content: element(header) };
@bottom-right{
content:"第" counter(page) "页 共 " counter(pages) "页";
font-family: SimSun;
font-size: 12px;
color:#000;
};
}
body {
font-family: 'SimSun'
}
h2 {
color: #0C0C0C;
}
table, th, td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
font-size: 12px;
color: #0C0C0C;
}
</style>
</head>
<body>
<div style="text-align: center;"><h2 th:text="${locker.lockerName}"></h2></div>
<table>
<tbody>
<tr>
<td colspan="10" style="background-color: #FFFF99; text-align: left; width: 190mm" th:text="${locker.exportDate}"></td>
</tr>
<tr>
<td colspan="10" style="background-color: #FFFF99; text-align: left" th:text="${locker.paymentModeName}"></td>
</tr>
<tr>
<th colspan="5" th:text="${locker.paymentMothod}"></th>
<th colspan="5" th:text="${locker.paymentTotal}"></th>
</tr>
<tr th:each="cabinet:${paymentList}">
<th colspan="5" th:text="${cabinet.paymentMothod}"></th>
<th colspan="5" th:text="${cabinet.paymentTotal}"></th>
</tr>
<tr>
<td colspan="10" style="background-color: #FFFF99; text-align: left" th:text="${locker.boxSizeName}"></td>
</tr>
<tr>
<th colspan="5" th:text="${locker.boxSize}"></th>
<th colspan="5" th:text="${locker.boxSizeTotal}"></th>
</tr>
<tr th:each="boxSize:${boxSizeList}">
<th colspan="5" th:text="${boxSize.boxSize}"></th>
<th colspan="5" th:text="${boxSize.boxSizeTotal}"></th>
</tr>
<tr>
<td colspan="10" style="background-color: #FFFF99; text-align: left" th:text="${locker.locationName}"></td>
</tr>
<tr>
<th colspan="5" th:text="${locker.location}"></th>
<th colspan="5" th:text="${locker.locationTotal}"></th>
</tr>
<tr th:each="location:${locationList}">
<th colspan="5" th:text="${location.location}"></th>
<th colspan="5" th:text="${location.locationTotal}"></th>
</tr>
</tbody>
</table>
</body>
</html>