gpt4 book ai didi

pdf - 如何在golang中从PDF中提取纯文本

转载 作者:IT王子 更新时间:2023-10-29 01:44:43 25 4
gpt4 key购买 nike

我想使用 GO 从 pdf 文件中提取文本。我尝试使用 ledongthuc/pdf Go 包实现方法 GetPlainText() 以获取没有格式的纯文本内容。但我没有得到纯文本。结果是:

 W
S
D
V
Y R
O
R
Q
W
D
L
U
H
P
H
Q
W
......

Go 代码

package main

import (
"bytes"
"fmt"

"github.com/ledongthuc/pdf"
)

func main() {
content, err := readPdf("test.pdf")
if err != nil {
panic(err)
}
fmt.Println(content)
return
}

func readPdf(path string) (string, error) {
r, err := pdf.Open(path)
if err != nil {
return "", err
}
totalPage := r.NumPage()

var textBuilder bytes.Buffer
for pageIndex := 1; pageIndex <= totalPage; pageIndex++ {
p := r.Page(pageIndex)
if p.V.IsNull() {
continue
}
textBuilder.WriteString(p.GetPlainText("\n"))
}
return textBuilder.String(), nil
}

最佳答案

您可以收到诸如“pdf 文档示例”之类的消息。而不是

Ex
a
m
pl
e

of

a

pd
f

doc
u
m
e
nt
.

您需要做的是更改 textBuilder.WriteString(p.GetPlainText("\n"))

textBuilder.WriteString(p.GetPlainText(""))

希望对您有所帮助。

关于pdf - 如何在golang中从PDF中提取纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44560265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com