Merge branch 'feat-cjy-report' into dev
This commit is contained in:
commit
ffaf09f4e2
@ -6,10 +6,25 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/phpdave11/gofpdf"
|
"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 加载中文字体
|
// loadChineseFont 加载中文字体
|
||||||
func loadChineseFont(pdf *gofpdf.Fpdf, fontPath string) error {
|
func loadChineseFont(pdf *gofpdf.Fpdf, fontPath string) error {
|
||||||
var fontData []byte
|
var fontData []byte
|
||||||
@ -52,13 +67,16 @@ func GeneratePDF(text, imageURL, outputPath, fontPath string) error {
|
|||||||
// 设置当前位置(x, y),从左上角开始
|
// 设置当前位置(x, y),从左上角开始
|
||||||
pdf.SetXY(20, 10)
|
pdf.SetXY(20, 10)
|
||||||
|
|
||||||
|
// 清理文本,移除PDF不支持的字符(如emoji)
|
||||||
|
cleanedText := cleanTextForPDF(text)
|
||||||
|
|
||||||
// 添加文本内容
|
// 添加文本内容
|
||||||
// 使用MultiCell方法处理多行文本,支持自动换行
|
// 使用MultiCell方法处理多行文本,支持自动换行
|
||||||
// 参数:宽度、行高、文本内容、边框、对齐方式、是否填充
|
// 参数:宽度、行高、文本内容、边框、对齐方式、是否填充
|
||||||
// A4页面宽度210mm,减去左右边距40mm,可用宽度170mm
|
// A4页面宽度210mm,减去左右边距40mm,可用宽度170mm
|
||||||
textWidth := 170.0
|
textWidth := 170.0
|
||||||
lineHeight := 7.0
|
lineHeight := 7.0
|
||||||
pdf.MultiCell(textWidth, lineHeight, text, "", "L", false)
|
pdf.MultiCell(textWidth, lineHeight, cleanedText, "", "L", false)
|
||||||
|
|
||||||
// 添加一些间距
|
// 添加一些间距
|
||||||
pdf.Ln(5)
|
pdf.Ln(5)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user