pdf修正字体

This commit is contained in:
2026-01-12 15:29:58 +08:00
parent bba5859343
commit 2d7f0526ba

View File

@@ -122,10 +122,10 @@ public class ArticlePdfGenerator {
margin: 2cm; margin: 2cm;
} }
body { 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; font-size: 12pt;
line-height: 1.8; line-height: 1.8;
color: #333; color: #333333;
} }
.article-header { .article-header {
margin-bottom: 30px; margin-bottom: 30px;
@@ -141,7 +141,7 @@ public class ArticlePdfGenerator {
} }
.meta { .meta {
font-size: 10pt; font-size: 10pt;
color: #666; color: #666666;
text-align: center; text-align: center;
} }
.meta span { .meta span {
@@ -171,14 +171,14 @@ public class ArticlePdfGenerator {
margin: 15px 0; margin: 15px 0;
} }
.article-content table td, .article-content table th { .article-content table td, .article-content table th {
border: 1px solid #ddd; border: 1px solid #dddddd;
padding: 8px; padding: 8px;
} }
.article-content blockquote { .article-content blockquote {
border-left: 4px solid #1890ff; border-left: 4px solid #1890ff;
padding-left: 15px; padding-left: 15px;
margin: 15px 0; margin: 15px 0;
color: #666; color: #666666;
} }
"""; """;
} }
@@ -243,28 +243,46 @@ public class ArticlePdfGenerator {
*/ */
private void addFontSupport(ITextRenderer renderer) { private void addFontSupport(ITextRenderer renderer) {
try { try {
// 尝试添加系统字体 // 尝试添加系统字体嵌入字体以确保PDF可移植
String[] fontPaths = { String[] fontPaths = {
"C:/Windows/Fonts/simsun.ttc", // Windows 宋体 // Windows
"C:/Windows/Fonts/msyh.ttc", // Windows 微软雅黑 "C:/Windows/Fonts/simsun.ttc", // 宋体
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc", // Linux 文泉驿微米 "C:/Windows/Fonts/msyh.ttc", // 微软雅
"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", // Linux 文泉驿正 "C:/Windows/Fonts/simhei.ttf", // 黑
"/System/Library/Fonts/PingFang.ttc" // macOS 苹方 // 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) { for (String fontPath : fontPaths) {
File fontFile = new File(fontPath); File fontFile = new File(fontPath);
if (fontFile.exists()) { if (fontFile.exists()) {
renderer.getFontResolver().addFont( try {
fontPath, renderer.getFontResolver().addFont(
BaseFont.IDENTITY_H, fontPath,
BaseFont.NOT_EMBEDDED BaseFont.IDENTITY_H,
); BaseFont.EMBEDDED // 嵌入字体确保PDF可移植
logger.debug("已加载字体: {}", fontPath); );
logger.info("已加载并嵌入字体: {}", fontPath);
fontLoaded = true;
} catch (Exception e) {
logger.warn("加载字体失败: {}, 错误: {}", fontPath, e.getMessage());
}
} }
} }
if (!fontLoaded) {
logger.error("未能加载任何中文字体PDF中的中文可能无法正常显示。请确保服务器安装了中文字体。");
}
} catch (Exception e) { } catch (Exception e) {
logger.warn("加载字体失败,将使用默认字体: {}", e.getMessage()); logger.error("字体加载过程异常: {}", e.getMessage(), e);
} }
} }
} }