后端
/**
* 导出PDF
* @param physicalExamination
* @param response
* @throws Exception
*/
@GetMapping("/exportPDF")
public void exportPDF(PhysicalExamination physicalExamination, HttpServletResponse response) throws Exception {
//设置响应格式等
response.setContentType("application/pdf");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
//设置要导出的pdf的标题
String title = LocalDate.now().toString();
response.setHeader("Content-disposition", "attachment; filename=".concat(String.valueOf(URLEncoder.encode(title + ".pdf", "UTF-8"))));
List<PhysicalExamination> list = physicalExaminationService.selectPhysicalExaminationList(physicalExamination); // 加个时间区间,时间区间用来导出PDF
// 评分
List<String> scores = new ArrayList<>();
scores.add(String.valueOf(size)); // 员工人数
String formatted = String.format("%.2f", averageExamRate);
scores.add(String.valueOf(formatted)); // 体检率
if (list != null && list.size() > 1000) {
list = list.subList(0, 1000);
}
userIndexService.createUserIndexPDF(response, list, scores);
}
@Override
public void createUserIndexPDF(HttpServletResponse response, List<PhysicalExamination> list, List<String> scores) throws Exception {
//设置纸张规格为A4纸
Rectangle rect = new Rectangle(PageSize.A4);
//创建文档实例
Document document = new Document(rect);
OutputStream out = response.getOutputStream();
PdfWriter.getInstance(document,out);
document.open();
document.newPage();
//添加中文字体
BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 设置字体和大小
Font font = new Font(bfChinese, 22, Font.NORMAL);
// 创建一个Chunk对象,并设置居中对齐
Chunk chunk = new Chunk("体检统计", font);
// 创建一个Paragraph对象,并添加Chunk对象
Paragraph paragraph = new Paragraph();
paragraph.add(chunk);
// 添加下边距
paragraph.setSpacingAfter(30); // 10 points of spacing after the paragraph
paragraph.setIndentationLeft(200);
// 将Paragraph对象添加到文档中
document.add(paragraph);
//创建一列的格子
PdfPTable goodTable = new PdfPTable(6);
PdfPCell cell2 = new PdfPCell(new Phrase("员工人数:", iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell2.setColspan(1);
//格子高度35px
cell2.setMinimumHeight(35);
//格子纵跨1个格子
cell2.setRowspan(1);
//格子内容左右居中
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
//格子内容上下居中
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable.addCell(cell2);
PdfPCell cell20 = new PdfPCell(new Phrase(scores.get(0), iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell20.setColspan(2);
//格子高度35px
cell20.setMinimumHeight(35);
//格子纵跨1个格子
cell20.setRowspan(1);
//格子内容左右居中
cell20.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中
cell20.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable.addCell(cell20);
PdfPCell cell21 = new PdfPCell(new Phrase("体检率:", iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell21.setColspan(1);
//格子高度35px
cell21.setMinimumHeight(35);
//格子纵跨1个格子
cell21.setRowspan(1);
//格子内容左右居中
cell21.setHorizontalAlignment(Element.ALIGN_LEFT);
//格子内容上下居中
cell21.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable.addCell(cell21);
PdfPCell cell22 = new PdfPCell(new Phrase(scores.get(1)+"%", iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell22.setColspan(2);
//格子高度35px
cell22.setMinimumHeight(35);
//格子纵跨1个格子
cell22.setRowspan(1);
//格子内容左右居中
cell22.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中
cell22.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable.addCell(cell22);
document.add(goodTable);
// 创建一个Chunk对象,并设置居中对齐
Chunk chunk1 = new Chunk(" ", font);
// 创建一个Paragraph对象,并添加Chunk对象
Paragraph paragraph1 = new Paragraph();
paragraph1.add(chunk1);
// 添加下边距
paragraph1.setSpacingAfter(10); // 10 points of spacing after the paragraph
// 将Paragraph对象添加到文档中
document.add(paragraph1);
//创建一列的格子
PdfPTable goodTable1 = new PdfPTable(8);
// 添加判断,分别是1、2、3、4就 分别填入 年 月份 季度 半年
String cellText = "";
// 使用 switch-case 根据 year_month_type 的值设置 cellText
switch (list.get(0).getYearMonthType()) {
case "1":
cellText = "年";
break;
case "2":
cellText = "月份";
break;
case "3":
cellText = "季度";
break;
case "4":
cellText = "年度";
break;
default:
// 处理默认值或无效值的情况
cellText = "未知类型";
break;
}
PdfPCell cell211 = new PdfPCell(new Phrase(cellText, iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell211.setColspan(2);
//格子高度35px
cell211.setMinimumHeight(35);
//格子纵跨1个格子
cell211.setRowspan(1);
//格子内容左右居中
cell211.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中
cell211.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable1.addCell(cell211);
PdfPCell cell201 = new PdfPCell(new Phrase("体检人数", iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell201.setColspan(2);
//格子高度35px
cell201.setMinimumHeight(35);
//格子纵跨1个格子
cell201.setRowspan(1);
//格子内容左右居中
cell201.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中
cell201.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable1.addCell(cell201);
PdfPCell cell212 = new PdfPCell(new Phrase("体检异常人数", iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell212.setColspan(2);
//格子高度35px
cell212.setMinimumHeight(35);
//格子纵跨1个格子
cell212.setRowspan(1);
//格子内容左右居中
cell212.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中
cell212.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable1.addCell(cell212);
PdfPCell cell221 = new PdfPCell(new Phrase("体检异常率", iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell221.setColspan(2);
//格子高度35px
cell221.setMinimumHeight(35);
//格子纵跨1个格子
cell221.setRowspan(1);
//格子内容左右居中
cell221.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中
cell221.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable1.addCell(cell221);
document.add(goodTable1);
for (int i = 0; i < list.size(); i++) {
PhysicalExamination physicalExamination = list.get(i); // 暂时注释
//创建一列的格子
PdfPTable goodTable2 = new PdfPTable(8); // 添加遍历
String cellText2 = "";
// 使用 switch-case 根据 year_month_type 的值设置 cellText
switch (list.get(0).getYearMonthType()) {
case "1":
cellText2 = physicalExamination.getYear();
break;
case "2":
cellText2 = physicalExamination.getMonth();
break;
case "3":
String quarter = "";
if(physicalExamination.getQuarter().equals("Q1")){
quarter= "第一季度";
}
if(physicalExamination.getQuarter().equals("Q2")){
quarter= "第二季度";
}
if(physicalExamination.getQuarter().equals("Q3")){
quarter= "第三季度";
}
if(physicalExamination.getQuarter().equals("Q4")){
quarter= "第四季度";
}
cellText2 = quarter;
break;
case "4":
String halfYear = "";
if(physicalExamination.getHalfYear().equals("H1")){
halfYear = "上半年";
}
if(physicalExamination.getHalfYear().equals("H2")){
halfYear = "下半年";
}
cellText2 = halfYear;
break;
default:
// 处理默认值或无效值的情况
cellText2 = "未知类型";
break;
}
PdfPCell cell311 = new PdfPCell(new Phrase(cellText2, iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell311.setColspan(2);
//格子高度35px
cell311.setMinimumHeight(35);
//格子纵跨1个格子
cell311.setRowspan(1);
//格子内容左右居中
cell311.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中
cell311.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable2.addCell(cell311);
PdfPCell cell301 = new PdfPCell(new Phrase(String.valueOf(physicalExamination.getPhysicalExamCount()), iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell301.setColspan(2);
//格子高度35px
cell301.setMinimumHeight(35);
//格子纵跨1个格子
cell301.setRowspan(1);
//格子内容左右居中
cell301.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中
cell301.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable2.addCell(cell301);
PdfPCell cell312 = new PdfPCell(new Phrase(String.valueOf(physicalExamination.getAbnormalExamCount()), iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell312.setColspan(2);
//格子高度35px
cell312.setMinimumHeight(35);
//格子纵跨1个格子
cell312.setRowspan(1);
//格子内容左右居中
cell312.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中
cell312.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable2.addCell(cell312);
PdfPCell cell321 = new PdfPCell(new Phrase(String.valueOf(physicalExamination.getAbnormalExamRate()), iTextPDFUtil.getColorFont()));
//格子横跨2个格子
cell321.setColspan(2);
//格子高度35px
cell321.setMinimumHeight(35);
//格子纵跨1个格子
cell321.setRowspan(1);
//格子内容左右居中
cell321.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中
cell321.setVerticalAlignment(Element.ALIGN_MIDDLE);
goodTable2.addCell(cell321);
document.add(goodTable2);
}
PhysicalExamination userIndex = list.get(0);
// 创建两列表格
PdfPTable table = new PdfPTable(2);
//table.setWidthPercentage(100);
Font labelFont = new Font(bfChinese,11, Font.NORMAL);
labelFont.setColor(0, 0, 0);
// 设置每列的列宽
float[] columnWidths = {30f,70f};
table.setWidths(columnWidths);
table.addCell(getCell1("统计结果:", labelFont));
table.addCell(getCell2("您的企业员工体检率为"+scores.get(1)+"%,体检关注员工健康、助力企业发展,异常检出率为6.59%,体检率>=80%:体检人数处于优秀水平,员工大多能每月进行一次体检,请继续保持。体检异常率<10%:员工身体情况良好,请继续保持员工健康宣传,保持员工的身体健康。您企业员工的健康风险评估率为0,中风险率为0,高风险率为0,评估率<50%:健康风险评估人数尚不足50%,处于较低水平,无法给企业和员工提供准确的健康分析与预测,应该尽力督促或要求员工每月进行健康风险评估。中风险率+高风险率<10%:员工易患常见慢性病风险较低,请继续保持员工健康宣传,保持员工的身体健康。员工健康管理需要持续进行健康检测、健康跟踪和健康干预,形成改善循环,才能提高员工的健康水平,助力企业健康发展。", labelFont));
document.add(table);
document.close();
}
public static PdfPCell getCell1(String text, Font font) {
PdfPCell pdfPCell = new PdfPCell(new Phrase(text, font));
//格子高度35px
pdfPCell.setMinimumHeight(35);
//格子内容左右居中
pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中
pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
return pdfPCell;
}
public static PdfPCell getCell2(String text, Font font) {
PdfPCell pdfPCell = new PdfPCell(new Phrase(text, font));
//格子高度35px
pdfPCell.setMinimumHeight(35);
//格子内容左右居中
pdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT);
//格子内容上下居中
pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
return pdfPCell;
}
VUE
// 导出事件方法
exportPDF() {
exportUserIndexPDF(this.addDateRange(this.queryParams, this.dateRange)).then((res) => {
console.log("111111");
//导出文件名
var filename = new Date().getTime();
//创建url
let url = window.URL.createObjectURL(res)
//创建a标签 并设置属性
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', filename + '.pdf')
//添加a标签
document.body.appendChild(link)
//执行下载
link.click();
//释放url对象
URL.revokeObjectURL(link.href);
//释放a标签
document.body.removeChild(link);
//this.download = false;
});
},