把浏览器中的页面数据下载为pdf
![页面样子](https://img-blog.csdnimg.cn/7c0f58887b6c40fe8a7112800e9a8c93.png
下载后效果:(只是内容不一致而已,样式差不多)
前言
通过键值对的方式把数据写入document对象中,下载为pdf
其中本案例是无模板导出PDF,需要设置pdf的格式
一、aaaController
/**
* 法律法规下载
*/
@PostMapping("/downloadLaws")
public void downloadLaws( HttpServletResponse response){
Map<String, Object> map = new HashMap<>();
map.put("organ", "中国制定机关");
map.put("word", "中国的发文字号");
map.put("publicTime", "中国的公布日期");
map.put("startTime", "中国的实施日期");
map.put("ageing", "永久有效");
map.put("rank", "中国的效力位阶");
List<String> catalogList = new ArrayList<>();
catalogList.add("第一章 多品牌官微下架蔡徐坤相关内容");
catalogList.add("第二章 狗仔说不排除爆料有假");
catalogList.add("第三章 雀巢中国等多个品牌微博已清空蔡徐坤相关内容");
catalogList.add("第四章 媒体评蔡徐坤事件:仍有许多疑点");
catalogList.add("第五章 蔡徐坤工作室称已起诉造谣者");
List<String> lawsDetails = new ArrayList<>();
lawsDetails.add("这套中文题目包括单选和完形填空。看后,不少网友表示这题目难度不大,但总感觉哪里有点儿怪;也有网友称,原来做完型看的懂是这种感觉。");
lawsDetails.add("采访中,王阿姨的手里紧紧攥着一张照片,照片拍摄于1983年3月,王阿姨穿着唐装,抱着10个月大的女儿,笑得十分开心。这张照片,是她这辈子跟女儿的唯一一张合照:“女儿1982年出生的,当时爸爸在服刑,我把孩子抱去他服刑的地方,看了一次就永远分别了。 ”");
lawsDetails.add("第一条");
lawsDetails.add("第一条");
lawsDetails.add("第一条");
docUtil.generatePDFDoc("aaaa", response, map, catalogList, lawsDetails);
}
二、docUtil
public class docUtil {
/**
*
* @param value 文本
* @param font 字体
* @param horizontalAlignment 水平样式 0-left, 1-center, 2-right
* @param verticalAlignment 垂直样式 4-top, 5-middle, 6-bottom;
* @param colspan 列合并
* @param rowspan 行合并
* @param borderSide 外边框
* 0-默认
* 1-隐藏上边框
* 2-隐藏下边框
* 3-隐藏上、下边框
* 4-隐藏左边框
* 5-隐藏左、上边框
* 6-隐藏左、下边框
* 7-隐藏左、上、下边框
* 8-隐藏右边框
* 9-隐藏右、上边框
* 10-隐藏右、下边框
* 11-隐藏右、上、下边框
* 12-隐藏左、右边框
* 13-隐藏上、左、右边框
* 14-隐藏下、左、右边框
* 15-隐藏全部
* @return
*/
public static PdfPCell createCell(String value, Font font, int horizontalAlignment, int verticalAlignment, int colspan, int rowspan, int borderSide) {
PdfPCell cell = new PdfPCell();
cell.setPhrase(new Phrase(value, font));
//水平居中
cell.setHorizontalAlignment(horizontalAlignment);
if(verticalAlignment>0){
//垂直居中
cell.setUseAscender(true);
}
//垂直居中
cell.setVerticalAlignment(verticalAlignment);
if(colspan>0 ){
cell.setColspan(colspan);
}
if(rowspan>0){
cell.setRowspan(rowspan);
}
if(borderSide>0){
cell.disableBorderSide(borderSide);
}
return cell;
}
/**
* 添加段落 自动换行
*/
public static void addTextParagraph(Document document, String text,Font font) throws DocumentException {
Paragraph paragraph = new Paragraph(text,font);
paragraph.setAlignment(0); //设置文字居中 0靠左 1,居中 2,靠右
paragraph.setIndentationLeft(12); //设置左缩进
paragraph.setIndentationRight(12); //设置右缩进
paragraph.setFirstLineIndent(32); //设置首行缩进
paragraph.setLeading(30f); //行间距
document.add(paragraph);
}
public static void addNoBorderCell(PdfPTable table,String text,Font font) {
PdfPCell cell = new PdfPCell(new Paragraph(text,font));
cell.setBorderWidth(0);
cell.setLeading(24,0); //行间距
table.addCell(cell);
}
public static PdfPTable createTable() throws DocumentException {
PdfPTable table = new PdfPTable(2);
// 设置总宽度
table.setTotalWidth(490);
table.setLockedWidth(true);
// 设置每一列宽度
table.setTotalWidth(new float[] {240,240});
table.setLockedWidth(true);
return table;
}
/**
* 无模板导出PDF
* @param fileName 导出文件名
* @param response 返回响应
* @param data 要打包的数据
*/
public static void generatePDFDoc(String fileName, HttpServletResponse response, Map<String, Object> data, List<String> catalogList,List<String> lawsDetails){
try{
fileName = URLEncoder.encode(fileName, "UTF-8");
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}
// 设置响应头
response.setContentType("application/force-download");
response.setHeader("Content-Disposition","attachment;fileName=" + fileName + ".pdf");
OutputStream out = null;
Document doc = new Document(PageSize.A4);
try {
out = response.getOutputStream();
PdfWriter writer = PdfWriter.getInstance(doc, out);
doc.open();
doc = makeSinglePDF(doc,data,catalogList,lawsDetails);
}catch (Exception e){
e.printStackTrace();
}finally {
// 关闭文档
if(doc!=null){
doc.close();
}
try {
if (out != null) {
out.flush();
out.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
/**
* 生成单个PDF文档
* @param data 数据键值对,根据实际情况修改键名称
* @return
*/
private static Document makeSinglePDF(Document doc,Map<String, Object> data,List<String> catalogList,List<String> lawsDetails){
try {
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 12, Font.NORMAL, BaseColor.BLACK);
Font fontTile = new Font(bf, 14, Font.BOLD, BaseColor.BLACK);
Font fontTwoTile = new Font(bf, 12, Font.BOLD, BaseColor.BLACK);
/**
* 标题
*/
Paragraph paragraph1 = new Paragraph((String) data.get("name"), fontTile);
paragraph1.setAlignment(PdfPCell.ALIGN_CENTER);
//设置行间距
paragraph1.setLeading(15f);
//设置段落下空白
paragraph1.setSpacingAfter(20f);
PdfPTable table =createTable();
addNoBorderCell(table, StringUtils.join("制定机关:",data.getOrDefault("organ"," ")),font);
addNoBorderCell(table, StringUtils.join("",""),font);
addNoBorderCell(table, StringUtils.join("发文字号:",data.getOrDefault("word"," ")),font);
addNoBorderCell(table, StringUtils.join("",""),font);
addNoBorderCell(table, StringUtils.join("公布日期:",data.getOrDefault("publicTime"," ")),font);
addNoBorderCell(table, StringUtils.join("施行日期:",data.getOrDefault("startTime"," ")),font);
addNoBorderCell(table, StringUtils.join("时效性:",data.getOrDefault("ageing"," ")),font);
addNoBorderCell(table, StringUtils.join("效力位阶:",data.getOrDefault("rank"," ")),font);
addNoBorderCell(table, StringUtils.join("",""),font);
/**
* 名称
*/
Paragraph paragraph2 = new Paragraph((String) data.get("name"), font);
paragraph2.setAlignment(PdfPCell.ALIGN_CENTER);
//设置行间距
paragraph2.setLeading(50f);
/**
* 解释
*/
Paragraph paragraph3 = new Paragraph((String) data.get("reason"), font);
paragraph3.setAlignment(PdfPCell.ALIGN_CENTER);
//设置行间距
paragraph3.setLeading(30f);
doc.add(paragraph1);
doc.add(table);
doc.add(paragraph2);
doc.add(paragraph3);
/**
* 目录
*/
if (ObjectUtil.isNotEmpty(catalogList)){
Paragraph paragraph4 = new Paragraph("目录", font);
paragraph4.setAlignment(PdfPCell.ALIGN_CENTER);
//设置行间距
paragraph4.setLeading(30f);
doc.add(paragraph4);
}
/**
* 目录内容
*/
PdfPTable tbText2 = new PdfPTable(new float[] { 30f});
Map<Integer, String> map = new HashMap<>();
Integer i=0;
Integer j=0;
for (String catalog : catalogList) {
map.put(i,catalog);
tbText2.addCell(createCell(catalog == null ? " " :catalog, font,
PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_LEFT, 0,0,15));
i++;
}
doc.add(tbText2);
/**
* 法律法规内容
*/
for (String lawsDetail : lawsDetails) {
Paragraph paragraph5 = new Paragraph(map.get(j), fontTwoTile);
j++;
paragraph5.setAlignment(PdfPCell.ALIGN_CENTER);
doc.add(paragraph5);
addTextParagraph(doc,lawsDetail,font);
}
}catch (Exception e){
e.printStackTrace();
}
return doc;
}
}