java后端将数据库中数据或前端传来的数据保存到pdf文件中。代码中有详细注解。
- 依赖包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
- 实现代码
/**
* 将数据保存到PDF文件
* @param filePath 文件存放路径
* @param fileName 文件名称
* @param data 数据内容
* data: {
* name: '标准名称',
* objName: '适用对象名称',
* version: '标准版本',
* remark: '备注',
* fields: [
* {
* name: '数据项名称',
* rules: [
* {
* name: '规则名称',
* type: '规则类型',
* params: '规则参数'
* }
* ]
* }
* ]
* }
*/
public void writePdfTable(String filePath, String fileName, ObjectNode data) throws Exception {
Document document = null;
PdfWriter writer = null;
try {
String fullPath = filePath + fileName + ".pdf";
File absolutePathDirectory = new File(filePath);
if (!absolutePathDirectory.exists()) {
absolutePathDirectory.mkdirs();
}
document = new Document();
writer = PdfWriter.getInstance(document, new FileOutputStream(fullPath));
document.open();
// 中文字体,解决中文不能显示问题
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 正常字体
Font font = new Font(bfChinese);
// 正常加粗字体
Font fontBold = new Font(bfChinese, 12, Font.BOLD);
// 加粗大字体
Font fontBigBold = new Font(bfChinese, 20, Font.BOLD);
// 添加主题
Paragraph themeQuality = new Paragraph("标准概要内容", fontBigBold);
themeQuality.setAlignment(Element.ALIGN_CENTER);
document.add(themeQuality);
// Table 标准内容
// 列宽
float[] columnWidthsQuality = {3.5f, 16.5f};
// 初始化Table:2行、宽度90%填充、前间距20f、后间距20f、splitLate、各列的宽度
PdfPTable tableStandard = initPdfTable(2, 90f, 20f, 20f, false, columnWidthsQuality);
List<PdfPRow> listRowQuality = tableStandard.getRows();
// 表头
// 标准名称
PdfPCell[] cellsQualityName = new PdfPCell[2];
PdfPRow rowQualityName = new PdfPRow(cellsQualityName);
cellsQualityName[0] = getCell(25f, "标准名称", fontBold, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, BaseColor.LIGHT_GRAY, 0);
cellsQualityName[1] = getCell(25f, data.name, fontBold, Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, null, 0);
listRowQuality.add(rowQualityName);
// 对象名称
PdfPCell[] cellsSubjectName = new PdfPCell[2];
PdfPRow rowSubjectName = new PdfPRow(cellsSubjectName);
cellsSubjectName[0] = getCell(25f, "对象名称", fontBold, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, BaseColor.LIGHT_GRAY, 0);
cellsSubjectName[1] = getCell(25f, data.objName, fontBold, Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, null, 0);
listRowQuality.add(rowSubjectName);
// 版本号
PdfPCell[] cellsVersion = new PdfPCell[2];
PdfPRow rowVersion = new PdfPRow(cellsVersion);
cellsVersion[0] = getCell(25f, "版本号", fontBold, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, BaseColor.LIGHT_GRAY, 0);
cellsVersion[1] = getCell(25f, data.version, fontBold, Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, null, 0);
listRowQuality.add(rowVersion);
// 备注
PdfPCell[] cellsRemark = new PdfPCell[2];
PdfPRow rowRemark = new PdfPRow(cellsRemark);
cellsRemark[0] = getCell(25f, "备注", fontBold, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, BaseColor.LIGHT_GRAY, 0);
cellsRemark[1] = getCell(25f, data.remark, fontBold, Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, null, 0);
listRowQuality.add(rowRemark);
// 将标准信息添加到文件中
document.add(tableStandard);
// 添加主题
Paragraph theme = new Paragraph("标准详细内容", fontBigBold);
theme.setAlignment(Element.ALIGN_CENTER);
document.add(theme);
// Table 规则详细内容
// 列宽
float[] columnWidthsRule = {4.5f, 3.5f, 6f, 6f};
// 初始化Table:4行、宽度90%填充、前间距20f、后间距20f、splitLate、各列的宽度
PdfPTable tableSpecification = initPdfTable(4, 90f, 20f, 20f, true, columnWidthsRule);
List<PdfPRow> listRow = tableSpecification.getRows();
// 表格列名
PdfPCell[] cells0 = new PdfPCell[4];
PdfPRow row0 = new PdfPRow(cells0);
// 第一列(水平居中、垂直居中)
cells0[0] = getCell(25f, "数据项名称", fontBold, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, BaseColor.LIGHT_GRAY, 0);
// 第二列(水平居中、垂直居中)
cells0[1] = getCell(25f, "规则类型", fontBold, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, BaseColor.LIGHT_GRAY, 0);
// 第三列(水平居中、垂直居中)
cells0[2] = getCell(25f, "规则名称", fontBold, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, BaseColor.LIGHT_GRAY, 0);
// 第四列列名(水平居中、垂直居中)
cells0[3] = getCell(25f, "规则参数", fontBold, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, BaseColor.LIGHT_GRAY, 0);
listRow.add(row0);
// 获取规则
for (JsonNode fieldNode : data.fields) {
ArrayNode rules = fieldNode.getSpecifications;
for (JsonNode rule : rules) {
PdfPCell[] cells = new PdfPCell[4];
PdfPRow row = new PdfPRow(cells);
// 字段名称(水平居中、垂直居中)
cells[0] = getCell(50f, fieldNode.name, font, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, null, 0);
// 规则类型(水平居中、垂直居中)
cells[1] = getCell(50f, rule.type, font, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, null, 0);
// 规则名称(水平居中、垂直居中)
cells[2] = getCell(50f, rule.name, font, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, null, 0);
// 规则参数值(水平居中、垂直居中)
cells[3] = getCell(50f, rule.params, font, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, null, 0);
// 把单元格添加到集合(行)
listRow.add(row);
}
}
//把规则信息添加到文件中
document.add(tableSpecification);
} catch (Exception e) {
throw e;
} finally {
if (document != null) {
document.close();
}
if (writer != null) {
writer.close();
}
}
}