/**
* 生成维保服务要求pdf
*/
// @PreAuthorize("@ss.hasPermi('biz:txfWbRequest:export')")
@Log(title = "维保服务要求", businessType = BusinessType.EXPORT)
@GetMapping("/createWbPDF")
public AjaxResult createWbPDF(TxfWbRequest txfWbRequest, HttpServletResponse response, HttpServletRequest request)
{
List<TxfWbRequest> txfWbRequests = txfWbRequestService.selectTxfWbRequestList(txfWbRequest);
try {
String uuid = UUID.randomUUID().toString();
String realFileName = uuid + ".pdf";
String filePath = RuoYiConfig.getDownloadPath() + realFileName;
txfWbRequestService.createWbPDF(txfWbRequests,filePath);
// 导出pdf
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
FileUtils.writeBytes(filePath, response.getOutputStream());
// 删除
FileUtils.deleteFile(filePath);
} catch (Exception e) {
e.printStackTrace();
}
return AjaxResult.success();
}
public void createWbPDF(List<TxfWbRequest> txfWbRequests,String filePath) throws DocumentException, FileNotFoundException {
//创建文件
Document document = new Document();
//建立一个书写器
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
//打开文件
document.open();
BaseFont bfChinese = null;//1 不能省略
try {
bfChinese = BaseFont.createFont("C:\\Windows\\Fonts\\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (IOException e) {
e.printStackTrace();
}
Font fontChinese = new Font(bfChinese, 10, Font.NORMAL);
Font fontChinese1 = new Font(bfChinese, 22, Font.NORMAL);
//添加内容
Paragraph paragraph = new Paragraph("建筑消防设施维护保养计划表", fontChinese1);
paragraph.setFont(fontChinese1);
paragraph.setAlignment(Element.ALIGN_MIDDLE);
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
// 3列的表.
PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100); // 宽度100%填充
table.setSpacingBefore(10f); // 前间距
table.setSpacingAfter(10f); // 后间距
List<PdfPRow> listRow = table.getRows();
//设置列宽
float[] columnWidths = {1.5f, 2f, 1f, 3f, 2.5f};
table.setWidths(columnWidths);
// PdfPCell pdfPCells[] = new PdfPCell[1];
// PdfPRow row1 = new PdfPRow(pdfPCells);
// 行首标题
PdfPCell hangShouCells[] = new PdfPCell[5];
PdfPRow rowOne = new PdfPRow(hangShouCells);
hangShouCells[0] = new PdfPCell(new Paragraph(sysDictDataService.getDictName("系统", "6"), fontChinese));
hangShouCells[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
hangShouCells[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
hangShouCells[1] = new PdfPCell(new Paragraph("维保设备及分项", fontChinese));
hangShouCells[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
hangShouCells[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
hangShouCells[2] = new PdfPCell(new Paragraph("检查周期", fontChinese));
hangShouCells[2].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
hangShouCells[2].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
hangShouCells[3] = new PdfPCell(new Paragraph("维保内容", fontChinese));
hangShouCells[3].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
hangShouCells[3].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
hangShouCells[4] = new PdfPCell(new Paragraph("维保计划 ", fontChinese));
hangShouCells[4].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
hangShouCells[4].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
listRow.add(rowOne);
if (txfWbRequests != null) {
Map<String, Integer> systemidMap = new HashMap<>();
for (int i = 0; i < txfWbRequests.size(); i++){
Integer value=systemidMap.get(txfWbRequests.get(i).getSystemid());
if (value==null){
systemidMap.put(txfWbRequests.get(i).getSystemid(),1);
}else {
value+=1;
systemidMap.put(txfWbRequests.get(i).getSystemid(),value);
}
}
List<String> list=new ArrayList<>();
list.addAll(systemidMap.keySet());
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int a=Integer.parseInt(o1.replace("_",""));
int b=Integer.parseInt(o2.replace("_",""));
return a-b;
}
});
for (String key:list){
boolean check=true;
for (int i = 0; i < txfWbRequests.size(); i++){
PdfPCell pdfPCells[] = new PdfPCell[5];
PdfPRow row1 = new PdfPRow(pdfPCells);
if (key.equals(txfWbRequests.get(i).getSystemid())){
if (check){
pdfPCells[0] = new PdfPCell(new Paragraph(sysDictDataService.getDictName(txfWbRequests.get(i).getSystemid(), "6"), fontChinese));
pdfPCells[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
pdfPCells[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
pdfPCells[0].setRowspan(systemidMap.get(key));
check=false;
}
pdfPCells[1] = new PdfPCell(new Paragraph(txfWbRequests.get(i).getFacility(), fontChinese));
pdfPCells[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
pdfPCells[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
pdfPCells[2] = new PdfPCell(new Paragraph(txfWbRequests.get(i).getCycle(), fontChinese));
pdfPCells[2].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
pdfPCells[2].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
pdfPCells[3] = new PdfPCell(new Paragraph(txfWbRequests.get(i).getRequestdesc(), fontChinese));
pdfPCells[3].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
pdfPCells[3].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
pdfPCells[4] = new PdfPCell(new Paragraph("", fontChinese));
pdfPCells[4].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
pdfPCells[4].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
listRow.add(row1);
}
}
}
}
//把表格添加到文件中
document.add(table);
//关闭文档
document.close();
//关闭书写器
writer.close();
}