Skip to content

Commit 35c0c63

Browse files
authored
Merge pull request #92 from Boyuan-IT-Club/Red_Moon
尝试让图片正常编译
2 parents 88a2b94 + 46cc057 commit 35c0c63

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

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

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.time.LocalDateTime;
1616
import java.time.format.DateTimeFormatter;
1717
import java.util.Objects;
18+
import java.util.Base64;
19+
import java.io.ByteArrayInputStream;
1820

1921
/**
2022
* PDF导出工具类
@@ -110,10 +112,31 @@ public static byte[] exportResumeToPdf(ResumeDTO resumeDTO) throws BusinessExcep
110112
String fieldLabel = field.getFieldLabel() != null ? field.getFieldLabel() : "未知字段";
111113
String fieldValue = field.getFieldValue() != null ? field.getFieldValue() : "";
112114

113-
System.out.println("添加字段: " + fieldLabel + " = " + fieldValue);
115+
System.out.println("添加字段: " + fieldLabel + " = " +
116+
(isBase64Image(fieldValue) ? "[图片数据]" : fieldValue));
114117

115118
PdfPCell fieldLabelCell = new PdfPCell(new Paragraph(fieldLabel, normalFont));
116-
PdfPCell fieldValueCell = new PdfPCell(new Paragraph(fieldValue, normalFont));
119+
PdfPCell fieldValueCell;
120+
121+
// 检查是否为Base64图片
122+
if (isBase64Image(fieldValue)) {
123+
Image image = createImageFromBase64(fieldValue);
124+
if (image != null) {
125+
// 使用图片创建单元格
126+
fieldValueCell = new PdfPCell(image, true);
127+
fieldValueCell.setHorizontalAlignment(Element.ALIGN_CENTER);
128+
fieldValueCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
129+
fieldValueCell.setPadding(5f);
130+
System.out.println("成功添加图片到PDF: " + fieldLabel);
131+
} else {
132+
// 图片转换失败,显示错误信息
133+
fieldValueCell = new PdfPCell(new Paragraph("[图片加载失败]", normalFont));
134+
System.err.println("图片转换失败: " + fieldLabel);
135+
}
136+
} else {
137+
// 普通文本字段
138+
fieldValueCell = new PdfPCell(new Paragraph(fieldValue, normalFont));
139+
}
117140

118141
fieldLabelCell.setBorder(Rectangle.BOX);
119142
fieldValueCell.setBorder(Rectangle.BOX);
@@ -247,6 +270,51 @@ private static String getStatusText(Integer status) {
247270
}
248271
}
249272

273+
/**
274+
* 检查字段值是否为Base64图片
275+
* @param fieldValue 字段值
276+
* @return 如果是Base64图片返回true
277+
*/
278+
private static boolean isBase64Image(String fieldValue) {
279+
if (fieldValue == null || fieldValue.length() < 20) {
280+
return false;
281+
}
282+
return fieldValue.startsWith("data:image/") && fieldValue.contains("base64,");
283+
}
284+
285+
/**
286+
* 将Base64图片字符串转换为Image对象
287+
* @param base64String Base64图片字符串
288+
* @return Image对象,如果转换失败返回null
289+
*/
290+
private static Image createImageFromBase64(String base64String) {
291+
try {
292+
// 提取Base64数据部分(去掉data:image/jpeg;base64,前缀)
293+
String base64Data = base64String.substring(base64String.indexOf(",") + 1);
294+
295+
// 解码Base64
296+
byte[] imageBytes = Base64.getDecoder().decode(base64Data);
297+
298+
// 创建Image对象
299+
Image image = Image.getInstance(imageBytes);
300+
301+
// 设置图片大小
302+
float maxWidth = 120f; // 最大宽度
303+
float maxHeight = 120f; // 最大高度
304+
305+
if (image.getWidth() > maxWidth || image.getHeight() > maxHeight) {
306+
image.scaleToFit(maxWidth, maxHeight);
307+
}
308+
309+
System.out.println("成功解析Base64图片,原始尺寸: " + image.getPlainWidth() + "x" + image.getPlainHeight());
310+
return image;
311+
312+
} catch (Exception e) {
313+
System.err.println("Base64图片转换失败: " + e.getMessage());
314+
return null;
315+
}
316+
}
317+
250318
/**
251319
* 格式化日期时间
252320
* @param dateTime 日期时间

0 commit comments

Comments
 (0)