This commit is contained in:
2025-12-04 12:30:33 +08:00
parent 7b45b43a0e
commit 4ce8fe4023
10 changed files with 224 additions and 107 deletions

View File

@@ -61,32 +61,39 @@ func (g *PDFGeneratorRefactored) findLogo() {
_, filename, _, _ := runtime.Caller(0)
baseDir := filepath.Dir(filename)
// 优先使用相对路径Linux风格使用正斜杠
// 优先使用 baseDir最可靠基于当前文件位置
logoPaths := []string{
"internal/shared/pdf/天远数据.png", // 相对于项目根目录(最常用
"./internal/shared/pdf/天远数据.png", // 当前目录下的相对路径
filepath.Join(baseDir, "天远数据.png"), // 相对当前文件
filepath.Join(baseDir, "天远数据.png"), // 相对当前文件(最优先
}
// 尝试相对路径
for _, logoPath := range logoPaths {
if _, err := os.Stat(logoPath); err == nil {
g.logoPath = logoPath
return
}
// 尝试相对于工作目录的路径
if workDir, err := os.Getwd(); err == nil {
logoPaths = append(logoPaths,
filepath.Join(workDir, "internal", "shared", "pdf", "天远数据.png"),
filepath.Join(workDir, "tyapi-server", "internal", "shared", "pdf", "天远数据.png"),
)
}
// 尝试服务器绝对路径(后备方案
// 尝试服务器绝对路径(Linux环境优先查找 /www/tyapi-server
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
}
logoPaths = append(logoPaths, serverPaths...)
}
// 尝试相对路径(作为最后的后备方案)
logoPaths = append(logoPaths,
"internal/shared/pdf/天远数据.png",
"./internal/shared/pdf/天远数据.png",
)
// 尝试所有路径
for _, logoPath := range logoPaths {
if _, err := os.Stat(logoPath); err == nil {
g.logoPath = logoPath
return
}
}
@@ -132,7 +139,6 @@ func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *ent
}
}()
// 创建PDF文档 (A4大小gofpdf v2 默认支持UTF-8)
pdf := gofpdf.New("P", "mm", "A4", "")
// 优化边距,减少空白
@@ -149,7 +155,6 @@ func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *ent
pdf.SetAuthor("TYAPI Server", true)
pdf.SetCreator("TYAPI Server", true)
// 创建页面构建器
pageBuilder := NewPageBuilder(g.logger, g.fontManager, g.textProcessor, g.markdownProc, g.tableParser, g.tableRenderer, g.jsonProcessor, g.logoPath, g.watermarkText)
@@ -173,4 +178,3 @@ func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *ent
return pdfBytes, nil
}