This commit is contained in:
2025-12-04 12:56:39 +08:00
parent 4ce8fe4023
commit f12c3fb8ad
12 changed files with 86 additions and 227 deletions

View File

@@ -3,7 +3,6 @@ package pdf
import (
"os"
"path/filepath"
"runtime"
"github.com/jung-kurt/gofpdf/v2"
"go.uber.org/zap"
@@ -16,23 +15,15 @@ type FontManager struct {
chineseFontLoaded bool
watermarkFontName string
watermarkFontLoaded bool
baseDir string // 缓存基础目录,避免重复计算
}
// NewFontManager 创建字体管理器
func NewFontManager(logger *zap.Logger) *FontManager {
// 获取当前文件所在目录
_, filename, _, _ := runtime.Caller(0)
baseDir := filepath.Dir(filename)
fm := &FontManager{
return &FontManager{
logger: logger,
chineseFontName: "ChineseFont",
watermarkFontName: "WatermarkFont",
baseDir: baseDir,
}
return fm
}
// LoadChineseFont 加载中文字体到PDF
@@ -133,77 +124,28 @@ func (fm *FontManager) getChineseFontPaths() []string {
// getWatermarkFontPaths 获取水印字体路径列表仅TTF格式
func (fm *FontManager) getWatermarkFontPaths() []string {
// 水印字体文件名(支持大小写变体)
// 水印字体文件名(尝试大小写变体)
fontNames := []string{
"yunfengfeiyunti-2.ttf",
"YunFengFeiYunTi-2.ttf", // 写版本(兼容)
"YunFengFeiYunTi-2.ttf", // 优先尝试大写版本
"yunfengfeiyunti-2.ttf", // 写版本(兼容)
}
return fm.buildFontPaths(fontNames)
}
// buildFontPaths 构建字体文件路径列表(支持多种路径查找方式)
// buildFontPaths 构建字体文件路径列表仅从resources/pdf/fonts加载
func (fm *FontManager) buildFontPaths(fontNames []string) []string {
var fontPaths []string
// 方式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))
}
}
}
// 获取resources/pdf目录使用统一的资源路径查找函数
resourcesPDFDir := GetResourcesPDFDir()
fontsDir := filepath.Join(resourcesPDFDir, "fonts")
// 方式2: 使用 pdf/fonts/ 目录(相对于当前文件)
// 只从resources/pdf/fonts目录加载
for _, fontName := range fontNames {
fontPaths = append(fontPaths, filepath.Join(fm.baseDir, "fonts", fontName))
}
// 方式3: 尝试相对于工作目录的路径
if workDir, err := os.Getwd(); err == nil {
for _, fontName := range fontNames {
paths := []string{
filepath.Join(workDir, "internal", "shared", "pdf", "fonts", fontName),
filepath.Join(workDir, "tyapi-server", "internal", "shared", "pdf", "fonts", fontName),
}
fontPaths = append(fontPaths, paths...)
}
}
// 方式4: 尝试使用可执行文件所在目录
if execPath, err := os.Executable(); err == nil {
execDir := filepath.Dir(execPath)
if realPath, err := filepath.EvalSymlinks(execPath); err == nil {
execDir = filepath.Dir(realPath)
}
for _, fontName := range fontNames {
paths := []string{
filepath.Join(execDir, "internal", "shared", "pdf", "fonts", fontName),
filepath.Join(filepath.Dir(execDir), "internal", "shared", "pdf", "fonts", fontName),
}
fontPaths = append(fontPaths, paths...)
}
}
// 方式5: 尝试使用环境变量指定的路径
if fontDir := os.Getenv("PDF_FONT_DIR"); fontDir != "" {
for _, fontName := range fontNames {
fontPaths = append(fontPaths, filepath.Join(fontDir, fontName))
}
}
// 方式6: 相对路径(作为最后的后备方案)
for _, fontName := range fontNames {
relativePaths := []string{
"internal/shared/pdf/fonts/" + fontName,
"./internal/shared/pdf/fonts/" + fontName,
}
fontPaths = append(fontPaths, relativePaths...)
fontPath := filepath.Join(fontsDir, fontName)
fontPaths = append(fontPaths, fontPath)
}
// 过滤出实际存在的字体文件
@@ -215,8 +157,6 @@ func (fm *FontManager) buildFontPaths(fontNames []string) []string {
}
}
// 字体文件不存在时不记录警告,使用系统默认字体即可
return existingFonts
}