知识分享之Golang——读取pdf中纯文本内容

简介: 知识分享之Golang篇是我在日常使用Golang时学习到的各种各样的知识的记录,将其整理出来以文章的形式分享给大家,来进行共同学习。欢迎大家进行持续关注。知识分享系列目前包含Java、Golang、Linux、Docker等等。

知识分享之Golang——读取pdf中纯文本内容

背景

知识分享之Golang篇是我在日常使用Golang时学习到的各种各样的知识的记录,将其整理出来以文章的形式分享给大家,来进行共同学习。欢迎大家进行持续关注。

知识分享系列目前包含Java、Golang、Linux、Docker等等。

开发环境

内容

日常我们有时需要进行对pdf文件进行读取其中的内容,我对比了各类现有的开源组件库,发现ledongthuc/pdf组件比较好用,现分享给大家。

1、安装

go get -u github.com/ledongthuc/pdf
go mod vendor

2、工具类

import (
    "bytes"
    "github.com/ledongthuc/pdf"
)

// ReadPdf 获取pdf文字内容
func ReadPdf(path string) (string, error) {
    f, r, err := pdf.Open(path)
    // remember close file
    defer f.Close()
    if err != nil {
        return "", err
    }
    var buf bytes.Buffer
    b, err := r.GetPlainText()
    if err != nil {
        return "", err
    }
    buf.ReadFrom(b)
    return buf.String(), nil
}

阅读按行分组的文本
func ReadPdfGroup(path string) (string, error) {
    f, r, err := pdf.Open(path)
    defer func() {
        _ = f.Close()
    }()
    if err != nil {
        return "", err
    }
    totalPage := r.NumPage()

    for pageIndex := 1; pageIndex <= totalPage; pageIndex++ {
        p := r.Page(pageIndex)
        if p.V.IsNull() {
            continue
        }

        rows, _ := p.GetTextByRow()
        for _, row := range rows {
            println(">>>> row: ", row.Position)
            for _, word := range row.Content {
                fmt.Println(word.S)
            }
        }
    }
    return "", nil
}
// PDF格式的所有文本
func readPdfFormatAll(path string) (string, error) {
    f, r, err := pdf.Open(path)
    // remember close file
    defer f.Close()
    if err != nil {
        return "", err
    }
    totalPage := r.NumPage()

    for pageIndex := 1; pageIndex <= totalPage; pageIndex++ {
        p := r.Page(pageIndex)
        if p.V.IsNull() {
            continue
        }
        var lastTextStyle pdf.Text
        texts := p.Content().Text
        for _, text := range texts {
            if isSameSentence(text, lastTextStyle) {
                lastTextStyle.S = lastTextStyle.S + text.S
            } else {
                fmt.Printf("Font: %s, Font-size: %f, x: %f, y: %f, content: %s \n", lastTextStyle.Font, lastTextStyle.FontSize, lastTextStyle.X, lastTextStyle.Y, lastTextStyle.S)
                lastTextStyle = text
            }
        }
    }
    return "", nil
}
本文声明:

5330898-d1c72b6c90e378f3.png
知识共享许可协议
本作品由 cn華少 采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可。

目录
相关文章
|
4月前
|
自然语言处理 文字识别 数据可视化
从PDF和图像中提取文本,以供大型语言模型使用
从PDF和图像中提取文本,以供大型语言模型使用
|
11月前
|
存储 Linux 测试技术
Python操作PDF-文本和图片提取(使用PyPDF2和PyMuPDF)
Python操作PDF-文本和图片提取(使用PyPDF2和PyMuPDF)
700 0
UIWebView 读取pdf,word,excel
UIWebView 读取pdf,word,excel
80 0
|
Python
|
编解码 安全 Unix
数据导入与预处理-第4章-数据获取python读取pdf文档
数据导入与预处理-第4章-数据获取Python读取PDF文档 1 PDF简介 1.1 pdf是什么 2 Python操作PDF 2.1 pdfplumber库
数据导入与预处理-第4章-数据获取python读取pdf文档
|
JSON Java BI
Java 解析pdf文档内容实战案例
Java 解析pdf文档内容实战案例
Java 解析pdf文档内容实战案例
|
IDE 开发工具 Python
python 读出pdf文件中的内容
python 读出pdf文件中的内容
76 0
|
自然语言处理 Python
Python 操作pdf文件(pdfplumber读取PDF写入Excel)
学习了解Python 操作pdf文件(pdfplumber读取PDF写入Excel)。
721 0
Python 操作pdf文件(pdfplumber读取PDF写入Excel)
|
JSON 自然语言处理 数据可视化
如何用Elasticsearch实现Word、PDF,TXT文件的全文内容检索?
如何用Elasticsearch实现Word、PDF,TXT文件的全文内容检索?
如何用Elasticsearch实现Word、PDF,TXT文件的全文内容检索?
|
存储 Linux Python
Python编程:读取pdf、pptx、docx、xlsx文件的页数
Python编程:读取pdf、pptx、docx、xlsx文件的页数
683 0