Extract words from PDF with golang?
Asked Answered
J

4

15

I don't understand type conversion. I know this isn't right, all I get is a bunch of hieroglyphs.

f, _ := os.Open("test.pdf") defer f.Close() io.Copy(os.Stdout, f)

I want to work with the strings....

Janijania answered 2/10, 2016 at 4:33 Comment(1)
checkout pdfcpu.Quintuplicate
I
11

I tried some go pdf libs, and found sajari/docconv works like I expect.

easy to use, here is a example:

package main

import (
    "fmt"
    "log"

    "code.sajari.com/docconv"
)

func main() {
    res, err := docconv.ConvertPath("your-file.pdf")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(res)
}
Inexplicit answered 18/9, 2017 at 7:39 Comment(2)
Note that the docconv package has dependencies that are only available for LinuxShockproof
If you are using Mac OS, please try to install dependencies via command brew install poppler and brew install tesseractPsychrometer
Q
9

It's because the PDF doesn't only contain the text, but it also contains the formats (fonts, padding, margin, position, shapes, image) information.

In case you need to read the plain text without format. I have forked a repository and implement the function to do that. You can check it at https://github.com/ledongthuc/pdf

I also have put an example, help it useful for you.

package main

import (
    "bytes"
    "fmt"

    "github.com/ledongthuc/pdf"
)

func main() {
    content, err := readPdf("test.pdf") // Read local pdf file
    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
}
Quipster answered 14/3, 2017 at 3:20 Comment(7)
I have a bug with your lib but it's not possible possible to post issue on ledongthuc/pdf Git.Pounce
@LeMoussel, not sure why can't you create the issue in my project. But anyway, you can ask send the bug here, I will try to help youQuipster
@ Le Dong Thuc : See How to extract plain text from PDF in golangPounce
@Pounce actually, you can: softwareengineering.stackexchange.com/questions/179468/…Nagey
@LeDongThuc Using your library, I'm getting the below error: malformed PDF: reading at offset 0: stream not presentRufinaruford
@ShaikSadiqAhmed were you able to solve your issue?Seleucid
@LeDongThuc I always get panic: malformed PDF: reading at offset 0: stream not present when I run ``` r, err := pdf.Open(path) r.Page(1).Content() ``` For example, this PDF: cs.utexas.edu/~roshan/CHET.pdf r.NumPage() and r.Outline() work tho.Unsteel
C
5

all I get is a bunch of hieroglyphs.

What you get is the content of a pdf file, which is not clear text.

If you want to read a pdf file in Go, use one of the golang pdf libraries like rsc.io/pdf, or one of those libraries like yob/pdfreader.

As mentioned here:

I doubt there is any 'solid framework' for this kind of stuff. PDF format isn't meant to be machine-friendly by design, and AFAIK there is no guaranteed way to parse arbitrary PDFs.

Cb answered 2/10, 2016 at 6:50 Comment(0)
Q
1

You can try to use pdf2go lib together with the popular: pdf2go

import (
    "fmt"
    "github.com/rudolfoborges/pdf2go"
)

func main() {
    pdf, err := pdf2go.New("path/to/file.pdf", pdf2go.Config{
        LogLevel: pdf2go.LogLevelError,
    })

    if err != nil {
        panic(err)
    }

    text, err := pdf.Text()
    if err != nil {
        panic(err)
    }

    fmt.Println(text)

    pages, err := pdf.Pages()

    if err != nil {
        panic(err)
    }

    for _, page := range pages {
        fmt.Println(page.Text())
    }
}
Quirinus answered 28/5, 2023 at 2:57 Comment(2)
Your answer could be improved by providing an example of the solution and how it helps the OP.Teamster
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewYodel

© 2022 - 2024 — McMap. All rights reserved.