This commit is contained in:
2025-12-04 14:21:58 +08:00
parent e57bef6609
commit 6a2241bc66
2 changed files with 91 additions and 9 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"tyapi-server/internal/domains/product/entities"
@@ -139,14 +140,66 @@ func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *ent
}
// 生成PDF字节流
// 注意gofpdf在Output时会重新访问字体文件,确保路径正确
// 注意gofpdf在Output时会重新访问字体文件
// 保存当前工作目录
originalWorkDir, _ := os.Getwd()
// 关键修复gofpdf在Output时会将绝对路径 /app/resources/pdf/fonts/simhei.ttf
// 转换为相对路径 app/resources/pdf/fonts/simhei.ttf去掉开头的/
//
// 为什么开发环境可以,生产环境不行?
// 1. Windows开发环境路径格式为 C:\...,不以/开头gofpdf处理方式不同
// 2. Linux生产环境路径格式为 /app/...,以/开头gofpdf在Output时会去掉开头的/
// 如果工作目录是 /app相对路径 app/resources 无法正确解析
// 3. 解决方案:将工作目录切换到根目录(/),这样相对路径 app/resources 就能解析为 /app/resources
resourcesDir := GetResourcesPDFDir()
if resourcesDir != "" && len(resourcesDir) > 0 && resourcesDir[0] == '/' {
// 切换到根目录,这样 gofpdf 转换后的相对路径 app/resources/pdf/fonts 就能解析为 /app/resources/pdf/fonts
if err := os.Chdir("/"); err == nil {
g.logger.Info("临时切换工作目录到根目录以修复gofpdf路径问题",
zap.String("reason", "gofpdf在Linux环境下会将绝对路径去掉开头的/转换为相对路径"),
zap.String("original_work_dir", originalWorkDir),
zap.String("new_work_dir", "/"),
zap.String("resources_dir", resourcesDir),
zap.String("example", "gofpdf将 /app/resources/pdf/fonts 转换为 app/resources/pdf/fonts需要工作目录为/才能正确解析"),
)
defer func() {
// 恢复原始工作目录
if originalWorkDir != "" {
if err := os.Chdir(originalWorkDir); err == nil {
g.logger.Debug("已恢复原始工作目录", zap.String("work_dir", originalWorkDir))
}
}
}()
} else {
g.logger.Warn("无法切换到根目录", zap.Error(err))
}
}
var buf bytes.Buffer
// 在Output前记录环境信息,便于调试
// 在Output前验证字体文件路径
if workDir, err := os.Getwd(); err == nil {
fontPath := filepath.Join(resourcesDir, "fonts", "simhei.ttf")
// 验证字体文件是否存在(使用绝对路径)
if absFontPath, err := filepath.Abs(fontPath); err == nil {
if _, err := os.Stat(absFontPath); err == nil {
g.logger.Debug("Output前验证字体文件存在",
zap.String("font_path", absFontPath),
zap.String("work_dir", workDir),
)
} else {
g.logger.Warn("Output前字体文件不存在",
zap.String("font_path", absFontPath),
zap.Error(err),
)
}
}
g.logger.Debug("准备生成PDF",
zap.String("work_dir", workDir),
zap.String("resources_pdf_dir", GetResourcesPDFDir()),
zap.String("resources_pdf_dir", resourcesDir),
)
}
@@ -157,11 +210,23 @@ func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *ent
if wd, e := os.Getwd(); e == nil {
currentWorkDir = wd
}
g.logger.Error("PDF Output失败",
zap.Error(err),
zap.String("current_work_dir", currentWorkDir),
zap.String("resources_pdf_dir", GetResourcesPDFDir()),
)
// 尝试分析错误:如果是路径问题,记录更多信息
errStr := err.Error()
if strings.Contains(errStr, "stat ") && strings.Contains(errStr, ": no such file") {
g.logger.Error("PDF Output失败字体文件路径问题",
zap.Error(err),
zap.String("current_work_dir", currentWorkDir),
zap.String("resources_pdf_dir", resourcesDir),
zap.String("error_message", errStr),
)
} else {
g.logger.Error("PDF Output失败",
zap.Error(err),
zap.String("current_work_dir", currentWorkDir),
zap.String("resources_pdf_dir", resourcesDir),
)
}
return nil, fmt.Errorf("生成PDF失败: %w", err)
}