From 2d7f0526ba2d33d3c47a3c968118a62ac8e44eba Mon Sep 17 00:00:00 2001 From: wangys <3401275564@qq.com> Date: Mon, 12 Jan 2026 15:29:58 +0800 Subject: [PATCH] =?UTF-8?q?pdf=E4=BF=AE=E6=AD=A3=E5=AD=97=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xyzh/news/util/ArticlePdfGenerator.java | 54 ++++++++++++------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/schoolNewsServ/news/src/main/java/org/xyzh/news/util/ArticlePdfGenerator.java b/schoolNewsServ/news/src/main/java/org/xyzh/news/util/ArticlePdfGenerator.java index 69b9312..5b7123f 100644 --- a/schoolNewsServ/news/src/main/java/org/xyzh/news/util/ArticlePdfGenerator.java +++ b/schoolNewsServ/news/src/main/java/org/xyzh/news/util/ArticlePdfGenerator.java @@ -122,10 +122,10 @@ public class ArticlePdfGenerator { margin: 2cm; } body { - font-family: SimSun, 'Microsoft YaHei', Arial, sans-serif; + font-family: 'WenQuanYi Micro Hei', 'Noto Sans CJK SC', 'Microsoft YaHei', SimSun, 'PingFang SC', sans-serif; font-size: 12pt; line-height: 1.8; - color: #333; + color: #333333; } .article-header { margin-bottom: 30px; @@ -141,7 +141,7 @@ public class ArticlePdfGenerator { } .meta { font-size: 10pt; - color: #666; + color: #666666; text-align: center; } .meta span { @@ -171,14 +171,14 @@ public class ArticlePdfGenerator { margin: 15px 0; } .article-content table td, .article-content table th { - border: 1px solid #ddd; + border: 1px solid #dddddd; padding: 8px; } .article-content blockquote { border-left: 4px solid #1890ff; padding-left: 15px; margin: 15px 0; - color: #666; + color: #666666; } """; } @@ -243,28 +243,46 @@ public class ArticlePdfGenerator { */ private void addFontSupport(ITextRenderer renderer) { try { - // 尝试添加系统字体 + // 尝试添加系统字体(嵌入字体以确保PDF可移植) String[] fontPaths = { - "C:/Windows/Fonts/simsun.ttc", // Windows 宋体 - "C:/Windows/Fonts/msyh.ttc", // Windows 微软雅黑 - "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc", // Linux 文泉驿微米黑 - "/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", // Linux 文泉驿正黑 - "/System/Library/Fonts/PingFang.ttc" // macOS 苹方 + // Windows + "C:/Windows/Fonts/simsun.ttc", // 宋体 + "C:/Windows/Fonts/msyh.ttc", // 微软雅黑 + "C:/Windows/Fonts/simhei.ttf", // 黑体 + // Linux + "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc", // 文泉驿微米黑 + "/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", // 文泉驿正黑 + "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc", // Noto Sans CJK + "/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc", + "/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc", + // macOS + "/System/Library/Fonts/PingFang.ttc", + "/System/Library/Fonts/STHeiti Light.ttc" }; + boolean fontLoaded = false; for (String fontPath : fontPaths) { File fontFile = new File(fontPath); if (fontFile.exists()) { - renderer.getFontResolver().addFont( - fontPath, - BaseFont.IDENTITY_H, - BaseFont.NOT_EMBEDDED - ); - logger.debug("已加载字体: {}", fontPath); + try { + renderer.getFontResolver().addFont( + fontPath, + BaseFont.IDENTITY_H, + BaseFont.EMBEDDED // 嵌入字体,确保PDF可移植 + ); + logger.info("已加载并嵌入字体: {}", fontPath); + fontLoaded = true; + } catch (Exception e) { + logger.warn("加载字体失败: {}, 错误: {}", fontPath, e.getMessage()); + } } } + + if (!fontLoaded) { + logger.error("未能加载任何中文字体!PDF中的中文可能无法正常显示。请确保服务器安装了中文字体。"); + } } catch (Exception e) { - logger.warn("加载字体失败,将使用默认字体: {}", e.getMessage()); + logger.error("字体加载过程异常: {}", e.getMessage(), e); } } }