|
15 | 15 | import java.time.LocalDateTime; |
16 | 16 | import java.time.format.DateTimeFormatter; |
17 | 17 | import java.util.Objects; |
| 18 | +import java.util.Base64; |
| 19 | +import java.io.ByteArrayInputStream; |
18 | 20 |
|
19 | 21 | /** |
20 | 22 | * PDF导出工具类 |
@@ -110,10 +112,31 @@ public static byte[] exportResumeToPdf(ResumeDTO resumeDTO) throws BusinessExcep |
110 | 112 | String fieldLabel = field.getFieldLabel() != null ? field.getFieldLabel() : "未知字段"; |
111 | 113 | String fieldValue = field.getFieldValue() != null ? field.getFieldValue() : ""; |
112 | 114 |
|
113 | | - System.out.println("添加字段: " + fieldLabel + " = " + fieldValue); |
| 115 | + System.out.println("添加字段: " + fieldLabel + " = " + |
| 116 | + (isBase64Image(fieldValue) ? "[图片数据]" : fieldValue)); |
114 | 117 |
|
115 | 118 | 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 | + } |
117 | 140 |
|
118 | 141 | fieldLabelCell.setBorder(Rectangle.BOX); |
119 | 142 | fieldValueCell.setBorder(Rectangle.BOX); |
@@ -247,6 +270,51 @@ private static String getStatusText(Integer status) { |
247 | 270 | } |
248 | 271 | } |
249 | 272 |
|
| 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 | + |
250 | 318 | /** |
251 | 319 | * 格式化日期时间 |
252 | 320 | * @param dateTime 日期时间 |
|
0 commit comments