-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPDF 2 Text.go
148 lines (118 loc) · 3.43 KB
/
PDF 2 Text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
File Name: PDF 2 Text.go
Copyright: 2018 Kleissner Investments s.r.o.
Author: Peter Kleissner
This code uses the commercial library UniDoc https://unidoc.io/ to extract text from PDFs.
*/
package fileconversion
import (
"io"
"strconv"
"strings"
"time"
"github.com/unidoc/unipdf/core"
"github.com/unidoc/unipdf/extractor"
pdf "github.com/unidoc/unipdf/model"
"github.com/unidoc/unipdf/common/license"
)
// InitPDFLicense initializes the PDF license
func InitPDFLicense(key, name string) {
// load the unidoc license (v3)
license.SetLicenseKey(key, name)
}
// PDFListContentStreams writes all text streams in a PDF to the writer
// It returns the number of characters attempted written (excluding "Page N" and new-lines) and an error, if any. It can be used to determine whether any text was extracted.
// The parameter size is the max amount of bytes (not characters) to write out.
func PDFListContentStreams(f io.ReadSeeker, w io.Writer, size int64) (written int64, err error) {
pdfReader, err := pdf.NewPdfReader(f)
if err != nil {
return 0, err
}
isEncrypted, err := pdfReader.IsEncrypted()
if err != nil {
return 0, err
}
if isEncrypted {
_, err = pdfReader.Decrypt([]byte(""))
if err != nil {
return 0, err
}
}
numPages, err := pdfReader.GetNumPages()
if err != nil {
return 0, err
}
for i := 0; i < numPages && size > 0; i++ {
pageNum := i + 1
page, err := pdfReader.GetPage(pageNum)
if err != nil {
return written, err
}
ex, err := extractor.New(page)
if err != nil {
return written, err
}
txt, err := ex.ExtractText()
if err != nil {
return written, err
}
// use the extracted text
txtNL := ""
if written > 0 {
txtNL += "\n\n"
}
textB := []byte(txtNL + "---- Page " + strconv.Itoa(pageNum) + " ----\n")
// empty page? skip if so.
txt = strings.TrimSpace(txt)
if len(txt) == 0 {
continue
}
textB = append(textB, []byte(txt)...)
if int64(len(textB)) > size {
textB = textB[:size]
}
if _, err = w.Write(textB); err != nil {
return written, err
}
size -= int64(len(textB))
written += int64(len(txt))
}
return written, nil
}
// PDFGetCreationDate tries to get the creation date
func PDFGetCreationDate(f io.ReadSeeker) (date time.Time, valid bool) {
// Below code is forked from https://github.com/unidoc/unidoc-examples/blob/master/pdf/metadata/pdf_metadata_get_docinfo.go
pdfReader, err := pdf.NewPdfReader(f)
if err != nil {
return date, false
}
trailerDict, err := pdfReader.GetTrailer()
if err != nil || trailerDict == nil {
return date, false
}
var infoDict *core.PdfObjectDictionary
infoObj := trailerDict.Get("Info")
switch t := infoObj.(type) {
case *core.PdfObjectReference:
infoRef := t
infoObj, err = pdfReader.GetIndirectObjectByNumber(int(infoRef.ObjectNumber))
infoObj = core.TraceToDirectObject(infoObj)
if err != nil {
return date, false
}
infoDict, _ = infoObj.(*core.PdfObjectDictionary)
case *core.PdfObjectDictionary:
infoDict = t
}
if infoDict == nil {
return date, false
}
if str, has := infoDict.Get("CreationDate").(*core.PdfObjectString); has {
creationDateA := strings.TrimPrefix(str.String(), "D:")
time1, err := time.Parse("20060102150405-07'00'", creationDateA)
if err == nil {
return time1.UTC(), true
}
}
return date, false
}