This commit is contained in:
@@ -43,7 +43,7 @@ func (fm *FontManager) LoadChineseFont(pdf *gofpdf.Fpdf) bool {
|
||||
|
||||
fontPaths := fm.getChineseFontPaths()
|
||||
if len(fontPaths) == 0 {
|
||||
fm.logger.Warn("未找到中文字体文件")
|
||||
// 字体文件不存在,使用系统默认字体,不记录警告
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ func (fm *FontManager) LoadChineseFont(pdf *gofpdf.Fpdf) bool {
|
||||
}
|
||||
}
|
||||
|
||||
fm.logger.Warn("无法加载中文字体文件")
|
||||
// 无法加载字体,使用系统默认字体,不记录警告
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -146,14 +146,17 @@ func (fm *FontManager) getWatermarkFontPaths() []string {
|
||||
func (fm *FontManager) buildFontPaths(fontNames []string) []string {
|
||||
var fontPaths []string
|
||||
|
||||
// 方式1: 优先使用相对路径(Linux风格,使用正斜杠)- 最常用
|
||||
for _, fontName := range fontNames {
|
||||
// 相对路径(相对于项目根目录)
|
||||
relativePaths := []string{
|
||||
"internal/shared/pdf/fonts/" + fontName,
|
||||
"./internal/shared/pdf/fonts/" + fontName,
|
||||
// 方式1: 服务器绝对路径(Linux环境,优先检查,因为服务器上文件通常在这里)
|
||||
if runtime.GOOS == "linux" {
|
||||
commonServerPaths := []string{
|
||||
"/www/tyapi-server/internal/shared/pdf/fonts",
|
||||
"/app/internal/shared/pdf/fonts",
|
||||
}
|
||||
for _, basePath := range commonServerPaths {
|
||||
for _, fontName := range fontNames {
|
||||
fontPaths = append(fontPaths, filepath.Join(basePath, fontName))
|
||||
}
|
||||
}
|
||||
fontPaths = append(fontPaths, relativePaths...)
|
||||
}
|
||||
|
||||
// 方式2: 使用 pdf/fonts/ 目录(相对于当前文件)
|
||||
@@ -194,17 +197,13 @@ func (fm *FontManager) buildFontPaths(fontNames []string) []string {
|
||||
}
|
||||
}
|
||||
|
||||
// 方式6: 服务器绝对路径(作为最后的后备方案)
|
||||
if runtime.GOOS == "linux" {
|
||||
commonServerPaths := []string{
|
||||
"/www/tyapi-server/internal/shared/pdf/fonts",
|
||||
"/app/internal/shared/pdf/fonts",
|
||||
}
|
||||
for _, basePath := range commonServerPaths {
|
||||
for _, fontName := range fontNames {
|
||||
fontPaths = append(fontPaths, filepath.Join(basePath, fontName))
|
||||
}
|
||||
// 方式6: 相对路径(作为最后的后备方案)
|
||||
for _, fontName := range fontNames {
|
||||
relativePaths := []string{
|
||||
"internal/shared/pdf/fonts/" + fontName,
|
||||
"./internal/shared/pdf/fonts/" + fontName,
|
||||
}
|
||||
fontPaths = append(fontPaths, relativePaths...)
|
||||
}
|
||||
|
||||
// 过滤出实际存在的字体文件
|
||||
@@ -216,22 +215,7 @@ func (fm *FontManager) buildFontPaths(fontNames []string) []string {
|
||||
}
|
||||
}
|
||||
|
||||
// 只记录关键错误,并显示调试信息
|
||||
if len(existingFonts) == 0 {
|
||||
workDir, _ := os.Getwd()
|
||||
execPath, _ := os.Executable()
|
||||
fm.logger.Warn("未找到字体文件",
|
||||
zap.Strings("font_names", fontNames),
|
||||
zap.String("work_dir", workDir),
|
||||
zap.String("exec_path", execPath),
|
||||
zap.String("base_dir", fm.baseDir),
|
||||
zap.String("first_tried_path", func() string {
|
||||
if len(fontPaths) > 0 {
|
||||
return fontPaths[0]
|
||||
}
|
||||
return "none"
|
||||
}()))
|
||||
}
|
||||
// 字体文件不存在时不记录警告,使用系统默认字体即可
|
||||
|
||||
return existingFonts
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user