This commit is contained in:
@@ -126,11 +126,11 @@ func (fm *FontManager) tryAddFont(pdf *gofpdf.Fpdf, fontPath, fontName string) b
|
||||
absFontPath = newAbsPath
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 使用filepath.ToSlash统一路径分隔符(Linux下使用/)
|
||||
// 注意:ToSlash不会改变路径的绝对/相对性质,只统一分隔符
|
||||
normalizedPath := filepath.ToSlash(absFontPath)
|
||||
|
||||
|
||||
// 在Linux下,绝对路径必须以/开头
|
||||
// 如果normalizedPath不是以/开头,说明转换有问题
|
||||
if len(normalizedPath) == 0 || normalizedPath[0] != '/' {
|
||||
@@ -166,14 +166,14 @@ func (fm *FontManager) tryAddFont(pdf *gofpdf.Fpdf, fontPath, fontName string) b
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
// 记录传递给gofpdf的实际路径
|
||||
fm.logger.Info("添加字体到gofpdf",
|
||||
zap.String("font_path", normalizedPath),
|
||||
zap.String("font_name", fontName),
|
||||
zap.Bool("is_absolute", len(normalizedPath) > 0 && normalizedPath[0] == '/'),
|
||||
)
|
||||
|
||||
|
||||
pdf.AddUTF8Font(fontName, "", normalizedPath) // 常规样式
|
||||
pdf.AddUTF8Font(fontName, "B", normalizedPath) // 粗体样式
|
||||
|
||||
@@ -223,7 +223,6 @@ func (fm *FontManager) getWatermarkFontPaths() []string {
|
||||
return fm.buildFontPaths(fontNames)
|
||||
}
|
||||
|
||||
|
||||
// buildFontPaths 构建字体文件路径列表(仅从resources/pdf/fonts加载)
|
||||
// 返回所有存在的字体文件的绝对路径
|
||||
func (fm *FontManager) buildFontPaths(fontNames []string) []string {
|
||||
|
||||
@@ -177,7 +177,7 @@ func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *ent
|
||||
// 这样gofpdf在Output时使用相对路径 app/resources/pdf/fonts 就能正确解析
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
|
||||
// 在Output前验证字体文件路径(此时工作目录应该是根目录/)
|
||||
if workDir, err := os.Getwd(); err == nil {
|
||||
// 验证绝对路径
|
||||
@@ -188,7 +188,7 @@ func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *ent
|
||||
zap.String("work_dir", workDir),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
// 验证相对路径(gofpdf可能使用的路径)
|
||||
fontRelPath := "app/resources/pdf/fonts/simhei.ttf"
|
||||
if relAbsPath, err := filepath.Abs(fontRelPath); err == nil {
|
||||
@@ -207,14 +207,14 @@ func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *ent
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
g.logger.Debug("准备生成PDF",
|
||||
zap.String("work_dir", workDir),
|
||||
zap.String("resources_pdf_dir", resourcesDir),
|
||||
zap.Bool("work_dir_changed", workDirChanged),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
err = pdf.Output(&buf)
|
||||
if err != nil {
|
||||
// 记录详细的错误信息
|
||||
@@ -222,7 +222,7 @@ func (g *PDFGeneratorRefactored) generatePDF(product *entities.Product, doc *ent
|
||||
if wd, e := os.Getwd(); e == nil {
|
||||
currentWorkDir = wd
|
||||
}
|
||||
|
||||
|
||||
// 尝试分析错误:如果是路径问题,记录更多信息
|
||||
errStr := err.Error()
|
||||
if strings.Contains(errStr, "stat ") && strings.Contains(errStr, ": no such file") {
|
||||
|
||||
Reference in New Issue
Block a user