This repository was archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 377
Adding GetPlainText methods, fixing charmap #21
Open
rikvanmechelen
wants to merge
20
commits into
rsc:master
Choose a base branch
from
rikvanmechelen:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+257
−46
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
56bd0c6
Update the purpose of the fork
ledongthuc 612c190
Change the import path
ledongthuc b95967f
Add function to get plain text from Page
ledongthuc daace13
Remove comment of rsc
ledongthuc 0e30ba2
Update
ledongthuc ffbf376
Correct the language of code block example
ledongthuc f8f8fe4
Update
ledongthuc d6cc515
Update README.md
ledongthuc f3eb144
Update README.md
ledongthuc 66da04e
Update README.md
ledongthuc 4ff10c6
Add space when get text from Content()
ledongthuc 1e8ebfa
Add readme content to get texts with style
ledongthuc fbd8755
Update
ledongthuc 11f580b
Add GetPlainText to Reader. Fix Encoder method
463f435
Merge pull request #1 from EndFirstCorp/master
ledongthuc 4e83a74
Update README.md
ivinpolosony 13fd063
Merge pull request #2 from ivinpolosony/master
ledongthuc 48f0c0b
page index starts at 1
rikvanmechelen 2deaee2
Recover from panics when getting plain text
50e8ea8
Merge pull request #1 from rikvanmechelen/panics
rikvanmechelen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,78 @@ | ||
| go get rsc.io/pdf | ||
| # PDF Reader | ||
|
|
||
| http://godoc.org/rsc.io/pdf | ||
| A simple Go library which enables reading PDF files. Forked from https://github.com/rsc/pdf | ||
|
|
||
| Features | ||
| - Get plain text content (without format) | ||
| - Get Content (including all font and formatting information) | ||
|
|
||
| ## Install: | ||
|
|
||
| `go get -u github.com/ledongthuc/pdf` | ||
|
|
||
|
|
||
| ## Read plain text | ||
|
|
||
| ```golang | ||
| 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 | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| buf.ReadFrom(r.GetPlainText()) | ||
| return buf.String(), nil | ||
| } | ||
| ``` | ||
|
|
||
| ## Read all text with styles from PDF | ||
|
|
||
| ```golang | ||
| func readPdf2(path string) (string, error) { | ||
| r, err := pdf.Open(path) | ||
| 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example lost a function,maybe like this: func isSameSentence(t1, t2 pdf.Text) bool {
if t1.Font == t2.Font && t1.FontSize == t2.FontSize {
return true
}
return false
}There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. func isSameSentence(t1, t2 pdf.Text) bool {
// if Y axis changes new line else same line
if t1.Y != t2.Y {
return false
}
return true
} |
||
| 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 | ||
|  | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be: