Skip to content

Commit 88a2b94

Browse files
authored
Merge pull request #91 from Boyuan-IT-Club/Red_Moon
持续修正pdf字体问题
2 parents feea9b1 + 3dfc232 commit 88a2b94

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ RUN apt-get update && \
1313
fonts-noto-cjk-extra \
1414
fonts-liberation \
1515
libfontconfig1 \
16-
&& rm -rf /var/lib/apt/lists/*
16+
&& rm -rf /var/lib/apt/lists/* \
17+
&& fc-cache -fv
1718

1819
# 设置中文环境变量
1920
ENV LANG=zh_CN.UTF-8 \

src/main/java/club/boyuan/official/utils/PdfExportUtil.java

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ public static byte[] exportResumeToPdf(ResumeDTO resumeDTO) throws BusinessExcep
3434
System.out.println("开始初始化PDF字体...");
3535
BaseFont testFont = getChineseBaseFont();
3636

37+
// 检查简历数据是否为空
38+
if (resumeDTO == null) {
39+
throw new BusinessException(BusinessExceptionEnum.EXPORT_PDF_FAILED, "PDF导出失败: 简历数据为空");
40+
}
41+
42+
System.out.println("开始导出PDF,用户ID: " + resumeDTO.getUserId());
43+
if (resumeDTO.getSimpleFields() != null) {
44+
System.out.println("简历字段数量: " + resumeDTO.getSimpleFields().size());
45+
} else {
46+
System.out.println("警告: 简历字段为null");
47+
}
48+
3749
ByteArrayOutputStream baos = new ByteArrayOutputStream();
3850

3951
// 创建文档
@@ -72,6 +84,8 @@ public static byte[] exportResumeToPdf(ResumeDTO resumeDTO) throws BusinessExcep
7284

7385
// 添加字段值信息
7486
if (resumeDTO.getSimpleFields() != null && !resumeDTO.getSimpleFields().isEmpty()) {
87+
System.out.println("开始添加简历详情,字段数量: " + resumeDTO.getSimpleFields().size());
88+
7589
Paragraph fieldsTitle = new Paragraph("简历详情", headerFont);
7690
fieldsTitle.setSpacingBefore(20);
7791
fieldsTitle.setSpacingAfter(10);
@@ -84,22 +98,40 @@ public static byte[] exportResumeToPdf(ResumeDTO resumeDTO) throws BusinessExcep
8498
// 表头
8599
PdfPCell header1 = new PdfPCell(new Paragraph("字段名称", headerFont));
86100
PdfPCell header2 = new PdfPCell(new Paragraph("字段值", headerFont));
101+
header1.setBorder(Rectangle.BOX);
102+
header2.setBorder(Rectangle.BOX);
103+
header1.setBackgroundColor(BaseColor.LIGHT_GRAY);
104+
header2.setBackgroundColor(BaseColor.LIGHT_GRAY);
87105
fieldsTable.addCell(header1);
88106
fieldsTable.addCell(header2);
89107

90108
// 数据行
91109
for (SimpleResumeFieldDTO field : resumeDTO.getSimpleFields()) {
92-
PdfPCell fieldLabel = new PdfPCell(new Paragraph(
93-
field.getFieldLabel() != null ? field.getFieldLabel() : "未知字段",
94-
normalFont));
95-
PdfPCell fieldValueCell = new PdfPCell(new Paragraph(
96-
field.getFieldValue() != null ? field.getFieldValue() : "",
97-
normalFont));
98-
fieldsTable.addCell(fieldLabel);
110+
String fieldLabel = field.getFieldLabel() != null ? field.getFieldLabel() : "未知字段";
111+
String fieldValue = field.getFieldValue() != null ? field.getFieldValue() : "";
112+
113+
System.out.println("添加字段: " + fieldLabel + " = " + fieldValue);
114+
115+
PdfPCell fieldLabelCell = new PdfPCell(new Paragraph(fieldLabel, normalFont));
116+
PdfPCell fieldValueCell = new PdfPCell(new Paragraph(fieldValue, normalFont));
117+
118+
fieldLabelCell.setBorder(Rectangle.BOX);
119+
fieldValueCell.setBorder(Rectangle.BOX);
120+
121+
fieldsTable.addCell(fieldLabelCell);
99122
fieldsTable.addCell(fieldValueCell);
100123
}
101124

102125
document.add(fieldsTable);
126+
System.out.println("添加简历详情成功");
127+
} else {
128+
System.out.println("警告: 简历详情为空或没有字段数据");
129+
130+
// 添加提示信息
131+
Paragraph noDataMsg = new Paragraph("暂无简历详情数据", normalFont);
132+
noDataMsg.setSpacingBefore(20);
133+
noDataMsg.setAlignment(Element.ALIGN_CENTER);
134+
document.add(noDataMsg);
103135
}
104136

105137
// 添加导出时间
@@ -112,7 +144,9 @@ public static byte[] exportResumeToPdf(ResumeDTO resumeDTO) throws BusinessExcep
112144
document.close();
113145
}
114146

115-
return baos.toByteArray();
147+
byte[] pdfBytes = baos.toByteArray();
148+
System.out.println("PDF导出成功,文件大小: " + pdfBytes.length + " bytes");
149+
return pdfBytes;
116150
} catch (Exception e) {
117151
System.err.println("PDF导出失败: " + e.getClass().getSimpleName() + " - " + e.getMessage());
118152
e.printStackTrace();
@@ -152,11 +186,15 @@ private static Font getFont(int size, int style) {
152186
private static BaseFont getChineseBaseFont() {
153187
// 优先尝试Docker容器中常见的字体路径
154188
String[][] fontConfigs = {
155-
// 容器中的Noto字体 - 最高优先级
156-
{"/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc,0", BaseFont.IDENTITY_H},
189+
// 容器中的Noto字体 - 按实际存在的路径优先排序
157190
{"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc,0", BaseFont.IDENTITY_H},
158-
{"/usr/share/fonts/truetype/noto/NotoSerifCJK-Regular.ttc,0", BaseFont.IDENTITY_H},
159191
{"/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc,0", BaseFont.IDENTITY_H},
192+
{"/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc,0", BaseFont.IDENTITY_H},
193+
{"/usr/share/fonts/truetype/noto/NotoSerifCJK-Regular.ttc,0", BaseFont.IDENTITY_H},
194+
195+
// 单独的CJK字体文件
196+
{"/usr/share/fonts/opentype/noto/NotoSansCJK-SC-Regular.otf", BaseFont.IDENTITY_H},
197+
{"/usr/share/fonts/opentype/noto/NotoSerifCJK-SC-Regular.otf", BaseFont.IDENTITY_H},
160198

161199
// 容器中的DejaVu字体
162200
{"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", BaseFont.IDENTITY_H},

0 commit comments

Comments
 (0)