Merge branch 'feat-cjy-report' into dev

This commit is contained in:
cjy 2026-01-21 15:37:24 +08:00
commit ffaf09f4e2

View File

@ -6,10 +6,25 @@ import (
"io"
"net/http"
"os"
"unicode"
"github.com/phpdave11/gofpdf"
)
// cleanTextForPDF 清理文本移除PDF不支持的字符如emoji
// gofpdf库不支持某些特殊字符
func cleanTextForPDF(text string) string {
var result []rune
for _, r := range text {
// 保留基本多文种平面BMP内的字符码点 <= 0xFFFF
// 这样可以保留中文、英文、数字等常用字符但过滤掉emoji等特殊字符
if r <= 0xFFFF && (unicode.IsPrint(r) || unicode.IsSpace(r)) {
result = append(result, r)
}
}
return string(result)
}
// loadChineseFont 加载中文字体
func loadChineseFont(pdf *gofpdf.Fpdf, fontPath string) error {
var fontData []byte
@ -52,13 +67,16 @@ func GeneratePDF(text, imageURL, outputPath, fontPath string) error {
// 设置当前位置x, y从左上角开始
pdf.SetXY(20, 10)
// 清理文本移除PDF不支持的字符如emoji
cleanedText := cleanTextForPDF(text)
// 添加文本内容
// 使用MultiCell方法处理多行文本支持自动换行
// 参数:宽度、行高、文本内容、边框、对齐方式、是否填充
// A4页面宽度210mm减去左右边距40mm可用宽度170mm
textWidth := 170.0
lineHeight := 7.0
pdf.MultiCell(textWidth, lineHeight, text, "", "L", false)
pdf.MultiCell(textWidth, lineHeight, cleanedText, "", "L", false)
// 添加一些间距
pdf.Ln(5)