forked from pdfcpu/pdfcpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
771 lines (588 loc) · 20.1 KB
/
api.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
package pdflib
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/hhrutter/pdflib/extract"
"github.com/hhrutter/pdflib/merge"
"github.com/hhrutter/pdflib/optimize"
"github.com/hhrutter/pdflib/read"
"github.com/hhrutter/pdflib/types"
"github.com/hhrutter/pdflib/validate"
"github.com/hhrutter/pdflib/write"
"github.com/pkg/errors"
)
var (
logInfoAPI *log.Logger
logStatsAPI *log.Logger
logErrorAPI *log.Logger
selectedPagesRegExp *regexp.Regexp
)
func setupRegExpForPageSelection() *regexp.Regexp {
e := "[!n]?((-\\d+)|(\\d+(-(\\d+)?)?))"
exp := "^" + e + "(," + e + ")*$"
re, _ := regexp.Compile(exp)
return re
}
func init() {
logInfoAPI = log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
logStatsAPI = log.New(ioutil.Discard, "STATS: ", log.Ldate|log.Ltime|log.Lshortfile)
logErrorAPI = log.New(os.Stdout, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
selectedPagesRegExp = setupRegExpForPageSelection()
}
// Verbose controls logging output.
func Verbose(verbose bool) {
if verbose {
logStatsAPI = log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile)
} else {
logStatsAPI = log.New(ioutil.Discard, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile)
}
}
// Read reads in a PDF file and builds an internal structure holding its cross reference table aka the PDFContext.
func Read(fileIn string, config *types.Configuration) (ctx *types.PDFContext, err error) {
//logInfoAPI.Printf("reading %s..\n", fileIn)
ctx, err = read.PDFFile(fileIn, config)
if err != nil {
err = errors.Wrap(err, "Read failed.")
}
return
}
// Validate validates a PDF file against ISO-32000-1:2008.
func Validate(fileIn string, config *types.Configuration) (err error) {
from1 := time.Now()
fmt.Printf("validating(mode=%s) %s ...\n", config.ValidationModeString(), fileIn)
//logInfoAPI.Printf("validating(mode=%s) %s..\n", config.ValidationModeString(), fileIn)
ctx, err := Read(fileIn, config)
if err != nil {
return
}
dur1 := time.Since(from1).Seconds()
from2 := time.Now()
err = validate.XRefTable(ctx.XRefTable)
if err != nil {
err = errors.Wrap(err, "validation error (try -mode=relaxed)")
} else {
fmt.Println("validation ok")
//logInfoAPI.Println("validation ok")
}
dur2 := time.Since(from2).Seconds()
dur := time.Since(from1).Seconds()
logStatsAPI.Printf("XRefTable:\n%s\n", ctx)
logStatsAPI.Println("Timing:")
logStatsAPI.Printf("read : %6.3fs %4.1f%%\n", dur1, dur1/dur*100)
logStatsAPI.Printf("validate : %6.3fs %4.1f%%\n", dur2, dur2/dur*100)
logStatsAPI.Printf("total processing time: %6.3fs\n\n", dur)
// at this stage: no binary breakup available!
ctx.Read.LogStats(logStatsAPI, ctx.Optimized)
return
}
// Write generates a PDF file for a given PDFContext.
func Write(ctx *types.PDFContext) (err error) {
fmt.Printf("writing %s ...\n", ctx.Write.DirName+ctx.Write.FileName)
//logInfoAPI.Printf("writing to %s..\n", fileName)
err = write.PDFFile(ctx)
if err != nil {
err = errors.Wrap(err, "Write failed.")
return
}
if ctx.StatsFileName != "" {
err = write.AppendStatsFile(ctx)
if err != nil {
err = errors.Wrap(err, "Write stats failed.")
}
}
return
}
// singlePageFileName for PDFContext and a specific page number.
func singlePageFileName(ctx *types.PDFContext, pageNr int) string {
baseFileName := filepath.Base(ctx.Read.FileName)
fileName := strings.TrimSuffix(baseFileName, ".pdf")
return fileName + "_" + strconv.Itoa(pageNr) + ".pdf"
}
func writeSinglePagePDF(ctx *types.PDFContext, pageNr int, dirOut string) (err error) {
ctx.ResetWriteContext()
w := ctx.Write
w.Command = "Split"
w.ExtractPageNr = pageNr
w.DirName = dirOut + "/"
w.FileName = singlePageFileName(ctx, pageNr)
fmt.Printf("writing %s ...\n", w.DirName+w.FileName)
return write.PDFFile(ctx)
}
func writeSinglePagePDFs(ctx *types.PDFContext, selectedPages types.IntSet, dirOut string) (err error) {
if selectedPages == nil {
selectedPages = types.IntSet{}
}
if len(selectedPages) == 0 {
for i := 1; i <= ctx.PageCount; i++ {
selectedPages[i] = true
}
}
for i, v := range selectedPages {
if v {
err = writeSinglePagePDF(ctx, i, dirOut)
if err != nil {
return
}
}
}
return
}
func readAndValidate(fileIn string, config *types.Configuration, from1 time.Time) (ctx *types.PDFContext, dur1, dur2 float64, err error) {
ctx, err = Read(fileIn, config)
if err != nil {
return
}
dur1 = time.Since(from1).Seconds()
from2 := time.Now()
//fmt.Printf("validating %s ...\n", fileIn)
//logInfoAPI.Printf("validating %s..\n", fileIn)
err = validate.XRefTable(ctx.XRefTable)
if err != nil {
return
}
dur2 = time.Since(from2).Seconds()
return
}
func readValidateAndOptimize(fileIn string, config *types.Configuration, from1 time.Time) (ctx *types.PDFContext, dur1, dur2, dur3 float64, err error) {
ctx, dur1, dur2, err = readAndValidate(fileIn, config, from1)
if err != nil {
return
}
from3 := time.Now()
//fmt.Printf("optimizing %s ...\n", fileIn)
//logInfoAPI.Printf("optimizing %s..\n", fileIn)
err = optimize.XRefTable(ctx)
if err != nil {
return
}
dur3 = time.Since(from3).Seconds()
return
}
// Optimize reads in fileIn, does validation, optimization and writes the result to fileOut.
func Optimize(fileIn, fileOut string, config *types.Configuration) (err error) {
fromStart := time.Now()
ctx, durRead, durVal, durOpt, err := readValidateAndOptimize(fileIn, config, fromStart)
if err != nil {
return
}
logStatsAPI.Printf("XRefTable:\n%s\n", ctx)
fromWrite := time.Now()
dirName, fileName := filepath.Split(fileOut)
ctx.Write.DirName = dirName
ctx.Write.FileName = fileName
err = Write(ctx)
if err != nil {
return
}
durWrite := time.Since(fromWrite).Seconds()
durTotal := time.Since(fromStart).Seconds()
logStatsAPI.Printf("XRefTable:\n%s\n", ctx)
logStatsAPI.Println("Timing:")
logStatsAPI.Printf("read : %6.3fs %4.1f%%\n", durRead, durRead/durTotal*100)
logStatsAPI.Printf("validate : %6.3fs %4.1f%%\n", durVal, durVal/durTotal*100)
logStatsAPI.Printf("optimize : %6.3fs %4.1f%%\n", durOpt, durOpt/durTotal*100)
logStatsAPI.Printf("write : %6.3fs %4.1f%%\n", durWrite, durWrite/durTotal*100)
logStatsAPI.Printf("total processing time: %6.3fs\n\n", durTotal)
ctx.Read.LogStats(logStatsAPI, ctx.Optimized)
ctx.Write.LogStats(logStatsAPI)
return
}
// ParsePageSelection ensures a correct page selection expression.
func ParsePageSelection(s string) (ps []string, err error) {
if s == "" {
return nil, nil
}
// Ensure valid comma separated expression of: {!}{-}# or {!}#-{#}
//
// Negated expressions:
// '!' negates an expression
// since '!' needs to be part of a single quoted string in bash
// as an alternative also 'n' works instead of "!"
//
// Extract all but page 4 may be expressed as: "1-,!4" or "1-,n4"
//
// The pageSelection is evaluated strictly from left to right!
// e.g. "!3,1-5" extracts pages 1-5 whereas "1-5,!3" extracts pages 1,2,4,5
//
if !selectedPagesRegExp.MatchString(s) {
return nil, errors.Errorf("-pages \"%s\" => syntax error\n", s)
}
//fmt.Printf("pageSelection: <%s>\n", pageSelection)
ps = strings.Split(s, ",")
return
}
func pagesForPageSelection(pageCount int, pageSelection *[]string) (selectedPages types.IntSet, err error) {
if pageSelection == nil {
logInfoAPI.Println("pagesForPageSelection: pageSelection is nil")
return
}
if len(*pageSelection) == 0 {
logStatsAPI.Println("pagesForPageSelection: pageSelection is empty")
}
selectedPages = types.IntSet{}
for _, v := range *pageSelection {
//logStatsAPI.Printf("pageExp: <%s>\n", v)
var negated bool
if v[0] == '!' || v[0] == 'n' {
negated = true
//logInfoAPI.Printf("is a negated exp\n")
v = v[1:]
}
if v[0] == '-' {
v = v[1:]
i, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
// Handle overflow gracefully
if i > pageCount {
i = pageCount
}
// identified
// -# ... select all pages up to and including #
// or !-# ... deselect all pages up to and including #
for j := 1; j <= i; j++ {
if negated {
selectedPages[j] = false
} else {
selectedPages[j] = true
}
}
continue
}
if strings.HasSuffix(v, "-") {
// must be #- ... select all pages from here until the end.
// or !#- ... deselect all pages from here until the end.
i, err := strconv.Atoi(v[:len(v)-1])
if err != nil {
return nil, err
}
// Handle overflow gracefully
if i > pageCount {
continue
}
for j := i; j <= pageCount; j++ {
if negated {
selectedPages[j] = false
} else {
selectedPages[j] = true
}
}
continue
}
// if v contains '-' somewhere in the middle
// this must be #-# ... select a page range
// or !#-# ... deselect a page range
pr := strings.Split(v, "-")
if len(pr) == 2 {
from, err := strconv.Atoi(pr[0])
if err != nil {
return nil, err
}
// Handle overflow gracefully
if from > pageCount {
continue
}
thru, err := strconv.Atoi(pr[1])
if err != nil {
return nil, err
}
// Handle overflow gracefully
if thru < from {
continue
}
if thru > pageCount {
thru = pageCount
}
for i := from; i <= thru; i++ {
if negated {
selectedPages[i] = false
} else {
selectedPages[i] = true
}
}
continue
}
// must be # ... select a specific page
// or !# ... deselect a sepcific page
i, err := strconv.Atoi(pr[0])
if err != nil {
return nil, err
}
// Handle overflow gracefully
if i > pageCount {
continue
}
if negated {
selectedPages[i] = false
} else {
selectedPages[i] = true
}
}
return
}
// Split generates a sequence of single page PDF files in dirOut creating one file for every page of inFile.
func Split(fileIn, dirOut string, config *types.Configuration) (err error) {
fromStart := time.Now()
fmt.Printf("splitting %s into %s ...\n", fileIn, dirOut)
ctx, durRead, durVal, durOpt, err := readValidateAndOptimize(fileIn, config, fromStart)
if err != nil {
return
}
fromWrite := time.Now()
err = writeSinglePagePDFs(ctx, nil, dirOut)
if err != nil {
return
}
durWrite := time.Since(fromWrite).Seconds()
durTotal := time.Since(fromStart).Seconds()
logStatsAPI.Printf("XRefTable:\n%s\n", ctx)
logStatsAPI.Println("Timing:")
logStatsAPI.Printf("read : %6.3fs %4.1f%%\n", durRead, durRead/durTotal*100)
logStatsAPI.Printf("validate : %6.3fs %4.1f%%\n", durVal, durVal/durTotal*100)
logStatsAPI.Printf("optimize : %6.3fs %4.1f%%\n", durOpt, durOpt/durTotal*100)
logStatsAPI.Printf("split : %6.3fs %4.1f%%\n", durWrite, durWrite/durTotal*100)
logStatsAPI.Printf("total processing time: %6.3fs\n\n", durTotal)
ctx.Read.LogStats(logStatsAPI, ctx.Optimized)
ctx.Write.LogStats(logStatsAPI)
return
}
// appendTo appends fileIn to ctxDest's page tree.
func appendTo(fileIn string, ctxDest *types.PDFContext) (err error) {
logStatsAPI.Printf("appendTo: appending %s to %s\n", fileIn, ctxDest.Read.FileName)
// Build a PDFContext for fileIn.
ctxSource, _, _, err := readAndValidate(fileIn, ctxDest.Configuration, time.Now())
if err != nil {
return
}
// Merge the source context into the dest context.
fmt.Printf("merging in %s ...\n", fileIn)
return merge.XRefTables(ctxSource, ctxDest)
}
// Merge some PDF files together and write the result to fileOut.
// This corresponds to concatenating these files in the order specified by filesIn.
// The first entry of filesIn serves as the destination xRefTable where all the remaining files gets merged into.
func Merge(filesIn []string, fileOut string, config *types.Configuration) (err error) {
fmt.Printf("merging into %s: %v\n", fileOut, filesIn)
//logErrorAPI.Printf("Merge: filesIn: %v\n", filesIn)
fileDest := filesIn[0]
ctxDest, _, _, err := readAndValidate(fileDest, config, time.Now())
if err != nil {
return
}
if ctxDest.XRefTable.Version() < types.V15 {
v, _ := types.Version("1.5")
ctxDest.XRefTable.RootVersion = &v
logStatsAPI.Println("Ensure V1.5 for writing object & xref streams")
}
// Repeatedly merge files into fileDest's xref table.
for _, file := range filesIn[1:] {
err = appendTo(file, ctxDest)
if err != nil {
return
}
}
err = optimize.XRefTable(ctxDest)
if err != nil {
return
}
err = validate.XRefTable(ctxDest.XRefTable)
if err != nil {
return
}
ctxDest.Write.Command = "Merge"
dirName, fileName := filepath.Split(fileOut)
ctxDest.Write.DirName = dirName
ctxDest.Write.FileName = fileName
err = Write(ctxDest)
if err != nil {
return
}
logStatsAPI.Printf("XRefTable:\n%s\n", ctxDest)
return
}
// ExtractImages dumps embedded image resources from fileIn into dirOut for selected pages.
func ExtractImages(fileIn, dirOut string, pageSelection *[]string, config *types.Configuration) (err error) {
fromStart := time.Now()
fmt.Printf("extracting images from %s into %s ...\n", fileIn, dirOut)
ctx, durRead, durVal, durOpt, err := readValidateAndOptimize(fileIn, config, fromStart)
if err != nil {
return
}
fromWrite := time.Now()
pages, err := pagesForPageSelection(ctx.PageCount, pageSelection)
if err != nil {
return
}
ctx.Write.DirName = dirOut
err = extract.Images(ctx, pages)
if err != nil {
return
}
durWrite := time.Since(fromWrite).Seconds()
durTotal := time.Since(fromStart).Seconds()
logStatsAPI.Printf("XRefTable:\n%s\n", ctx)
logStatsAPI.Println("Timing:")
logStatsAPI.Printf("read : %6.3fs %4.1f%%\n", durRead, durRead/durTotal*100)
logStatsAPI.Printf("validate : %6.3fs %4.1f%%\n", durVal, durVal/durTotal*100)
logStatsAPI.Printf("optimize : %6.3fs %4.1f%%\n", durOpt, durOpt/durTotal*100)
logStatsAPI.Printf("write images : %6.3fs %4.1f%%\n", durWrite, durWrite/durTotal*100)
logStatsAPI.Printf("total processing time: %6.3fs\n\n", durTotal)
return
}
// ExtractFonts dumps embedded fontfiles from fileIn into dirOut for selected pages.
func ExtractFonts(fileIn, dirOut string, pageSelection *[]string, config *types.Configuration) (err error) {
fromStart := time.Now()
fmt.Printf("extracting fonts from %s into %s ...\n", fileIn, dirOut)
ctx, durRead, durVal, durOpt, err := readValidateAndOptimize(fileIn, config, fromStart)
if err != nil {
return
}
fromWrite := time.Now()
pages, err := pagesForPageSelection(ctx.PageCount, pageSelection)
if err != nil {
return
}
ctx.Write.DirName = dirOut
err = extract.Fonts(ctx, pages)
if err != nil {
return
}
durWrite := time.Since(fromWrite).Seconds()
durTotal := time.Since(fromStart).Seconds()
logStatsAPI.Printf("XRefTable:\n%s\n", ctx)
logStatsAPI.Println("Timing:")
logStatsAPI.Printf("read : %6.3fs %4.1f%%\n", durRead, durRead/durTotal*100)
logStatsAPI.Printf("validate : %6.3fs %4.1f%%\n", durVal, durVal/durTotal*100)
logStatsAPI.Printf("optimize : %6.3fs %4.1f%%\n", durOpt, durOpt/durTotal*100)
logStatsAPI.Printf("write fonts : %6.3fs %4.1f%%\n", durWrite, durWrite/durTotal*100)
logStatsAPI.Printf("total processing time: %6.3fs\n\n", durTotal)
return
}
// ExtractPages generates single page PDF files from fileIn in dirOut for selected pages.
func ExtractPages(fileIn, dirOut string, pageSelection *[]string, config *types.Configuration) (err error) {
fromStart := time.Now()
fmt.Printf("extracting pages from %s into %s ...\n", fileIn, dirOut)
ctx, durRead, durVal, durOpt, err := readValidateAndOptimize(fileIn, config, fromStart)
if err != nil {
return
}
fromWrite := time.Now()
pages, err := pagesForPageSelection(ctx.PageCount, pageSelection)
if err != nil {
return
}
err = writeSinglePagePDFs(ctx, pages, dirOut)
if err != nil {
return
}
durWrite := time.Since(fromWrite).Seconds()
durTotal := time.Since(fromStart).Seconds()
logStatsAPI.Printf("XRefTable:\n%s\n", ctx)
logStatsAPI.Println("Timing:")
logStatsAPI.Printf("read : %6.3fs %4.1f%%\n", durRead, durRead/durTotal*100)
logStatsAPI.Printf("validate : %6.3fs %4.1f%%\n", durVal, durVal/durTotal*100)
logStatsAPI.Printf("optimize : %6.3fs %4.1f%%\n", durOpt, durOpt/durTotal*100)
logStatsAPI.Printf("write PDFs : %6.3fs %4.1f%%\n", durWrite, durWrite/durTotal*100)
logStatsAPI.Printf("total processing time: %6.3fs\n\n", durTotal)
ctx.Read.LogStats(logStatsAPI, ctx.Optimized)
ctx.Write.LogStats(logStatsAPI)
return
}
// ExtractContent dumps "PDF source" files from fileIn into dirOut for selected pages.
func ExtractContent(fileIn, dirOut string, pageSelection *[]string, config *types.Configuration) (err error) {
fromStart := time.Now()
fmt.Printf("extracting content from %s into %s ...\n", fileIn, dirOut)
ctx, durRead, durVal, durOpt, err := readValidateAndOptimize(fileIn, config, fromStart)
if err != nil {
return
}
fromWrite := time.Now()
pages, err := pagesForPageSelection(ctx.PageCount, pageSelection)
if err != nil {
return
}
ctx.Write.DirName = dirOut
err = extract.Content(ctx, pages)
if err != nil {
return
}
durWrite := time.Since(fromWrite).Seconds()
durTotal := time.Since(fromStart).Seconds()
logStatsAPI.Printf("XRefTable:\n%s\n", ctx)
logStatsAPI.Println("Timing:")
logStatsAPI.Printf("read : %6.3fs %4.1f%%\n", durRead, durRead/durTotal*100)
logStatsAPI.Printf("validate : %6.3fs %4.1f%%\n", durVal, durVal/durTotal*100)
logStatsAPI.Printf("optimize : %6.3fs %4.1f%%\n", durOpt, durOpt/durTotal*100)
logStatsAPI.Printf("write content : %6.3fs %4.1f%%\n", durWrite, durWrite/durTotal*100)
logStatsAPI.Printf("total processing time: %6.3fs\n\n", durTotal)
return
}
// ExtractText converts PDF into text
func ExtractText(fileIn string, config *types.Configuration) (io.Reader, error) {
fromStart := time.Now()
fmt.Printf("extracting text from %s ...\n", fileIn)
ctx, durRead, durVal, durOpt, err := readValidateAndOptimize(fileIn, config, fromStart)
if err != nil {
return nil, err
}
fromWrite := time.Now()
r, err := extract.Text(ctx)
if err != nil {
return nil, err
}
durWrite := time.Since(fromWrite).Seconds()
durTotal := time.Since(fromStart).Seconds()
logStatsAPI.Printf("XRefTable:\n%s\n", ctx)
logStatsAPI.Println("Timing:")
logStatsAPI.Printf("read : %6.3fs %4.1f%%\n", durRead, durRead/durTotal*100)
logStatsAPI.Printf("validate : %6.3fs %4.1f%%\n", durVal, durVal/durTotal*100)
logStatsAPI.Printf("optimize : %6.3fs %4.1f%%\n", durOpt, durOpt/durTotal*100)
logStatsAPI.Printf("write content : %6.3fs %4.1f%%\n", durWrite, durWrite/durTotal*100)
logStatsAPI.Printf("total processing time: %6.3fs\n\n", durTotal)
return r, nil
}
// Trim generates a trimmed version of fileIn containing all pages selected.
func Trim(fileIn, fileOut string, pageSelection *[]string, config *types.Configuration) (err error) {
// pageSelection points to an empty slice if flag pages was omitted.
fromStart := time.Now()
fmt.Printf("trimming %s ...\n", fileIn)
ctx, durRead, durVal, durOpt, err := readValidateAndOptimize(fileIn, config, fromStart)
if err != nil {
return
}
fromWrite := time.Now()
pages, err := pagesForPageSelection(ctx.PageCount, pageSelection)
if err != nil {
return
}
ctx.Write.Command = "Trim"
ctx.Write.ExtractPages = pages
dirName, fileName := filepath.Split(fileOut)
ctx.Write.DirName = dirName
ctx.Write.FileName = fileName
err = Write(ctx)
if err != nil {
return
}
durWrite := time.Since(fromWrite).Seconds()
durTotal := time.Since(fromStart).Seconds()
logStatsAPI.Printf("XRefTable:\n%s\n", ctx)
logStatsAPI.Println("Timing:")
logStatsAPI.Printf("read : %6.3fs %4.1f%%\n", durRead, durRead/durTotal*100)
logStatsAPI.Printf("validate : %6.3fs %4.1f%%\n", durVal, durVal/durTotal*100)
logStatsAPI.Printf("optimize : %6.3fs %4.1f%%\n", durOpt, durOpt/durTotal*100)
logStatsAPI.Printf("write PDF : %6.3fs %4.1f%%\n", durWrite, durWrite/durTotal*100)
logStatsAPI.Printf("total processing time: %6.3fs\n\n", durTotal)
ctx.Read.LogStats(logStatsAPI, ctx.Optimized)
ctx.Write.LogStats(logStatsAPI)
return
}