This commit is contained in:
@@ -43,7 +43,7 @@ func (fm *FontManager) LoadChineseFont(pdf *gofpdf.Fpdf) bool {
|
||||
|
||||
fontPaths := fm.getChineseFontPaths()
|
||||
if len(fontPaths) == 0 {
|
||||
fm.logger.Warn("未找到中文字体文件")
|
||||
// 字体文件不存在,使用系统默认字体,不记录警告
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ func (fm *FontManager) LoadChineseFont(pdf *gofpdf.Fpdf) bool {
|
||||
}
|
||||
}
|
||||
|
||||
fm.logger.Warn("无法加载中文字体文件")
|
||||
// 无法加载字体,使用系统默认字体,不记录警告
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -146,14 +146,17 @@ func (fm *FontManager) getWatermarkFontPaths() []string {
|
||||
func (fm *FontManager) buildFontPaths(fontNames []string) []string {
|
||||
var fontPaths []string
|
||||
|
||||
// 方式1: 优先使用相对路径(Linux风格,使用正斜杠)- 最常用
|
||||
for _, fontName := range fontNames {
|
||||
// 相对路径(相对于项目根目录)
|
||||
relativePaths := []string{
|
||||
"internal/shared/pdf/fonts/" + fontName,
|
||||
"./internal/shared/pdf/fonts/" + fontName,
|
||||
// 方式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))
|
||||
}
|
||||
}
|
||||
fontPaths = append(fontPaths, relativePaths...)
|
||||
}
|
||||
|
||||
// 方式2: 使用 pdf/fonts/ 目录(相对于当前文件)
|
||||
@@ -194,17 +197,13 @@ func (fm *FontManager) buildFontPaths(fontNames []string) []string {
|
||||
}
|
||||
}
|
||||
|
||||
// 方式6: 服务器绝对路径(作为最后的后备方案)
|
||||
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))
|
||||
}
|
||||
// 方式6: 相对路径(作为最后的后备方案)
|
||||
for _, fontName := range fontNames {
|
||||
relativePaths := []string{
|
||||
"internal/shared/pdf/fonts/" + fontName,
|
||||
"./internal/shared/pdf/fonts/" + fontName,
|
||||
}
|
||||
fontPaths = append(fontPaths, relativePaths...)
|
||||
}
|
||||
|
||||
// 过滤出实际存在的字体文件
|
||||
@@ -216,22 +215,7 @@ func (fm *FontManager) buildFontPaths(fontNames []string) []string {
|
||||
}
|
||||
}
|
||||
|
||||
// 只记录关键错误,并显示调试信息
|
||||
if len(existingFonts) == 0 {
|
||||
workDir, _ := os.Getwd()
|
||||
execPath, _ := os.Executable()
|
||||
fm.logger.Warn("未找到字体文件",
|
||||
zap.Strings("font_names", fontNames),
|
||||
zap.String("work_dir", workDir),
|
||||
zap.String("exec_path", execPath),
|
||||
zap.String("base_dir", fm.baseDir),
|
||||
zap.String("first_tried_path", func() string {
|
||||
if len(fontPaths) > 0 {
|
||||
return fontPaths[0]
|
||||
}
|
||||
return "none"
|
||||
}()))
|
||||
}
|
||||
// 字体文件不存在时不记录警告,使用系统默认字体即可
|
||||
|
||||
return existingFonts
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ func (pb *PageBuilder) AddFirstPage(pdf *gofpdf.Fpdf, product *entities.Product,
|
||||
pb.addWatermark(pdf, chineseFontAvailable)
|
||||
|
||||
// 封面页布局 - 居中显示
|
||||
pageWidth, pageHeight := pdf.GetPageSize()
|
||||
pageWidth, _ := pdf.GetPageSize()
|
||||
|
||||
// 标题区域(页面中上部)
|
||||
pdf.SetY(80)
|
||||
@@ -128,14 +128,21 @@ func (pb *PageBuilder) AddFirstPage(pdf *gofpdf.Fpdf, product *entities.Product,
|
||||
}
|
||||
}
|
||||
|
||||
// 底部信息(价格等)
|
||||
// 价格信息(右下角,在产品详情之后)
|
||||
if !product.Price.IsZero() {
|
||||
pdf.SetY(pageHeight - 60)
|
||||
// 获取产品详情结束后的Y坐标,稍微下移显示价格
|
||||
contentEndY := pdf.GetY()
|
||||
pdf.SetY(contentEndY + 5)
|
||||
pdf.SetTextColor(0, 0, 0)
|
||||
pb.fontManager.SetFont(pdf, "", 12)
|
||||
_, lineHt = pdf.GetFontSize()
|
||||
pdf.CellFormat(0, lineHt, fmt.Sprintf("价格:%s 元", product.Price.String()), "", 1, "C", false, 0, "")
|
||||
pb.fontManager.SetFont(pdf, "", 14)
|
||||
_, priceLineHt := pdf.GetFontSize()
|
||||
priceText := fmt.Sprintf("价格:%s 元", product.Price.String())
|
||||
textWidth := pdf.GetStringWidth(priceText)
|
||||
// 右对齐:从页面宽度减去文本宽度和右边距(15mm)
|
||||
pdf.SetX(pageWidth - textWidth - 15)
|
||||
pdf.CellFormat(textWidth, priceLineHt, priceText, "", 0, "R", false, 0, "")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// AddDocumentationPages 添加接口文档页面
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user