-
Notifications
You must be signed in to change notification settings - Fork 3
/
pdfsort.go
108 lines (95 loc) · 2.1 KB
/
pdfsort.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
// $ pdfsort <input> <output>
//
// pdfsort takes two single-sided scans and reorders the pages as if it was a double-sided scan.
//
// When scanning a double-sided stack of papers on a single-sided scanner, first scan one side (producing pages 1, 3, ..., 2n-1), and then flip the entire stack and scan the other side (producing pages 2n, 2n-2, ..., 2).
// pdfsort orders a pdf produced this way.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/unidoc/unidoc/common/license"
pdf "github.com/unidoc/unidoc/pdf/model"
)
var licenseKey = flag.String("license_key", "", "file containing unidoc license key")
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: pdfsort <input> <output>\n")
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
if len(flag.Args()) != 2 {
flag.Usage()
os.Exit(1)
}
if licenseKey != nil && *licenseKey != "" {
if err := loadLicense(*licenseKey); err != nil {
log.Fatalf("pdfsort: error loading license: %v\n", err)
}
}
if err := pdfsort(flag.Arg(0), flag.Arg(1)); err != nil {
log.Fatalf("pdfsort: %v", err)
}
}
func loadLicense(filename string) error {
key, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return license.SetLicenseKey(string(key))
}
func pages(n int) []int {
p := make([]int, 0, n)
for i := 0; i < n; i++ {
if i%2 == 0 {
p = append(p, i/2+1)
} else {
p = append(p, n-(i-1)/2)
}
}
return p
}
func pdfsort(in, out string) error {
f, err := os.Open(in)
if err != nil {
return err
}
defer f.Close()
r, err := pdf.NewPdfReader(f)
if err != nil {
return err
}
w := pdf.NewPdfWriter()
n, err := r.GetNumPages()
if err != nil {
return err
}
if n%2 != 0 {
return fmt.Errorf("expected even number of pages, found %d", n)
}
ocProps, err := r.GetOCProperties()
if err != nil {
return err
}
w.SetOCProperties(ocProps)
for _, i := range pages(n) {
page, err := r.GetPage(i)
if err != nil {
return err
}
if err = w.AddPage(page); err != nil {
return err
}
}
wf, err := os.Create(out)
if err != nil {
return err
}
defer wf.Close()
return w.Write(wf)
}