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

@@ -9,7 +9,6 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/jung-kurt/gofpdf/v2"
@@ -51,106 +50,20 @@ func (g *PDFGenerator) registerChineseFont() string {
return "ChineseFont"
}
// getChineseFontPaths 获取中文字体路径(支持跨平台
func (g *PDFGenerator) getChineseFontPaths() []string {
var fontPaths []string
// Windows系统
if runtime.GOOS == "windows" {
fontPaths = []string{
`C:\Windows\Fonts\simhei.ttf`, // 黑体(优先,常用)
`C:\Windows\Fonts\simsun.ttf`, // 宋体如果存在单独的TTF文件
`C:\Windows\Fonts\msyh.ttf`, // 微软雅黑如果存在单独的TTF文件
`C:\Windows\Fonts\simkai.ttf`, // 楷体
}
} else if runtime.GOOS == "linux" {
// Linux系统常见字体路径
fontPaths = []string{
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc",
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttf",
"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttf",
"/usr/share/fonts/truetype/arphic/uming.ttc",
"/usr/share/fonts/truetype/arphic/ukai.ttc",
"/usr/share/fonts/truetype/arphic/uming.ttf",
"/usr/share/fonts/truetype/arphic/ukai.ttf",
"/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf",
"/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
"/usr/share/fonts/truetype/noto/NotoSansCJK-Bold.ttc",
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.otf",
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
"/usr/local/share/fonts/simhei.ttf",
"/usr/local/share/fonts/simsun.ttf",
"/usr/local/share/fonts/simkai.ttf",
"/opt/fonts/simhei.ttf",
"/opt/fonts/simsun.ttf",
"/opt/fonts/simkai.ttf",
"/home/.fonts/simhei.ttf",
"/home/.fonts/simsun.ttf",
"/home/.fonts/simkai.ttf",
"/root/.fonts/simhei.ttf",
"/root/.fonts/simsun.ttf",
"/root/.fonts/simkai.ttf",
}
} else if runtime.GOOS == "darwin" {
// macOS系统字体路径
fontPaths = []string{
"/System/Library/Fonts/PingFang.ttc",
"/System/Library/Fonts/STHeiti Light.ttc",
"/System/Library/Fonts/STSong.ttc",
"/Library/Fonts/Microsoft/SimHei.ttf",
}
}
// 过滤出实际存在的字体文件
var existingFonts []string
for _, fontPath := range fontPaths {
if _, err := os.Stat(fontPath); err == nil {
existingFonts = append(existingFonts, fontPath)
}
}
return existingFonts
}
// findLogo 查找logo文件
// findLogo 查找logo文件仅从resources/pdf加载
func (g *PDFGenerator) findLogo() {
// 获取当前文件所在目录
_, filename, _, _ := runtime.Caller(0)
baseDir := filepath.Dir(filename)
// 获取resources/pdf目录使用统一的资源路径查找函数
resourcesPDFDir := GetResourcesPDFDir()
logoPath := filepath.Join(resourcesPDFDir, "logo.png")
// 优先使用相对路径Linux风格使用正斜杠
logoPaths := []string{
"internal/shared/pdf/天远数据.png", // 相对于项目根目录(最常用)
"./internal/shared/pdf/天远数据.png", // 当前目录下的相对路径
filepath.Join(baseDir, "天远数据.png"), // 相对当前文件
}
// 尝试相对路径
for _, logoPath := range logoPaths {
if _, err := os.Stat(logoPath); err == nil {
g.logoPath = logoPath
return
}
}
// 尝试服务器绝对路径(后备方案)
if runtime.GOOS == "linux" {
serverPaths := []string{
"/www/tyapi-server/internal/shared/pdf/天远数据.png",
"/app/internal/shared/pdf/天远数据.png",
}
for _, logoPath := range serverPaths {
if _, err := os.Stat(logoPath); err == nil {
g.logoPath = logoPath
return
}
}
// 检查文件是否存在
if _, err := os.Stat(logoPath); err == nil {
g.logoPath = logoPath
return
}
// 只记录关键错误
g.logger.Warn("未找到logo文件")
g.logger.Warn("未找到logo文件", zap.String("path", logoPath))
}
// GenerateProductPDF 为产品生成PDF文档接受响应类型内部转换