This commit is contained in:
2025-09-21 21:44:44 +08:00
parent 8dc4cb19fc
commit 59d26ac08e

View File

@@ -107,34 +107,33 @@ func (s *AuthorizationService) generatePDFContent(userInfo map[string]interface{
pdf := gofpdf.New("P", "mm", "A4", "") pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage() pdf.AddPage()
// 添加中文字体支持 - 使用项目字体文件 // 添加中文字体支持 - 参考imageService的路径处理方式
fontPaths := []string{ fontPaths := []string{
"static/SIMHEI.TTF", // 相对于工作目录的路径与imageService一致
"/app/static/SIMHEI.TTF", // Docker容器内的字体文件 "/app/static/SIMHEI.TTF", // Docker容器内的字体文件
"../../static/SIMHEI.TTF", // 开发环境字体文件(相对于当前工作目录) "app/main/api/static/SIMHEI.TTF", // 开发环境备用路径
"app/main/api/static/SIMHEI.TTF", // 开发环境字体文件(绝对路径)
} }
wd, wdErr := os.Getwd()
if wdErr != nil {
logx.Errorf("获取工作目录失败: %v", wdErr)
}
logx.Infof("当前工作目录: %s", wd)
// 尝试添加字体 // 尝试添加字体
fontAdded := false fontAdded := false
for _, fontPath := range fontPaths { for _, fontPath := range fontPaths {
if _, err := os.Stat(fontPath); err == nil { if _, err := os.Stat(fontPath); err == nil {
pdf.AddUTF8Font("ChineseFont", "", fontPath) pdf.AddUTF8Font("ChineseFont", "", fontPath)
pdf.SetFont("ChineseFont", "", 12)
fontAdded = true fontAdded = true
logx.Infof("成功加载字体: %s", fontPath) logx.Infof("成功加载字体: %s", fontPath)
break break
} else { } else {
logx.Debugf("字体文件不存在: %s", fontPath) logx.Debugf("字体文件不存在: %s, 错误: %v", fontPath, err)
} }
} }
// 如果没有找到字体文件,使用默认字体 // 如果没有找到字体文件,使用默认字体,并记录警告
if !fontAdded { if !fontAdded {
pdf.SetFont("Arial", "", 12) pdf.SetFont("Arial", "", 12)
logx.Errorf("未找到中文字体文件使用默认Arial字体可能无法正确显示中文")
} else {
// 设置默认字体
pdf.SetFont("ChineseFont", "", 12)
} }
// 获取用户信息 // 获取用户信息
@@ -145,14 +144,22 @@ func (s *AuthorizationService) generatePDFContent(userInfo map[string]interface{
currentDate := time.Now().Format("2006年1月2日") currentDate := time.Now().Format("2006年1月2日")
// 设置标题样式 - 大字体、居中 // 设置标题样式 - 大字体、居中
pdf.SetFont("ChineseFont", "", 20) // 使用20号字体 if fontAdded {
pdf.SetFont("ChineseFont", "", 20) // 使用20号字体
} else {
pdf.SetFont("Arial", "", 20)
}
pdf.CellFormat(0, 15, "授权书", "", 1, "C", false, 0, "") pdf.CellFormat(0, 15, "授权书", "", 1, "C", false, 0, "")
// 添加空行 // 添加空行
pdf.Ln(5) pdf.Ln(5)
// 设置正文样式 - 正常字体 // 设置正文样式 - 正常字体
pdf.SetFont("ChineseFont", "", 12) if fontAdded {
pdf.SetFont("ChineseFont", "", 12)
} else {
pdf.SetFont("Arial", "", 12)
}
// 构建授权书内容(去掉标题部分) // 构建授权书内容(去掉标题部分)
content := fmt.Sprintf(`海南天远大数据科技有限公司: content := fmt.Sprintf(`海南天远大数据科技有限公司: