-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfcovers.go
146 lines (118 loc) · 3.17 KB
/
pdfcovers.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
pdflicense "github.com/unidoc/unipdf/v3/common/license"
pdfcore "github.com/unidoc/unipdf/v3/core"
pdf "github.com/unidoc/unipdf/v3/model"
)
var usageMessage = `usage: pdfcovers [-s] [-o file] [pdf files]
Reads pdfs and creates a new pdf containing the first page of each.
If a pdf is corrupted or encrypted and cannot extract the cover,
then it logs it and proceeds with the next one.
In every page it adds a text annotation with the filename of the origin pdf.
Examples:
pdfcovers -o index.pdf *.pdf
pdfcovers *.pdf | 9 page
find . -name *.pdf | pdfcovers -s -o index.pdf
find . -name *.pdf | pdfcovers -s -o index.pdf cover-page.pdf
find . -name *.pdf -exec pdfcovers -s -o index.pdf '{}' '+'
Flags:
`
func usage() {
fmt.Fprintf(os.Stderr, usageMessage)
flag.PrintDefaults()
os.Exit(2)
}
var outputFile = flag.String("o", "", "The output filename. The default is stdout")
var sourceStdin = flag.Bool("s", false, "Read file names from stdin. Useful with 'find | pdfcovers -s'")
var licenseFile = flag.String("lf", "", "A file with a unidoc(http://unidoc.io/pricing) license")
var customerName = flag.String("ln", "", "The name of the customer with a unidoc(http://unidoc.io/pricing) license")
func setLicense() {
if *licenseFile == "" && *customerName == "" {
return
}
licenseKey, err := ioutil.ReadFile(*licenseFile)
if err != nil {
log.Fatal("Cannot read the unidoc license file: ", err)
}
if err := pdflicense.SetLicenseKey(string(licenseKey), *customerName); err != nil {
log.Fatal("Cannot set the unidoc license: ", err)
}
}
func appendPage(w *pdf.PdfWriter, fname string, pageNum int) error {
fin, err := os.Open(fname)
if err != nil {
return err
}
defer fin.Close()
r, err := pdf.NewPdfReader(fin)
if err != nil {
return err
}
page, err := r.GetPage(pageNum)
if err != nil {
return err
}
ann := pdf.NewPdfAnnotationText()
ann.Contents = pdfcore.MakeString(fname)
ann.Rect = pdfcore.MakeArrayFromIntegers([]int{20, 100, 60, 150})
page.AddAnnotation(ann.PdfAnnotation)
err = w.AddPage(page)
if err != nil {
return err
}
return nil
}
func writeOutput(w *pdf.PdfWriter) error {
var fout io.WriteSeeker
if *outputFile == "" {
fout = os.Stdout
} else {
f, err := os.Create(*outputFile)
if err != nil {
return err
}
defer f.Close()
fout = f
}
return w.Write(fout)
}
func main() {
log.SetPrefix("")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
if flag.NArg() == 0 && !*sourceStdin {
usage()
}
setLicense()
w := pdf.NewPdfWriter()
if flag.NArg() > 0 {
for _, fname := range flag.Args() {
if err := appendPage(&w, fname, 1); err != nil {
log.Println("Failed to add pages from "+fname+":", err)
}
}
}
if *sourceStdin {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fname := scanner.Text()
if err := appendPage(&w, fname, 1); err != nil {
log.Println("Failed to add pages from "+fname+":", err)
}
}
if err := scanner.Err(); err != nil {
log.Println("Failed to read filenames from stdin:", err)
}
}
if err := writeOutput(&w); err != nil {
log.Println("Failed to write output pdf:", err)
}
}