This commit is contained in:
@@ -32,11 +32,6 @@ func NewFontManager(logger *zap.Logger) *FontManager {
|
||||
baseDir: baseDir,
|
||||
}
|
||||
|
||||
// 记录基础目录(用于调试)
|
||||
logger.Debug("字体管理器初始化",
|
||||
zap.String("base_dir", baseDir),
|
||||
zap.String("caller_file", filename))
|
||||
|
||||
return fm
|
||||
}
|
||||
|
||||
@@ -48,27 +43,19 @@ func (fm *FontManager) LoadChineseFont(pdf *gofpdf.Fpdf) bool {
|
||||
|
||||
fontPaths := fm.getChineseFontPaths()
|
||||
if len(fontPaths) == 0 {
|
||||
fm.logger.Warn("未找到中文字体文件,PDF中的中文可能显示为空白",
|
||||
zap.String("base_dir", fm.baseDir),
|
||||
zap.String("hint", "请确保字体文件存在于 internal/shared/pdf/fonts/ 目录下,或设置 PDF_FONT_DIR 环境变量"))
|
||||
fm.logger.Warn("未找到中文字体文件")
|
||||
return false
|
||||
}
|
||||
|
||||
// 尝试加载字体
|
||||
for _, fontPath := range fontPaths {
|
||||
fm.logger.Debug("尝试加载字体", zap.String("font_path", fontPath))
|
||||
if fm.tryAddFont(pdf, fontPath, fm.chineseFontName) {
|
||||
fm.chineseFontLoaded = true
|
||||
fm.logger.Info("成功加载中文字体", zap.String("font_path", fontPath))
|
||||
return true
|
||||
} else {
|
||||
fm.logger.Debug("字体加载失败", zap.String("font_path", fontPath))
|
||||
}
|
||||
}
|
||||
|
||||
fm.logger.Warn("所有中文字体文件都无法加载,PDF中的中文可能显示为空白",
|
||||
zap.Int("tried_paths", len(fontPaths)),
|
||||
zap.Strings("font_paths", fontPaths))
|
||||
fm.logger.Warn("无法加载中文字体文件")
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -80,7 +67,6 @@ func (fm *FontManager) LoadWatermarkFont(pdf *gofpdf.Fpdf) bool {
|
||||
|
||||
fontPaths := fm.getWatermarkFontPaths()
|
||||
if len(fontPaths) == 0 {
|
||||
fm.logger.Warn("未找到水印字体文件,将使用主字体")
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -88,12 +74,10 @@ func (fm *FontManager) LoadWatermarkFont(pdf *gofpdf.Fpdf) bool {
|
||||
for _, fontPath := range fontPaths {
|
||||
if fm.tryAddFont(pdf, fontPath, fm.watermarkFontName) {
|
||||
fm.watermarkFontLoaded = true
|
||||
fm.logger.Info("成功加载水印字体", zap.String("font_path", fontPath))
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
fm.logger.Warn("所有水印字体文件都无法加载,将使用主字体")
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -101,10 +85,7 @@ func (fm *FontManager) LoadWatermarkFont(pdf *gofpdf.Fpdf) bool {
|
||||
func (fm *FontManager) tryAddFont(pdf *gofpdf.Fpdf, fontPath, fontName string) bool {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fm.logger.Error("添加字体时发生panic",
|
||||
zap.Any("panic", r),
|
||||
zap.String("font_path", fontPath),
|
||||
zap.String("font_name", fontName))
|
||||
// 静默处理,不记录日志
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -113,18 +94,25 @@ func (fm *FontManager) tryAddFont(pdf *gofpdf.Fpdf, fontPath, fontName string) b
|
||||
return false
|
||||
}
|
||||
|
||||
// gofpdf v2使用AddUTF8Font添加支持UTF-8的字体
|
||||
pdf.AddUTF8Font(fontName, "", fontPath) // 常规样式
|
||||
pdf.AddUTF8Font(fontName, "B", fontPath) // 粗体样式
|
||||
// 将相对路径转换为绝对路径(gofpdf在Output时需要绝对路径)
|
||||
absFontPath, err := filepath.Abs(fontPath)
|
||||
if err != nil {
|
||||
absFontPath = fontPath
|
||||
}
|
||||
|
||||
// 再次检查绝对路径文件是否存在
|
||||
if _, err := os.Stat(absFontPath); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// gofpdf v2使用AddUTF8Font添加支持UTF-8的字体(使用绝对路径)
|
||||
pdf.AddUTF8Font(fontName, "", absFontPath) // 常规样式
|
||||
pdf.AddUTF8Font(fontName, "B", absFontPath) // 粗体样式
|
||||
|
||||
// 验证字体是否可用
|
||||
pdf.SetFont(fontName, "", 12)
|
||||
testWidth := pdf.GetStringWidth("测试")
|
||||
if testWidth == 0 {
|
||||
fm.logger.Debug("字体加载后无法使用",
|
||||
zap.String("font_path", fontPath),
|
||||
zap.String("font_name", fontName),
|
||||
zap.Float64("test_width", testWidth))
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -158,127 +146,79 @@ func (fm *FontManager) getWatermarkFontPaths() []string {
|
||||
func (fm *FontManager) buildFontPaths(fontNames []string) []string {
|
||||
var fontPaths []string
|
||||
|
||||
// 方式1: 使用 pdf/fonts/ 目录(相对于当前文件)
|
||||
// 方式1: 优先使用相对路径(Linux风格,使用正斜杠)- 最常用
|
||||
for _, fontName := range fontNames {
|
||||
// 相对路径(相对于项目根目录)
|
||||
relativePaths := []string{
|
||||
"internal/shared/pdf/fonts/" + fontName,
|
||||
"./internal/shared/pdf/fonts/" + fontName,
|
||||
}
|
||||
fontPaths = append(fontPaths, relativePaths...)
|
||||
}
|
||||
|
||||
// 方式2: 使用 pdf/fonts/ 目录(相对于当前文件)
|
||||
for _, fontName := range fontNames {
|
||||
fontPaths = append(fontPaths, filepath.Join(fm.baseDir, "fonts", fontName))
|
||||
}
|
||||
|
||||
// 方式2: 尝试相对于工作目录的路径(适用于编译后的二进制文件)
|
||||
// 这是最常用的方式,适用于直接运行二进制文件的情况
|
||||
// 方式3: 尝试相对于工作目录的路径
|
||||
if workDir, err := os.Getwd(); err == nil {
|
||||
fm.logger.Debug("工作目录", zap.String("work_dir", workDir))
|
||||
for _, fontName := range fontNames {
|
||||
// 尝试多个可能的相对路径
|
||||
paths := []string{
|
||||
filepath.Join(workDir, "internal", "shared", "pdf", "fonts", fontName),
|
||||
filepath.Join(workDir, "fonts", fontName),
|
||||
filepath.Join(workDir, fontName),
|
||||
// 如果工作目录是项目根目录的父目录
|
||||
filepath.Join(workDir, "tyapi-server", "internal", "shared", "pdf", "fonts", fontName),
|
||||
}
|
||||
fontPaths = append(fontPaths, paths...)
|
||||
}
|
||||
} else {
|
||||
fm.logger.Warn("无法获取工作目录", zap.Error(err))
|
||||
}
|
||||
|
||||
// 方式3: 尝试使用可执行文件所在目录
|
||||
// 适用于systemd服务或直接运行二进制文件的情况
|
||||
// 方式4: 尝试使用可执行文件所在目录
|
||||
if execPath, err := os.Executable(); err == nil {
|
||||
execDir := filepath.Dir(execPath)
|
||||
// 解析符号链接,获取真实路径(Linux上很重要)
|
||||
if realPath, err := filepath.EvalSymlinks(execPath); err == nil {
|
||||
execDir = filepath.Dir(realPath)
|
||||
}
|
||||
fm.logger.Debug("可执行文件目录",
|
||||
zap.String("exec_dir", execDir),
|
||||
zap.String("exec_path", execPath))
|
||||
for _, fontName := range fontNames {
|
||||
paths := []string{
|
||||
filepath.Join(execDir, "internal", "shared", "pdf", "fonts", fontName),
|
||||
filepath.Join(execDir, "fonts", fontName),
|
||||
filepath.Join(execDir, fontName),
|
||||
// 如果可执行文件在bin目录,尝试上一级目录
|
||||
filepath.Join(filepath.Dir(execDir), "internal", "shared", "pdf", "fonts", fontName),
|
||||
}
|
||||
fontPaths = append(fontPaths, paths...)
|
||||
}
|
||||
} else {
|
||||
fm.logger.Warn("无法获取可执行文件路径", zap.Error(err))
|
||||
}
|
||||
|
||||
// 方式4: 尝试使用环境变量指定的路径(如果设置了)
|
||||
// 方式5: 尝试使用环境变量指定的路径
|
||||
if fontDir := os.Getenv("PDF_FONT_DIR"); fontDir != "" {
|
||||
fm.logger.Info("使用环境变量指定的字体目录", zap.String("font_dir", fontDir))
|
||||
for _, fontName := range fontNames {
|
||||
fontPaths = append(fontPaths, filepath.Join(fontDir, fontName))
|
||||
}
|
||||
}
|
||||
|
||||
// 方式5: 尝试常见的服务器部署路径(硬编码路径,作为后备方案)
|
||||
// 支持多种Linux服务器部署场景
|
||||
commonServerPaths := []string{
|
||||
"/www/tyapi-server/internal/shared/pdf/fonts", // 用户提供的路径
|
||||
"/app/internal/shared/pdf/fonts", // Docker常见路径
|
||||
"/usr/local/tyapi-server/internal/shared/pdf/fonts", // 标准安装路径
|
||||
"/opt/tyapi-server/internal/shared/pdf/fonts", // 可选安装路径
|
||||
"/home/ubuntu/tyapi-server/internal/shared/pdf/fonts", // Ubuntu用户目录
|
||||
"/root/tyapi-server/internal/shared/pdf/fonts", // root用户目录
|
||||
"/var/www/tyapi-server/internal/shared/pdf/fonts", // Web服务器常见路径
|
||||
}
|
||||
for _, basePath := range commonServerPaths {
|
||||
for _, fontName := range fontNames {
|
||||
fontPaths = append(fontPaths, filepath.Join(basePath, fontName))
|
||||
// 方式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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 记录所有尝试的路径(用于调试)
|
||||
fm.logger.Debug("查找字体文件",
|
||||
zap.Strings("font_names", fontNames),
|
||||
zap.Int("total_paths", len(fontPaths)),
|
||||
zap.Strings("paths", fontPaths))
|
||||
|
||||
// 过滤出实际存在的字体文件(并检查权限)
|
||||
// 过滤出实际存在的字体文件
|
||||
var existingFonts []string
|
||||
for _, fontPath := range fontPaths {
|
||||
// 检查文件是否存在
|
||||
fileInfo, err := os.Stat(fontPath)
|
||||
if err == nil {
|
||||
// 检查是否为常规文件(不是目录)
|
||||
if !fileInfo.Mode().IsRegular() {
|
||||
fm.logger.Debug("路径不是常规文件",
|
||||
zap.String("font_path", fontPath),
|
||||
zap.String("mode", fileInfo.Mode().String()))
|
||||
continue
|
||||
}
|
||||
// 检查是否有读取权限(Linux上很重要)
|
||||
if fileInfo.Mode().Perm()&0444 == 0 {
|
||||
fm.logger.Warn("字体文件无读取权限",
|
||||
zap.String("font_path", fontPath),
|
||||
zap.String("mode", fileInfo.Mode().String()))
|
||||
// 仍然尝试添加,因为可能通过其他方式有权限
|
||||
}
|
||||
if err == nil && fileInfo.Mode().IsRegular() {
|
||||
existingFonts = append(existingFonts, fontPath)
|
||||
fm.logger.Debug("找到字体文件",
|
||||
zap.String("font_path", fontPath),
|
||||
zap.Int64("size", fileInfo.Size()))
|
||||
} else {
|
||||
// 只在Debug级别记录,避免日志过多
|
||||
fm.logger.Debug("字体文件不存在",
|
||||
zap.String("font_path", fontPath),
|
||||
zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// 只记录关键错误
|
||||
if len(existingFonts) == 0 {
|
||||
fm.logger.Warn("未找到任何字体文件",
|
||||
zap.Strings("font_names", fontNames),
|
||||
zap.String("base_dir", fm.baseDir),
|
||||
zap.Int("total_paths_checked", len(fontPaths)))
|
||||
} else {
|
||||
fm.logger.Info("找到字体文件",
|
||||
zap.Int("count", len(existingFonts)),
|
||||
zap.Strings("paths", existingFonts))
|
||||
fm.logger.Warn("未找到字体文件", zap.Strings("font_names", fontNames))
|
||||
}
|
||||
|
||||
return existingFonts
|
||||
|
||||
Reference in New Issue
Block a user