0%

java 导出PDF

简介

网上找到 itextpdf,使用体验还不错,但是许可协议属于APGL,商业使用有一定限制。
选择其开源版本 OpenPdf**, **使用方法大致一样

Maven

1
2
3
4
5
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.30</version>
</dependency>

基本使用

  1. font 设置字体,字号,加粗格式
  2. Paragraph 设置具体内容
  3. PdfPTable 可表格设置所占屏幕的比例,setWidthPercentage(100)
  4. 可设置表格各行所占的比例,new PdfPTable(new float[]{4, 2, 2, 3})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
public byte[] getPdfByte(OutboundDetailVO detailVO) {
String filePath = "output1.pdf";
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Document document = new Document();
// 可选择写入文件,或者相关流
PdfWriter.getInstance(document, Files.newOutputStream(Paths.get(filePath)));
// PdfWriter.getInstance(document, baos);
document.open();

// 字体设置
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont, 9, Font.NORMAL);
Font headerFont = new Font(baseFont, 10, Font.BOLD);
Font titleFont = new Font(baseFont, 14, Font.BOLD);

// 添加标题
Paragraph title = new Paragraph("XXX",titleFont);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
document.add(new Paragraph("\n"));

// 格式化客户信息
String format = "{}: {}";
String customerName = StrUtil.format(format, "aaa", detailVO.getCustomerName());
String orderNumber = StrUtil.format(format, "bbb", detailVO.getOrdernumber());
String date = StrUtil.format(format, "ccc", DateUtil.format(detailVO.getDate(),"yyyy-MM-dd"));
String outboundNumber = StrUtil.format(format, "ddd", detailVO.getWarehouseOutN());
String outboundBy = StrUtil.format(format, "eee", detailVO.getOutstockuname());
String checkBy = StrUtil.format(format, "fff", detailVO.getCheckuname());

// 创建客户信息表格
PdfPTable customerInfoTable = new PdfPTable(new float[]{4, 2, 2, 3});
customerInfoTable.setWidthPercentage(100);
customerInfoTable.addCell(createCell(customerName, font, false, true, true, Element.ALIGN_LEFT));
customerInfoTable.addCell(createCell(orderNumber, font, false, true, true, Element.ALIGN_LEFT));
customerInfoTable.addCell(createCell(date, font, false, true, true, Element.ALIGN_LEFT));
customerInfoTable.addCell(createCell(outboundNumber, font, false, true, true, Element.ALIGN_LEFT));
document.add(customerInfoTable);
document.add(new Paragraph("\n"));

// 创建主表格
PdfPTable table = new PdfPTable(new float[]{2, 2, 2, 2, 2});
table.setWidthPercentage(100);
table.addCell(createCell("1", headerFont, true, false, false, Element.ALIGN_CENTER));
table.addCell(createCell("2", headerFont, true, false, false, Element.ALIGN_CENTER));
table.addCell(createCell("3", headerFont, true, false, false, Element.ALIGN_CENTER));
table.addCell(createCell("4", headerFont, true, false, false, Element.ALIGN_CENTER));
table.addCell(createCell("5", headerFont, true, false, false, Element.ALIGN_CENTER));

// 添加表格内容
for (outstockeditspecification one : detailVO.getDetailList()) {
table.addCell(createCell(one.getSpecification(), font, false, false, true, Element.ALIGN_CENTER));
table.addCell(createCell(one.getPieces(), font, false, false, true, Element.ALIGN_CENTER));
table.addCell(createCell(one.getLevel(), font, false, false, true, Element.ALIGN_CENTER));
table.addCell(createCell(one.getQuantity(), font, false, false, true, Element.ALIGN_CENTER));
table.addCell(createCell(one.getExplain(), font, false, false, true, Element.ALIGN_CENTER));
}

document.add(table);
document.add(new Paragraph("\n"));

// 创建底部信息表格
PdfPTable footerTable = new PdfPTable(new float[]{1, 1});
footerTable.setWidthPercentage(100);
footerTable.addCell(createCell(outboundBy, font, false, true, true, Element.ALIGN_LEFT));
footerTable.addCell(createCell(checkBy, font, false, true, true, Element.ALIGN_RIGHT));

document.add(footerTable);

// 关闭文档
document.close();

// 将 ByteArrayOutputStream 转换为 byte 数组
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

/**
* 创建一个单元格,设置内容和格式选项。
*
* @param content 单元格的文本内容
* @param font 单元格文本使用的字体
* @param isHeader 是否为表头单元格,表头单元格将使用粗体
* @param noBorder 是否去除单元格的边框
* @param noBackground 是否去除单元格的背景色
* @param alignment 文本对齐方式
* @return 带有指定内容和格式的单元格
*/
private PdfPCell createCell(String content, Font font, boolean isHeader, boolean noBorder, boolean noBackground, int alignment) {
PdfPCell cell = new PdfPCell(new Phrase(content, font));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(alignment);
cell.setPadding(5);

if (noBorder) {
cell.setBorder(Rectangle.NO_BORDER);
}

if (isHeader) {
cell.setBackgroundColor(new Color(250, 250, 251));
}

return cell;
}
-------------本文结束感谢您的阅读-------------
您的支持将成为我创作的动力!