-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunidoc_glue.go
261 lines (235 loc) · 7.06 KB
/
unidoc_glue.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2019 PaperCut Software International Pty Ltd. All rights reserved.
package doclib
import (
"errors"
"flag"
"fmt"
"io"
"os"
"github.com/unidoc/unipdf/v3/common"
"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/extractor"
"github.com/unidoc/unipdf/v3/model"
)
var (
// Debug can be set true to enable debug level logging.
Debug bool
// Trace can be set true to enable debug level logging.
Trace bool
// ExposeErrors can be set to true to not recover from errors in library functions.
ExposeErrors bool
)
const (
// Make sure to enter a valid license key.
// Otherwise text is truncated and a watermark added to the text.
// License keys are available via: https://unidoc.io
uniDocLicenseKey = `
-----BEGIN UNIDOC LICENSE KEY-----
....
-----END UNIDOC LICENSE KEY-----
`
companyName = "(Your company)"
creatorName = "PDF Search"
)
// init sets up UniDoc licensing and logging.
func init() {
err := license.SetLicenseKey(uniDocLicenseKey, companyName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading UniDoc license: %v\n", err)
}
model.SetPdfCreator(creatorName)
flag.BoolVar(&Debug, "d", false, "Print debugging information.")
flag.BoolVar(&Trace, "e", false, "Print detailed debugging information.")
flag.BoolVar(&ExposeErrors, "x", ExposeErrors, "Don't recover from library panics.")
if Trace {
Debug = true
}
}
func InitLogging() {
fmt.Printf("Debug=%t\n", Debug)
// panic(Debug)
if Trace {
common.SetLogger(common.NewConsoleLogger(common.LogLevelTrace))
} else if Debug {
common.SetLogger(common.NewConsoleLogger(common.LogLevelDebug))
} else {
common.SetLogger(common.NewConsoleLogger(common.LogLevelInfo))
}
}
// PdfOpen opens PDF `inPath` and attempts to handle null encryption schemes.
func PdfOpen(inPath string) (*model.PdfReader, error) {
f, err := os.Open(inPath)
if err != nil {
return nil, err
}
defer f.Close()
return PdfOpenReader(f, false)
}
// PdfOpen opens PDF `inPath` lazily and attempts to handle null encryption schemes.
// Caller must close the returned file handle if there are no errors.
func PdfOpenFileLazy(inPath string) (*os.File, *model.PdfReader, error) {
f, err := os.Open(inPath)
if err != nil {
return nil, nil, err
}
pdfReader, err := PdfOpenReader(f, true)
if err != nil {
f.Close()
return nil, nil, err
}
return f, pdfReader, nil
}
// PdfOpenReader opens the PDF accessed by `rs` and attempts to handle null encryption schemes.
// If `lazy` is true, a lazy PDF reader is opened.
func PdfOpenReader(rs io.ReadSeeker, lazy bool) (*model.PdfReader, error) {
var pdfReader *model.PdfReader
var err error
if lazy {
pdfReader, err = model.NewPdfReaderLazy(rs)
} else {
pdfReader, err = model.NewPdfReader(rs)
}
if err != nil {
return nil, err
}
isEncrypted, err := pdfReader.IsEncrypted()
if err != nil {
return nil, err
}
if isEncrypted {
_, err = pdfReader.Decrypt([]byte(""))
if err != nil {
return nil, err
}
}
return pdfReader, nil
}
// PageSizePt returns the width and height of `page` in points.
func PageSizePt(page *model.PdfPage) (width, height float64, err error) {
b, err := page.GetMediaBox()
if err != nil {
return 0, 0, err
}
return b.Urx - b.Llx, b.Ury - b.Lly, nil
}
// ExtractPageTextMarks returns the extracted text and corresponding TextMarks on page `page`.
func ExtractPageTextMarks(page *model.PdfPage) (string, *extractor.TextMarkArray, error) {
ex, err := extractor.New(page)
if err != nil {
return "", nil, err
}
pageText, _, _, err := ex.ExtractPageText()
if err != nil {
return "", nil, err
}
return pageText.Text(), pageText.Marks(), nil
}
// PDFPageProcessor is used for processing a PDF one page at a time.
// It is an opaque struct.
type PDFPageProcessor struct {
inPath string
pdfFile *os.File
pdfReader *model.PdfReader
}
// CreatePDFPageProcessorFile creates a PDFPageProcessor for reading the PDF `inPath`.
func CreatePDFPageProcessorFile(inPath string) (*PDFPageProcessor, error) {
f, err := os.Open(inPath)
if err != nil {
common.Log.Error("CreatePDFPageProcessorFile: Could not open inPath=%q. err=%v", inPath, err)
return nil, err
}
processor, err := CreatePDFPageProcessorReader(inPath, f)
if err != nil {
f.Close()
return nil, err
}
processor.pdfFile = f
return processor, err
}
// CreatePDFPageProcessorReader creates a PDFPageProcessor for reading the PDF referenced by
// `rs`.
// `inPath` is provided for logging only. It is expected to be the path referenced by `rs`.
func CreatePDFPageProcessorReader(inPath string, rs io.ReadSeeker) (*PDFPageProcessor, error) {
processor := PDFPageProcessor{inPath: inPath}
var err error
processor.pdfReader, err = PdfOpenReader(rs, true)
if err != nil {
common.Log.Debug("CreatePDFPageProcessor: PdfOpenReader failed. inPath=%q. err=%v",
inPath, err)
return nil, err
}
return &processor, nil
}
// Close closes file handles opened by CreatePDFPageProcessorFile.
func (p *PDFPageProcessor) Close() error {
if p.pdfFile == nil {
return nil
}
err := p.pdfFile.Close()
p.pdfFile = nil
return err
}
// NumPages return the number of pages in the PDF referenced by `p`.
func (p PDFPageProcessor) NumPages() (uint32, error) {
numPages, err := p.pdfReader.GetNumPages()
return uint32(numPages), err
}
// Process runs `processPage` on every page in PDF `p.inPath`.
// It can recover from errors in the libraries it calls if `ExposeErrors` is false.
func (p *PDFPageProcessor) Process(processPage func(pageNum uint32, page *model.PdfPage) error) (
err error) {
if !ExposeErrors {
defer func() {
if r := recover(); r != nil {
common.Log.Error("Recovering from a panic!!!: %q r=%#v", p.inPath, r)
switch t := r.(type) {
case error:
err = t
case string:
err = errors.New(t)
}
}
}()
}
err = processPDFPages(p.inPath, p.pdfReader, processPage)
return err
}
// ProcessPDFPagesFile runs `processPage` on every page in PDF`inPath`.
// It is a convenience function.
func ProcessPDFPagesFile(inPath string, processPage func(pageNum uint32, page *model.PdfPage) error) error {
p, err := CreatePDFPageProcessorFile(inPath)
if err != nil {
return err
}
defer p.Close()
return p.Process(processPage)
}
// ProcessPDFPagesReader runs `processPage` on every page in PDF opened in `rs`.
// It is a convenience function.
func ProcessPDFPagesReader(inPath string, rs io.ReadSeeker,
processPage func(pageNum uint32, page *model.PdfPage) error) error {
p, err := CreatePDFPageProcessorReader(inPath, rs)
if err != nil {
return err
}
return p.Process(processPage)
}
// processPDFPages runs `processPage` on every page in PDF `inPath`.
func processPDFPages(inPath string, pdfReader *model.PdfReader,
processPage func(pageNum uint32, page *model.PdfPage) error) error {
numPages, err := pdfReader.GetNumPages()
if err != nil {
return err
}
common.Log.Debug("processPDFPages: inPath=%q numPages=%d", inPath, numPages)
for pageNum := uint32(1); pageNum <= uint32(numPages); pageNum++ {
page, err := pdfReader.GetPage(int(pageNum))
if err != nil {
return err
}
if err = processPage(pageNum, page); err != nil {
return err
}
}
return nil
}