PDF导出
controller控制层
public void export(queryVo vo, PageQuery pageQuery,HttpServletResponse response) {
TableDataInfo<TaskAudit> auditTableDataInfo = iTaskAuditService.queryPageAuditList(bo, pageQuery);
try {
iTaskAuditService.createPDF(auditTableDataInfo.getRows(), pageQuery,response);
} catch (Exception e) {
throw new RuntimeException(e);;
}
}
service实现类
public void createPDF(List<TaskAudit> taskAudits, PageQuery pageQuery, HttpServletResponse response) throws Exception {
String fileName = "导出_" + LoginHelper.getUsername() + "_" + DateUtils.parseDateToStr("yyyy-MM-dd", new Date()) + ".pdf";
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font headFont = new Font(bf, 18, Font.BOLD);
Font textFont = new Font(bf, 12, Font.NORMAL);
int column = 2;
Document document = new Document();
OutputStream out = response.getOutputStream();
try {
PdfWriter writer = PdfWriter.getInstance(document, out);
document.open();
document.setPageSize(PageSize.A4);
PdfPTable table = ExcelUtil.createTable(column);
table.addCell(ExcelUtil.createTitle("导出", headFont, Element.ALIGN_CENTER, column, false));
//
for (int i = 0; i < taskAudits.size(); i++) {
TaskAudit taskAudit = taskAudits.get(i);
table.addCell(ExcelUtil.createCell(String.valueOf(i + 1), textFont));
PdfPCell taskCell = ExcelUtil.createCell(
(taskAudit.getTaskName() + "—:" + taskAudit.getTaskCode() +
"\n 投放周期:" + taskAudit.getServingStartTime().substring(0, 9) + "~" + taskAudit.getServingEndTime().substring(0, 9) +
"\n 公司:" + taskAudit.getSupplierName() + "\n 供应商:" + taskAudit.getDeptName()), textFont);
table.addCell(taskCell);
//广告点位
if (taskAudit.getAuditStatus() >= 1) {
List<TaskAudit> pointInfo = this.selectAuditInfo(taskAudit.getId(), pageQuery).getRows();
for (int k = 0; k < pointInfo.size(); k++) {
for (int j = 0; j < column; j++) {
TaskAudit pointRecordVo = pointInfo.get(k);
if (j == 0) {
PdfPCell black1 = ExcelUtil.createCell("", textFont);
black1.disableBorderSide(3);
table.addCell(black1);
} else {
String auditStatusName = "";
if (pointRecordVo.getAuditStatus() == 1) {
auditStatusName = "待审核";
} else if (pointRecordVo.getAuditStatus() == 2) {
auditStatusName = "审核不通过";
} else if (pointRecordVo.getAuditStatus() == 3) {
auditStatusName = "审核通过";
}
String auditTime;
if (pointRecordVo.getAuditTime() == null || ("").equals(pointRecordVo.getAuditTime())) {
auditTime = "";
} else {
auditTime = DateUtils.parseDateToStr("yyyy-MM-dd", pointRecordVo.getAuditTime());
}
PdfPCell pointCell = ExcelUtil.createCell((pointRecordVo.getPointName() + "—:" + pointRecordVo.getPointCode()
+ "\n 审核结果:" + auditStatusName
+ "\n 审核人:" + Optional.ofNullable(pointRecordVo.getAuditName()).orElse("")
+ "\n 审核时间:" + auditTime), textFont);
pointCell.disableBorderSide(2);
table.addCell(pointCell);
//设置图片table
PdfPCell black2 = ExcelUtil.createCell("", textFont);
black2.disableBorderSide(3);
table.addCell(black2);
PdfPCell cells = new PdfPCell();
cells.setFixedHeight(180);
String imgUrl = pointRecordVo.getSmallFileUrl();
String[] imgList = imgUrl.split(",");
PdfPTable imgTable = new PdfPTable(imgList.length);
for (int l = 0; l < imgList.length; l++) {
String imgPath = fileService.getShareUrl(imgList[l]);
Image image = Image.getInstance(imgPath);
image.setWidthPercentage(20.0f);
PdfPCell cell = new PdfPCell(image, true);
cell.disableBorderSide(15);
imgTable.addCell(cell);
}
cells.addElement(imgTable);
cells.disableBorderSide(1);
table.addCell(cells);
}
}
}
}
}
document.add(table);
document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
// return file;
}
}
Excelutil工具类
public static PdfPCell createCell(String value, Font font) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_LEFT);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setPhrase(new Phrase(value, font));
return cell;
}
/**
* 创建PDF文件表格
* @param colNumber
* @return
*/
public static PdfPTable createTable(int colNumber) {
PdfPTable table = new PdfPTable(colNumber);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
int size = 2;
float width[] = new float[size];
for (int i = 0; i < size; i++) {
if (i == 0) {
width[i] = 60;
} else {
width[i] = 520;
}
}
try {
table.setTotalWidth(width);
} catch (DocumentException e) {
throw new RuntimeException(e);
}
return table;
}