-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparameters.go
503 lines (421 loc) · 11.1 KB
/
parameters.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package pico
import (
"context"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
var jpegOptMap = map[string]interface{}{
"quality": nil,
"optimize": nil,
"progressive": nil,
}
var transparentFileType = map[string]bool{
"png": true,
"tiff": true,
}
type nameFn func(pdf string, index int32, first, last int32) string
type CallOption func(o *Parameters, command []string) []string
type Parameters struct {
popplerPath string
userPw string
ownerPw string
options []CallOption
timeout time.Duration
// These fields are used by Convert Function
dpi int
firstPage int32
lastPage int32
job int32
fmt string
jpegOpt map[string]string
outputFile string
outputFolder string
outputFileFn nameFn
outputFolderFn nameFn
singleFile bool
verbose bool
strict bool
transparent bool
grayscale bool
useCropBox bool
usePdftocario bool
hideAnnotations bool
scaleTo int
scaleToX int
scaleToY int
// these are what must be computed
baseCommand []string
binary string
pageCount int32
minPagesPerWorker int32
ctx context.Context
cancel context.CancelFunc
// this field is only used by GetPDFInfo() call
rawDates bool
}
// pageRangeForPart calculates the page range needed to be converted for a given file
// by specific worker during Convert() call.
func (p *Parameters) pageRangeForPart(pdf string, index int32) (int32, int32, error) {
reminder := p.pageCount % p.job
amortization := int32(0)
if index < reminder {
amortization = 1
}
first := p.firstPage + index*p.minPagesPerWorker
last := first + reminder + p.minPagesPerWorker - 1 + amortization
// FIXME: seems redundant
if last > p.lastPage {
panic("Wrong calculation, worker lastPage should not greater than task lastPage")
}
return first, last, nil
}
// pagePrangeForFile calculates the page range needed to be converted for a given file
// by specific worker during ConvertFiles() call.
func (p *Parameters) pageRangeForFile(pdf string, index int32) (int32, int32, error) {
first, last := p.firstPage, p.lastPage
pages, err := GetPagesCount(pdf, p.options...)
if err != nil {
return 0, 0, errors.Wrap(err, "failed to get pages count ")
}
totalPage := int32(pages)
if last < 0 || last > totalPage {
last = totalPage
}
if first > last {
return 0, 0, errors.WithStack(newWrongPageRangeError(first, last))
}
return first, last, nil
}
// buildCommand builds the full command line to convert (part of) a PDF file,
// outputFile and outputFolder are calculated during this call based on the
// index of the worker/convertor.
func (p *Parameters) buildCommand(pdf string, index, first, last int32) []string {
outputFile := p.outputFile
if outputFile == "" {
ext := path.Ext(pdf)
outputFile = pdf[:len(pdf)-len(ext)]
}
if p.outputFileFn != nil {
outputFile = p.outputFileFn(pdf, index, first, last)
}
outputFolder := p.outputFolder
if p.outputFolderFn != nil {
outputFolder = p.outputFolderFn(pdf, index, first, last)
}
outputFile = filepath.Join(outputFolder, outputFile)
os.MkdirAll(filepath.Dir(outputFile), 0755)
command := []string{
getCommandPath(p.binary, p.popplerPath),
"-progress",
"-f", strconv.Itoa(int(first)),
"-l", strconv.Itoa(int(last)),
}
command = append(command, p.baseCommand...)
command = append(command, pdf, outputFile)
return command
}
func (p *Parameters) apply(options ...CallOption) error {
command := []string{}
for _, option := range options {
command = option(p, command)
}
if p.usePdftocario && p.fmt == "ppm" {
p.fmt = "png"
}
parsedFormat, _, usePdfcairoFormat := parseFormat(p.fmt, p.grayscale)
switch parsedFormat {
case "jpeg":
command = append(command, "-jpeg")
case "png":
command = append(command, "-png")
case "tiff":
command = append(command, "-tiff")
}
usePdfCairo := p.usePdftocario || usePdfcairoFormat ||
(p.transparent && transparentFileType[parsedFormat])
if usePdfCairo {
p.binary = "pdftocairo"
}
// this considered as a Fatal if we cannot get the version of poppler utilities
version, err := getPopplerVersion(p.ctx, p.binary, p.popplerPath)
if err != nil {
return errors.WithStack(err)
}
major, minor := version[0], version[1]
if major == 0 {
if minor <= 57 {
p.jpegOpt = nil
}
if minor <= 83 {
p.hideAnnotations = false
}
}
if usePdfCairo && p.hideAnnotations {
return errors.WithStack(
newWrongArgumentError("hideAnnotations is not supported with pdftocairo"))
}
// size related options
if p.scaleTo > 0 {
command = append(command, "-scale-to", strconv.Itoa(p.scaleTo))
} else {
if p.scaleToX > 0 {
command = append(command, "-scale-to-x", strconv.Itoa(p.scaleToX))
}
if p.scaleToY > 0 {
command = append(command, "-scale-to-y", strconv.Itoa(p.scaleToY))
}
}
// ctx
if p.timeout > 0 {
p.ctx, p.cancel = context.WithTimeout(p.ctx, p.timeout)
}
p.options = options
p.baseCommand = command
return nil
}
// WithPopplerPath sets poppler binaries lookup path
func WithPopplerPath(popplerPath string) CallOption {
return func(p *Parameters, command []string) []string {
p.popplerPath = popplerPath
return command
}
}
// WithUserPw sets PDF's password
func WithUserPw(userPw string) CallOption {
return func(p *Parameters, command []string) []string {
p.userPw = userPw
return append(command, "-upw", userPw)
}
}
// WithOwnerPw sets PDF's owner password
func WithOwnerPw(ownerPw string) CallOption {
return func(p *Parameters, command []string) []string {
p.ownerPw = ownerPw
return append(command, "-opw", ownerPw)
}
}
// WithTimeout
func WithTimeout(timeout time.Duration) CallOption {
return func(p *Parameters, command []string) []string {
p.timeout = timeout
return command
}
}
// WithDpi sets image quality in DPI (default 200)
func WithDpi(dpi int) CallOption {
// this is the ClientOption function type
return func(p *Parameters, command []string) []string {
p.dpi = dpi
return command
}
}
// WithScaleTo sets the size of the resulting images, size=400 will fit the
// image to a 400x400 box, preserving aspect ratio
func WithScaleTo(size int) CallOption {
return func(p *Parameters, command []string) []string {
p.scaleTo = size
return command
}
}
// WithSize is the alias of WithScaleTo
var WithSize = WithScaleTo
func WithScaleToX(size int) CallOption {
return func(p *Parameters, command []string) []string {
p.scaleToX = size
return command
}
}
func WithScaleToY(size int) CallOption {
return func(p *Parameters, command []string) []string {
p.scaleToY = size
return command
}
}
// WithFirstPage sets the first page to convert
func WithFirstPage(firstPage int) CallOption {
return func(p *Parameters, command []string) []string {
if firstPage >= 0 {
p.firstPage = int32(firstPage)
}
return command
}
}
// WithLastPage sets the last page to convert
func WithLastPage(lastPage int) CallOption {
return func(p *Parameters, command []string) []string {
if lastPage >= 0 {
p.lastPage = int32(lastPage)
}
return command
}
}
// WithPageRange sets the range of pages to convert
func WithPageRange(firstPage, lastPage int) CallOption {
return func(p *Parameters, command []string) []string {
p.firstPage = int32(firstPage)
p.lastPage = int32(lastPage)
return command
}
}
// WithJob sets the number of threads to use
func WithJob(job int) CallOption {
if job < 1 {
job = 1
}
return func(p *Parameters, command []string) []string {
p.job = int32(job)
return command
}
}
// WithFormat sets the output image format
func WithFormat(fmt string) CallOption {
return func(p *Parameters, command []string) []string {
p.fmt = fmt
return command
}
}
func WithJPEGQuality(quality int) CallOption {
return func(p *Parameters, command []string) []string {
if quality < 0 || quality > 100 {
quality = 75
}
p.jpegOpt["quality"] = strconv.Itoa(quality)
return command
}
}
func WithJPEGOptimize(optimize bool) CallOption {
return func(p *Parameters, command []string) []string {
if optimize {
p.jpegOpt["optimize"] = "y"
} else {
p.jpegOpt["optimize"] = "n"
}
return command
}
}
func WithJPEGProgressive(progressive bool) CallOption {
return func(p *Parameters, command []string) []string {
if progressive {
p.jpegOpt["progressive"] = "y"
} else {
p.jpegOpt["progressive"] = "n"
}
return command
}
}
func WithJPEGOpt(jpegOpt map[string]string) CallOption {
for k := range jpegOpt {
if _, ok := jpegOptMap[k]; !ok {
log.Fatal("Invalid JPEG option: " + k)
}
}
return func(p *Parameters, command []string) []string {
p.jpegOpt = jpegOpt
parts := []string{}
for k, v := range jpegOpt {
parts = append(parts, fmt.Sprintf("%s=%s", k, v))
}
return append(command, "-jpegopt", strings.Join(parts, ","))
}
}
func WithOutputFile(outputFile string) CallOption {
return func(p *Parameters, command []string) []string {
p.outputFile = outputFile
return command
}
}
func WithOutputFileFn(fn nameFn) CallOption {
return func(p *Parameters, command []string) []string {
p.outputFileFn = fn
return command
}
}
// Write the resulting images to a folder (instead of directly in memory)
func WithOutputFolder(outputFolder string) CallOption {
return func(p *Parameters, command []string) []string {
p.outputFolder = outputFolder
return command
}
}
func WithSingleFile() CallOption {
return func(p *Parameters, command []string) []string {
p.singleFile = true
return append(command, "-singlefile")
}
}
// WithVerbose will prints useful debugging information
func WithVerbose() CallOption {
return func(p *Parameters, command []string) []string {
p.verbose = true
return command
}
}
// WithStrict sets to strict mode, when a Syntax Error is thrown, it will be raised as an Exception
func WithStrict() CallOption {
return func(p *Parameters, command []string) []string {
p.strict = true
return command
}
}
func WithTransparent() CallOption {
return func(p *Parameters, command []string) []string {
p.transparent = true
return command
}
}
func WithGrayScale() CallOption {
return func(p *Parameters, command []string) []string {
p.grayscale = true
return append(command, "-grayscale")
}
}
func WithUseCropBox() CallOption {
return func(p *Parameters, command []string) []string {
p.useCropBox = true
return command
}
}
func WithUsePdftocario() CallOption {
return func(p *Parameters, command []string) []string {
p.usePdftocario = true
return command
}
}
func WithHideAnnotations() CallOption {
return func(p *Parameters, command []string) []string {
p.hideAnnotations = true
return command
}
}
func WithContext(ctx context.Context) CallOption {
return func(p *Parameters, command []string) []string {
p.ctx = ctx
return command
}
}
func defaultConvertCallOption() *Parameters {
ctx, cancel := context.WithCancel(context.Background())
return &Parameters{
dpi: 200,
fmt: "ppm",
firstPage: 1,
lastPage: -1,
job: 1,
timeout: -1,
ctx: ctx,
cancel: cancel,
binary: "pdftoppm",
}
}
func defaultConvertFilesCallOption() *Parameters {
p := defaultConvertCallOption()
p.job = 4
return p
}