maven配置## 标题
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.12</version>
</dependency>
pdf工具类## 标题
package cn.prop.fm.pdf;
import com.itextpdf.text.pdf.BaseFont;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.List;
import java.util.Map;
/**
* 功能:pdf处理工具类
*
* @author qust
* @version 1.0 2018/2/23 17:21
*/
public class PdfUtils {
private PdfUtils() {
}
private static final Logger LOGGER = LoggerFactory.getLogger(PdfUtils.class);
/**
* 按模板和参数生成html字符串,再转换为flying-saucer识别的Document
*
* @param templateName freemarker模板名称
* @param variables freemarker模板参数
* @return Document
*/
private static Document generateDoc(FreeMarkerConfigurer configurer, String templateName, Map<String, Object> variables) {
Template tp;
try {
tp = configurer.getConfiguration().getTemplate(templateName);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
return null;
}
StringWriter stringWriter = new StringWriter();
try (BufferedWriter writer = new BufferedWriter(stringWriter)) {
try {
tp.process(variables, writer);
writer.flush();
} catch (TemplateException e) {
LOGGER.error("模板不存在或者路径错误", e);
} catch (IOException e) {
LOGGER.error("IO异常", e);
}
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return builder.parse(new ByteArrayInputStream(stringWriter.toString().getBytes()));
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
return null;
}
}
/**
* 核心: 根据freemarker模板生成pdf文档
*
* @param configurer freemarker配置
* @param templateName freemarker模板名称
* @param out 输出流
* @param listVars freemarker模板参数
* @throws Exception 模板无法找到、模板语法错误、IO异常
*/
private static void generateAll(FreeMarkerConfigurer configurer, String templateName, OutputStream out, List<Map<String, Object>> listVars) throws Exception {
if (CollectionUtils.isEmpty(listVars)) {
LOGGER.warn("警告:freemarker模板参数为空!");
return;
}
ITextRenderer renderer = new ITextRenderer();
Document doc = generateDoc(configurer, templateName, listVars.get(0));
renderer.setDocument(doc, null);
//设置字符集(宋体),此处必须与模板中的<body style="font-family: SimSun">一致,区分大小写,不能写成汉字"宋体"
//设置字符集(宋体),此处必须与模板中的<body style="font-family: SimSun">一致,区分大小写,不能写成汉字"宋体"
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//展现和输出pdf
renderer.layout();
renderer.createPDF(out, false);
//根据参数集个数循环调用模板,追加到同一个pdf文档中
//(注意:此处从1开始,因为第0是创建pdf,从1往后则向pdf中追加内容)
for (int i = 1; i < listVars.size(); i++) {
Document docAppend = generateDoc(configurer, templateName, listVars.get(i));
renderer.setDocument(docAppend, null);
renderer.layout();
renderer.writeNextDocument(); //写下一个pdf页面
}
renderer.finishPDF(); //完成pdf写入
}
/**
* pdf下载
*
* @param configurer freemarker配置
* @param templateName freemarker模板名称(带后缀.ftl)
* @param listVars 模板参数集
* @param response HttpServletResponse
* @param fileName 下载文件名称(带文件扩展名后缀)
*/
public static void download(FreeMarkerConfigurer configurer, String templateName, List<Map<String, Object>> listVars, HttpServletResponse response, String fileName) {
// 设置编码、文件ContentType类型、文件头、下载文件名
response.setCharacterEncoding("utf-8");
response.setContentType("application/force-download");
try {
response.setHeader("Content-Disposition", "attachment;fileName=" +
new String(fileName.getBytes("gb2312"), "ISO8859-1"));
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage(), e);
}
try (ServletOutputStream out = response.getOutputStream()) {
generateAll(configurer, templateName, out, listVars);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
/**
* pdf预览
*
* @param configurer freemarker配置
* @param templateName freemarker模板名称(带后缀.ftl)
* @param listVars 模板参数集
* @param response HttpServletResponse
*/
public static void preview(FreeMarkerConfigurer configurer, String templateName, List<Map<String, Object>> listVars, HttpServletResponse response) {
try (ServletOutputStream out = response.getOutputStream()) {
generateAll(configurer, templateName, out, listVars);
out.flush();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}
controller层## 标题
package cn.prop.fm.pdf;
import cn.prop.fm.vo.MerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
/**
* 功能:pdf预览、下载
*
*/
@Controller
@RequestMapping(value = "/pdf")
public class PdfController {
@Autowired
private FreeMarkerConfigurer configurer;
/**
* pdf预览
*
* @param request HttpServletRequest
* @param response HttpServletResponse
*/
@RequestMapping(value = "/preview", method = RequestMethod.GET)
public void preview(HttpServletRequest request, HttpServletResponse response) {
// 构造freemarker模板引擎参数,listVars.size()个数对应pdf页数
List<Map<String, Object>> listVars = new ArrayList<>();
List<MerVO> a = new LinkedList<>();
MerVO s = new MerVO();
s.setMerId("1");
s.setMerName("1wewe");
MerVO b = new MerVO();
b.setMerId("1");
b.setMerName("1wewe");
a.add(s);
a.add(b);
Map<String, Object> variables = new HashMap<>();
variables.put("title", "测试预览ASGX!");
variables.put("list", a);
listVars.add(variables);
PdfUtils.preview(configurer, "pdfPage.ftl", listVars, response);
}
/**
* pdf下载
*
* @param request HttpServletRequest
* @param response HttpServletResponse
*/
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response) {
List<Map<String, Object>> listVars = new ArrayList<>();
Map<String, Object> variables = new HashMap<>();
variables.put("title", "测试下载ASGX!");
listVars.add(variables);
PdfUtils.download(configurer, "pdfPage.ftl", listVars, response, "测试中文.pdf");
}
}
ftl模板## 标题
提示:样式需要更加实际情况调整,与html的页面布局有差异
<!DOCTYPE html>
<html>
<head lang="en">
<title>Spring Boot Demo - PDF</title>
<style>
margin:0,0;
body{ text-align:center}
#divcss{margin:0 auto;width:100%;height:50%;}
table {
border-collapse: collapse;text-align:center;
}
table, td, th {
border: 1px solid black;
}
</style>
</head>
<body style="font-family: 'SimSun'">
<div id="divcss">
<div style="text-align:center">
<h2>物业名</h2>
<h3>202006账单结算通知单</h3>
</div>
<div style="">
<div style="width:50%;height: 100%;float:left;">
<span>楼号/房号:1栋-2楼-201</span>
<br></br>
<span>租户名:孙悟空</span>
</div>
<div style="height: 100%;float:right">
<p>账单周期:2020-06-01至2020-06-261</p>
</div>
</div>
<div style="clear: both">
<table border="1" style=" width: 100%">
<tr>
<th>抄表费用</th>
<th>表计表号</th>
<th>上期读数</th>
<th>本期读数</th>
<th>总用电量</th>
<th>费用金额</th>
</tr>
<tr>
<td>电费</td>
<td>000000001221</td>
<td>2020-03-31</td>
<td>2020-03-31</td>
<td>50.84</td>
<td>50.84</td>
</tr>
<tr>
<td colspan="6">其它费用项</td>
</tr>
<tr>
<td colspan="6">测试固定收费:¥100 </td>
</tr>
</table>
</div>
<div style="border-bottom: 1px solid;border-left: 1px solid;border-right: 1px solid;height: 140px;text-align: center;margin-top: -18px;">
<h3>此处为支付方式区域</h3>
<span style="
position: relative;
top: 30px;
">温馨提示:请贵公司及时缴清费用,以免逾期后自动断电影响正常用电,入有疑问,请致电服务热线。</span>
</div>
<div>
<span style="margin-right: 8%;">制表人:车小明是的</span>
<span > 打印日期:2020-08-23 09:23</span>
</div>
</div>
</body>
</html>