Skip to content

Latest commit

History

27 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

PDF Reader

A simple Go library which enables reading PDF files.

Features

  • Get plain text content (without format)
  • Get Content (including all font and formatting information)

Install:

go get -u github.com/rsc/pdf

Read plain text

package main
import (
"bytes""fmt""github.com/rsc/pdf"
)
funcmain() {
content, err:=readPdf("test.pdf") // Read local pdf fileiferr!=nil {
panic(err)
}
fmt.Println(content)
return
}
funcreadPdf(pathstring) (string, error) {
r, err:=pdf.Open(path)
iferr!=nil {
return"", err
}
varbuf bytes.Bufferbuf.ReadFrom(p.GetPlainText())
returnbuf.String(), nil
}

Read all text with styles from PDF

funcreadPdf2(pathstring) (string, error) {
r, err:=pdf.Open(path)
iferr!=nil {
return"", err
}
totalPage:=r.NumPage()
forpageIndex:=1; pageIndex<=totalPage; pageIndex++ {
p:=r.Page(pageIndex)
ifp.V.IsNull() {
continue
}
varlastTextStyle pdf.Texttexts:=p.Content().Textfor_, text:=rangetexts {
ifisSameSentence(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
}

Demo

Run example

About

PDF reader

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages