Skip to content

Commit 28c437d

Browse files
committed
feat: 优化内容处理
1 parent cefefae commit 28c437d

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

pdf.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package docreader
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/ledongthuc/pdf"
78
)
@@ -21,7 +22,7 @@ func (r *PdfReader) ReadText(filePath string) (string, error) {
2122
// 获取总页数
2223
totalPages := reader.NumPage()
2324

24-
var content string
25+
var content strings.Builder
2526

2627
// 逐页读取文本
2728
for pageNum := 1; pageNum <= totalPages; pageNum++ {
@@ -36,11 +37,11 @@ func (r *PdfReader) ReadText(filePath string) (string, error) {
3637
continue
3738
}
3839

39-
content += text
40-
content += "\n\n--- 第 " + fmt.Sprintf("%d", pageNum) + " 页 ---\n\n"
40+
content.WriteString(text)
41+
content.WriteString("\n\n--- 第 " + fmt.Sprintf("%d", pageNum) + " 页 ---\n\n")
4142
}
4243

43-
return content, nil
44+
return content.String(), nil
4445
}
4546

4647
// GetMetadata 获取 PDF 文件的元数据

0 commit comments

Comments
 (0)