This commit is contained in:
@@ -2,6 +2,7 @@ package pdf
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/jung-kurt/gofpdf/v2"
|
||||
@@ -12,157 +13,140 @@ import (
|
||||
type FontManager struct {
|
||||
logger *zap.Logger
|
||||
chineseFontName string
|
||||
chineseFontPath string
|
||||
chineseFontLoaded bool
|
||||
watermarkFontName string
|
||||
watermarkFontPath string
|
||||
watermarkFontLoaded bool
|
||||
baseDir string // 缓存基础目录,避免重复计算
|
||||
}
|
||||
|
||||
// NewFontManager 创建字体管理器
|
||||
func NewFontManager(logger *zap.Logger) *FontManager {
|
||||
// 获取当前文件所在目录
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
baseDir := filepath.Dir(filename)
|
||||
|
||||
return &FontManager{
|
||||
logger: logger,
|
||||
chineseFontName: "ChineseFont",
|
||||
watermarkFontName: "WatermarkFont",
|
||||
baseDir: baseDir,
|
||||
}
|
||||
}
|
||||
|
||||
// LoadChineseFont 加载中文字体到PDF(只使用黑体)
|
||||
// LoadChineseFont 加载中文字体到PDF
|
||||
func (fm *FontManager) LoadChineseFont(pdf *gofpdf.Fpdf) bool {
|
||||
if fm.chineseFontLoaded {
|
||||
return true
|
||||
}
|
||||
|
||||
fontPaths := fm.getHeiFontPaths() // 只获取黑体路径
|
||||
fontPaths := fm.getChineseFontPaths()
|
||||
if len(fontPaths) == 0 {
|
||||
fm.logger.Warn("未找到黑体字体路径,PDF中的中文可能显示为空白")
|
||||
fm.logger.Warn("未找到中文字体文件,PDF中的中文可能显示为空白")
|
||||
return false
|
||||
}
|
||||
|
||||
// 尝试添加黑体字体
|
||||
// 尝试加载字体
|
||||
for _, fontPath := range fontPaths {
|
||||
if fm.tryAddFont(pdf, fontPath) {
|
||||
fm.chineseFontPath = fontPath
|
||||
if fm.tryAddFont(pdf, fontPath, fm.chineseFontName) {
|
||||
fm.chineseFontLoaded = true
|
||||
fm.logger.Info("成功添加黑体字体(常规和粗体)", zap.String("font_path", fontPath))
|
||||
fm.logger.Info("成功加载中文字体", zap.String("font_path", fontPath))
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
fm.logger.Warn("未找到可用的黑体字体文件,PDF中的中文可能显示为空白")
|
||||
fm.logger.Warn("所有中文字体文件都无法加载,PDF中的中文可能显示为空白")
|
||||
return false
|
||||
}
|
||||
|
||||
// LoadWatermarkFont 加载水印字体到PDF(使用宋体或其他非黑体字体)
|
||||
// LoadWatermarkFont 加载水印字体到PDF
|
||||
func (fm *FontManager) LoadWatermarkFont(pdf *gofpdf.Fpdf) bool {
|
||||
if fm.watermarkFontLoaded {
|
||||
return true
|
||||
}
|
||||
|
||||
fontPaths := fm.getWatermarkFontPaths() // 获取水印字体路径(宋体等)
|
||||
fontPaths := fm.getWatermarkFontPaths()
|
||||
if len(fontPaths) == 0 {
|
||||
// 如果找不到水印字体,使用主字体(黑体)
|
||||
fm.logger.Warn("未找到水印字体,将使用主字体(黑体)")
|
||||
fm.logger.Warn("未找到水印字体文件,将使用主字体")
|
||||
return false
|
||||
}
|
||||
|
||||
// 尝试添加水印字体
|
||||
// 尝试加载字体
|
||||
for _, fontPath := range fontPaths {
|
||||
if fm.tryAddWatermarkFont(pdf, fontPath) {
|
||||
fm.watermarkFontPath = fontPath
|
||||
if fm.tryAddFont(pdf, fontPath, fm.watermarkFontName) {
|
||||
fm.watermarkFontLoaded = true
|
||||
fm.logger.Info("成功添加水印字体", zap.String("font_path", fontPath))
|
||||
fm.logger.Info("成功加载水印字体", zap.String("font_path", fontPath))
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
fm.logger.Warn("未找到可用的水印字体文件,将使用主字体(黑体)")
|
||||
fm.logger.Warn("所有水印字体文件都无法加载,将使用主字体")
|
||||
return false
|
||||
}
|
||||
|
||||
// tryAddFont 尝试添加字体(捕获panic)
|
||||
func (fm *FontManager) tryAddFont(pdf *gofpdf.Fpdf, fontPath string) bool {
|
||||
// tryAddFont 尝试添加字体(统一处理中文字体和水印字体)
|
||||
func (fm *FontManager) tryAddFont(pdf *gofpdf.Fpdf, fontPath, fontName string) bool {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fm.logger.Error("添加字体时发生panic", zap.Any("panic", r), zap.String("font_path", fontPath))
|
||||
fm.logger.Error("添加字体时发生panic",
|
||||
zap.Any("panic", r),
|
||||
zap.String("font_path", fontPath),
|
||||
zap.String("font_name", fontName))
|
||||
}
|
||||
}()
|
||||
|
||||
// 检查文件是否存在
|
||||
if _, err := os.Stat(fontPath); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// gofpdf v2使用AddUTF8Font添加支持UTF-8的字体
|
||||
pdf.AddUTF8Font(fm.chineseFontName, "", fontPath) // 常规样式
|
||||
pdf.AddUTF8Font(fm.chineseFontName, "B", fontPath) // 粗体样式
|
||||
pdf.AddUTF8Font(fontName, "", fontPath) // 常规样式
|
||||
pdf.AddUTF8Font(fontName, "B", fontPath) // 粗体样式
|
||||
|
||||
// 验证字体是否可用
|
||||
pdf.SetFont(fontName, "", 12)
|
||||
testWidth := pdf.GetStringWidth("测试")
|
||||
if testWidth == 0 {
|
||||
fm.logger.Debug("字体加载后无法使用",
|
||||
zap.String("font_path", fontPath),
|
||||
zap.String("font_name", fontName),
|
||||
zap.Float64("test_width", testWidth))
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// tryAddWatermarkFont 尝试添加水印字体(捕获panic)
|
||||
func (fm *FontManager) tryAddWatermarkFont(pdf *gofpdf.Fpdf, fontPath string) bool {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fm.logger.Error("添加水印字体时发生panic", zap.Any("panic", r), zap.String("font_path", fontPath))
|
||||
}
|
||||
}()
|
||||
|
||||
// gofpdf v2使用AddUTF8Font添加支持UTF-8的字体
|
||||
pdf.AddUTF8Font(fm.watermarkFontName, "", fontPath) // 常规样式
|
||||
pdf.AddUTF8Font(fm.watermarkFontName, "B", fontPath) // 粗体样式
|
||||
return true
|
||||
}
|
||||
|
||||
// getHeiFontPaths 获取黑体字体路径(支持跨平台,只返回黑体)
|
||||
func (fm *FontManager) getHeiFontPaths() []string {
|
||||
var fontPaths []string
|
||||
|
||||
// Windows系统
|
||||
if runtime.GOOS == "windows" {
|
||||
fontPaths = []string{
|
||||
`C:\Windows\Fonts\simhei.ttf`, // 黑体(优先)
|
||||
}
|
||||
} else if runtime.GOOS == "linux" {
|
||||
// Linux系统黑体字体路径
|
||||
fontPaths = []string{
|
||||
"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", // 文泉驿正黑(黑体)
|
||||
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc", // 文泉驿微米黑(黑体)
|
||||
}
|
||||
} else if runtime.GOOS == "darwin" {
|
||||
// macOS系统黑体字体路径
|
||||
fontPaths = []string{
|
||||
"/Library/Fonts/Microsoft/SimHei.ttf", // 黑体
|
||||
"/System/Library/Fonts/STHeiti Light.ttc", // 黑体
|
||||
}
|
||||
// getChineseFontPaths 获取中文字体路径列表(仅TTF格式)
|
||||
func (fm *FontManager) getChineseFontPaths() []string {
|
||||
// 按优先级排序的字体文件列表
|
||||
fontNames := []string{
|
||||
"simhei.ttf", // 黑体(默认)
|
||||
"simkai.ttf", // 楷体(备选)
|
||||
"simfang.ttf", // 仿宋(备选)
|
||||
}
|
||||
|
||||
// 过滤出实际存在的字体文件
|
||||
var existingFonts []string
|
||||
for _, fontPath := range fontPaths {
|
||||
if _, err := os.Stat(fontPath); err == nil {
|
||||
existingFonts = append(existingFonts, fontPath)
|
||||
}
|
||||
}
|
||||
|
||||
return existingFonts
|
||||
return fm.buildFontPaths(fontNames)
|
||||
}
|
||||
|
||||
// getWatermarkFontPaths 获取水印字体路径(支持跨平台,使用宋体或其他非黑体字体)
|
||||
// getWatermarkFontPaths 获取水印字体路径列表(仅TTF格式)
|
||||
func (fm *FontManager) getWatermarkFontPaths() []string {
|
||||
// 水印字体文件名(支持大小写变体)
|
||||
fontNames := []string{
|
||||
"yunfengfeiyunti-2.ttf",
|
||||
"YunFengFeiYunTi-2.ttf", // 大写版本(兼容)
|
||||
}
|
||||
|
||||
return fm.buildFontPaths(fontNames)
|
||||
}
|
||||
|
||||
// buildFontPaths 构建字体文件路径列表
|
||||
func (fm *FontManager) buildFontPaths(fontNames []string) []string {
|
||||
var fontPaths []string
|
||||
|
||||
// Windows系统
|
||||
if runtime.GOOS == "windows" {
|
||||
fontPaths = []string{
|
||||
`C:\Windows\Fonts\simsun.ttf`, // 宋体(优先用于水印)
|
||||
`C:\Windows\Fonts\simkai.ttf`, // 楷体(备选)
|
||||
}
|
||||
} else if runtime.GOOS == "linux" {
|
||||
// Linux系统宋体字体路径
|
||||
fontPaths = []string{
|
||||
"/usr/share/fonts/truetype/arphic/uming.ttc", // 文鼎PL UMing(宋体)
|
||||
}
|
||||
} else if runtime.GOOS == "darwin" {
|
||||
// macOS系统宋体字体路径
|
||||
fontPaths = []string{
|
||||
"/System/Library/Fonts/STSong.ttc", // 宋体
|
||||
}
|
||||
// 使用 pdf/fonts/ 目录
|
||||
for _, fontName := range fontNames {
|
||||
fontPaths = append(fontPaths, filepath.Join(fm.baseDir, "fonts", fontName))
|
||||
}
|
||||
|
||||
// 过滤出实际存在的字体文件
|
||||
@@ -176,22 +160,22 @@ func (fm *FontManager) getWatermarkFontPaths() []string {
|
||||
return existingFonts
|
||||
}
|
||||
|
||||
// SetFont 设置字体(使用黑体)
|
||||
// SetFont 设置中文字体
|
||||
func (fm *FontManager) SetFont(pdf *gofpdf.Fpdf, style string, size float64) {
|
||||
if fm.chineseFontLoaded {
|
||||
pdf.SetFont(fm.chineseFontName, style, size)
|
||||
} else {
|
||||
// 如果没有黑体字体,使用Arial作为后备
|
||||
// 如果没有中文字体,使用Arial作为后备
|
||||
pdf.SetFont("Arial", style, size)
|
||||
}
|
||||
}
|
||||
|
||||
// SetWatermarkFont 设置水印字体(使用宋体或其他非黑体字体)
|
||||
// SetWatermarkFont 设置水印字体
|
||||
func (fm *FontManager) SetWatermarkFont(pdf *gofpdf.Fpdf, style string, size float64) {
|
||||
if fm.watermarkFontLoaded {
|
||||
pdf.SetFont(fm.watermarkFontName, style, size)
|
||||
} else {
|
||||
// 如果水印字体不可用,使用主字体(黑体)作为后备
|
||||
// 如果水印字体不可用,使用主字体作为后备
|
||||
fm.SetFont(pdf, style, size)
|
||||
}
|
||||
}
|
||||
@@ -200,18 +184,3 @@ func (fm *FontManager) SetWatermarkFont(pdf *gofpdf.Fpdf, style string, size flo
|
||||
func (fm *FontManager) IsChineseFontAvailable() bool {
|
||||
return fm.chineseFontLoaded
|
||||
}
|
||||
|
||||
// GetChineseFontName 获取中文字体名称
|
||||
func (fm *FontManager) GetChineseFontName() string {
|
||||
return fm.chineseFontName
|
||||
}
|
||||
|
||||
// GetWatermarkFontName 获取水印字体名称
|
||||
func (fm *FontManager) GetWatermarkFontName() string {
|
||||
return fm.watermarkFontName
|
||||
}
|
||||
|
||||
// IsWatermarkFontAvailable 检查水印字体是否可用
|
||||
func (fm *FontManager) IsWatermarkFontAvailable() bool {
|
||||
return fm.watermarkFontLoaded
|
||||
}
|
||||
|
||||
BIN
internal/shared/pdf/fonts/YunFengFeiYunTi-2.ttf
Normal file
BIN
internal/shared/pdf/fonts/YunFengFeiYunTi-2.ttf
Normal file
Binary file not shown.
BIN
internal/shared/pdf/fonts/msyh.ttc
Normal file
BIN
internal/shared/pdf/fonts/msyh.ttc
Normal file
Binary file not shown.
BIN
internal/shared/pdf/fonts/simfang.ttf
Normal file
BIN
internal/shared/pdf/fonts/simfang.ttf
Normal file
Binary file not shown.
BIN
internal/shared/pdf/fonts/simhei.ttf
Normal file
BIN
internal/shared/pdf/fonts/simhei.ttf
Normal file
Binary file not shown.
BIN
internal/shared/pdf/fonts/simkai.ttf
Normal file
BIN
internal/shared/pdf/fonts/simkai.ttf
Normal file
Binary file not shown.
Reference in New Issue
Block a user