1.引入maven依赖。
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
2.编写PDFUtil.java工具类。
/** PDF工具类 */
@Slf4j
public class PDFUtil {
public static final String PDF_URL = "C:/Users/恢复演练报告.pdf";
/**
* 获取当前时间,返回格式 yyyy-MM-dd HH:mm:ss
* @return
*/
public static String getNowDate(){
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()).toString();
}
/**
* 创建PDF文档和自定义头部
* @return
* @throws DocumentException
* @throws IOException
*/
public static Document createPdfHead() throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(PDF_URL));
document.open();
BaseFont fontChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChina18 = new Font(fontChinese, 18);
Font fontChina12 = new Font(fontChinese, 12);
Paragraph titleParagraph = new Paragraph("数据库备份恢复演练报告",fontChina18);
titleParagraph.setAlignment(Element.ALIGN_CENTER);// 居中
document.add(titleParagraph);
Paragraph blank = new Paragraph(" ");
document.add(blank);
Chunk c1 = new Chunk("创建日期:", fontChina12);
Chunk c2 = new Chunk(new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString(), fontChina12);
Paragraph snoParagraph = new Paragraph();
snoParagraph.add(c1);
snoParagraph.add(c2);
snoParagraph.setAlignment(Element.ALIGN_RIGHT);
document.add(snoParagraph);
document.add(blank);
return document;
}
public static PdfPTable createTable() throws DocumentException,IOException{
BaseFont fontChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChina18 = new Font(fontChinese, 18);
Font fontChina12 = new Font(fontChinese, 12);
PdfPTable table = new PdfPTable(4);
table.setWidthPercentage(100);
// 恢复演练实例名
table.addCell(PDFUtil.setCell("恢复演练实例名",fontChina12));
table.addCell(PDFUtil.setCell("pg857da",fontChina12,3));
table.addCell(PDFUtil.setCell("目标实例",fontChina12));
table.addCell(PDFUtil.setCell("pg8956u",fontChina12));
table.addCell(PDFUtil.setCell("目标实例端口号",fontChina12));
table.addCell(PDFUtil.setCell("3306",fontChina12));
table.addCell(PDFUtil.setCell("恢复演练开始时间",fontChina12));
table.addCell(PDFUtil.setCell(PDFUtil.getNowDate(),fontChina12,3));
table.addCell(PDFUtil.setCell("恢复演练结束时间",fontChina12));
table.addCell(PDFUtil.setCell(PDFUtil.getNowDate(),fontChina12,3));
table.addCell(PDFUtil.setCell("恢复演练执行人",fontChina12));
table.addCell(PDFUtil.setCell("XXXXXX团队",fontChina12,3));
table.addCell(PDFUtil.setCell("恢复演练方式",fontChina12));
table.addCell(PDFUtil.setCell("按最近一次全备恢复",fontChina12,3));
table.addCell(PDFUtil.setCell("恢复演练结果",fontChina12));
table.addCell(PDFUtil.setCell("验证通过",fontChina12,3));
table.addCell(PDFUtil.setCenterCell("恢复演练过程",fontChina12,4));
table.addCell(PDFUtil.setCell("(1)选择恢复的备份片",fontChina12,4));
table.addCell(PDFUtil.setBackupSetCell(fontChina12,4));
table.addCell(PDFUtil.setCell("(2)恢复演练结果",fontChina12,4));
table.addCell(PDFUtil.setDrillResultCell(fontChina12,4));
table.addCell(PDFUtil.setCell("(3)实例恢复信息",fontChina12,4));
table.addCell(PDFUtil.setRestoreInfoCell(fontChina12,4));
table.addCell(PDFUtil.setCell("(4)启库信息",fontChina12,4));
table.addCell(PDFUtil.setCell(" ",fontChina12,4));
table.addCell(PDFUtil.setCell("(5)恢复准确性信息",fontChina12,4));
table.addCell(PDFUtil.setAccuracyInfoCell(fontChina12,4));
table.addCell(PDFUtil.setCell("(6)释放恢复实例信息",fontChina12,4));
table.addCell(PDFUtil.setReleaseInfoCell(fontChina12,4));
return table;
}
/**
*
* @param text 文本内容
* @param font 字体
* @param colspan 夸多少列
* @return PDF单元格
*/
public static PdfPCell setCell(String text,Font font,int colspan){
PdfPCell cell = new PdfPCell();
cell.setPhrase(new Paragraph(text, font));
cell.setColspan(colspan);
return cell;
}
/**
*
* @param text 文本
* @param font 字体
* @return PDF单元格
*/
public static PdfPCell setCell(String text,Font font){
PdfPCell cell = new PdfPCell();
cell.setPhrase(new Paragraph(text, font));
return cell;
}
/**
* 设置单元格文本内容垂直居中
* @param text 文本
* @param font 字体
* @param colspan 夸多少列
* @return
*/
public static PdfPCell setCenterCell(String text,Font font,int colspan){
PdfPCell cell = new PdfPCell();
Paragraph para = new Paragraph(text,font);
cell.setPhrase(para);
cell.setColspan(colspan);
cell.setUseAscender(true);
cell.setVerticalAlignment(cell.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
return cell;
}
public static PdfPCell setBackupSetCell(Font font,int colspan){
PdfPCell cell = new PdfPCell();
cell.setColspan(colspan);
cell.setFixedHeight(100);
Paragraph startTime = new Paragraph("开始时间: "+getNowDate(),font);
Paragraph finishTime = new Paragraph("结束时间: "+getNowDate(),font);
Paragraph size = new Paragraph("备份片大小: "+"1024",font);
Paragraph backupType = new Paragraph("备份类型: "+"自动备份",font);
Paragraph status = new Paragraph("备份片状态: "+"备份成功",font);
cell.addElement(startTime);
cell.addElement(finishTime);
cell.addElement(size);
cell.addElement(backupType);
cell.addElement(status);
return cell;
}
public static PdfPCell setDrillResultCell(Font font,int colspan){
PdfPCell cell = new PdfPCell();
cell.setColspan(colspan);
cell.setFixedHeight(120);
Paragraph id = new Paragraph("操作ID: "+"549",font);
Paragraph name = new Paragraph("实例名称: "+"pgm789k",font);
Paragraph des = new Paragraph("操作描述: "+"创建恢复实例",font);
Paragraph startTime = new Paragraph("恢复开始时间: "+getNowDate(),font);
Paragraph finishTime = new Paragraph("恢复结束时间: "+getNowDate(),font);
Paragraph status = new Paragraph("恢复状态: "+"成功",font);
cell.addElement(id);
cell.addElement(name);
cell.addElement(des);
cell.addElement(startTime);
cell.addElement(finishTime);
cell.addElement(status);
return cell;
}
public static PdfPCell setRestoreInfoCell(Font font,int colspan){
PdfPCell cell = new PdfPCell();
cell.setColspan(colspan);
cell.setFixedHeight(120);
String str1 = "恢复实例名称:"+"pgm789k"+" "+"恢复实例类型:"+"临时恢复实例";
String str2 = "运行状态:"+"运行中"+" "+" 可用区:"+"ECA";
String str3 = "数据库类型:"+"postgre9.6.10"+" "+"创建时间:"+getNowDate();
Paragraph id = new Paragraph(str1,font);
Paragraph name = new Paragraph(str2,font);
Paragraph des = new Paragraph(str3,font);
Paragraph port = new Paragraph("连接端口: "+"12345",font);
Paragraph url = new Paragraph("域名: "+"pgm789k.db.papub",font);
Paragraph src = new Paragraph("恢复来源: "+"pga675r",font);
cell.addElement(id);
cell.addElement(name);
cell.addElement(des);
cell.addElement(port);
cell.addElement(url);
cell.addElement(src);
return cell;
}
public static PdfPCell setAccuracyInfoCell(Font font,int colspan){
PdfPCell cell = new PdfPCell();
cell.setColspan(colspan);
cell.setFixedHeight(120);
Paragraph id = new Paragraph("备份实例ID: "+"54369",font);
Paragraph name = new Paragraph("开始archive 日志地址: "+"00000000F1000020000000F",font);
Paragraph des = new Paragraph("结束archive 日志地址: "+"00000000F1000020000000F",font);
Paragraph startTime = new Paragraph("开始时间: "+getNowDate(),font);
Paragraph finishTime = new Paragraph("结束时间: "+getNowDate(),font);
Paragraph status = new Paragraph("状态: "+"成功",font);
cell.addElement(id);
cell.addElement(name);
cell.addElement(des);
cell.addElement(status);
cell.addElement(startTime);
cell.addElement(finishTime);
return cell;
}
public static PdfPCell setReleaseInfoCell(Font font,int colspan){
PdfPCell cell = new PdfPCell();
cell.setColspan(colspan);
cell.setFixedHeight(120);
Paragraph id = new Paragraph("资源名称: "+"pgm789k",font);
Paragraph name = new Paragraph("操作: "+"删除PG实例",font);
Paragraph des = new Paragraph("操作时间: "+getNowDate(),font);
Paragraph finishTime = new Paragraph("结束时间: "+getNowDate(),font);
Paragraph status = new Paragraph("状态: "+"成功",font);
cell.addElement(id);
cell.addElement(name);
cell.addElement(des);
cell.addElement(finishTime);
cell.addElement(status);
return cell;
}
}
3.编写前端控制器
@RestController
@Slf4j
public class DemoController {
@GetMapping("/pdf")
public String pdf() throws FileNotFoundException, DocumentException, IOException {
log.info(">>>开始创建PDF文档");
Document document = PDFUtil.createPdfHead();
PdfPTable table = PDFUtil.createTable();
document.add(table);
document.close();
log.info(">>>PDF文档创建成功");
return "PDF文档创建成功";
}
}
4.访问连接http://localhost:8184/pdf
然后在本地电脑C:\Users路径查看PDF文档