-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpdf_actions.go
224 lines (207 loc) · 5.99 KB
/
pdf_actions.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
// Copyright 2017, 2022 The Agostle Authors. All rights reserved.
// Use of this source code is governed by an Apache 2.0
// license that can be found in the LICENSE file.
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"sort"
"strings"
"context"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/tgulacsi/agostle/converter"
)
func init() {
pdfCmd := &ffcli.Command{Name: "pdf", ShortHelp: "pdf commands"}
subcommands = append(subcommands, pdfCmd)
var out string
withOutFlag := func(name string) *flag.FlagSet {
fs := newFlagSet(name)
fs.StringVar(&out, "o", "", "output file")
return fs
}
{
var sort bool
fs := withOutFlag("merge")
fs.BoolVar(&sort, "sort", false, "shall we sort the files by name before merge?")
mergeCmd := ffcli.Command{Name: "merge", ShortHelp: "merges the given PDFs into one",
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
for i, s := range args {
if s == "" {
args[i] = "-"
}
}
if err := mergePdf(ctx, out, args, sort); err != nil {
return fmt.Errorf("mergePDF out=%q sort=%v inp=%v: %w", out, sort, args, err)
}
return nil
},
}
pdfCmd.Subcommands = append(pdfCmd.Subcommands, &mergeCmd)
}
fs := withOutFlag("split")
flagSplitPages := fs.String("pages", "", "pages (comma separated)")
splitCmd := ffcli.Command{Name: "split", ShortHelp: "splits the given PDF into one per page",
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
var splitInp string
if len(args) != 0 {
splitInp = args[0]
}
if splitInp == "" {
splitInp = "-"
}
if err := splitPdfZip(ctx, out, splitInp, parseUint16s(strings.Split(*flagSplitPages, ","))); err != nil {
return fmt.Errorf("splitPdfZip out=%q inp=%q: %w", out, splitInp, err)
}
return nil
},
}
pdfCmd.Subcommands = append(pdfCmd.Subcommands, &splitCmd)
countCmd := ffcli.Command{Name: "count", ShortHelp: "prints the number of pages in the given pdf",
Exec: func(ctx context.Context, args []string) error {
var countInp string
if len(args) != 0 {
countInp = args[0]
}
if err := countPdf(ctx, countInp); err != nil {
return fmt.Errorf("countPdf inp=%s: %w", countInp, err)
}
return nil
},
}
pdfCmd.Subcommands = append(pdfCmd.Subcommands, &countCmd)
fs = withOutFlag("clean")
cleanCmd := ffcli.Command{Name: "clean", ShortHelp: "clean PDF from encryption", FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
var cleanInp string
if len(args) != 0 {
cleanInp = args[0]
}
if err := cleanPdf(ctx, out, cleanInp); err != nil {
return fmt.Errorf("cleanPdf out=%q inp=%q: %w", out, cleanInp, err)
}
return nil
},
}
pdfCmd.Subcommands = append(pdfCmd.Subcommands, &cleanCmd)
{
var mime string
fs = withOutFlag("topdf")
fs.StringVar(&mime, "mime", "application/octet-stream", "input mimetype")
topdfCmd := ffcli.Command{Name: "topdf",
ShortHelp: "tries to convert the given file (you can specify its mime-type) to PDF",
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
var topdfInp string
if len(args) != 0 {
topdfInp = args[0]
}
if err := toPdf(out, topdfInp, mime); err != nil {
return fmt.Errorf("topdf out=%q inp=%q mime=%q: %w", out, topdfInp, mime, err)
}
return nil
},
}
pdfCmd.Subcommands = append(pdfCmd.Subcommands, &topdfCmd)
}
fs = withOutFlag("fill")
fillPdfCmd := ffcli.Command{Name: "fill", ShortHelp: "fill PDF form",
ShortUsage: `fill PDF form
input.pdf key1=value1 key2=value2...`,
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
var fillInp string
var fillKeyvals []string
if len(args) != 0 {
fillInp = args[0]
fillKeyvals = args[1:]
}
if err := fillFdf(ctx, out, fillInp, fillKeyvals...); err != nil {
return fmt.Errorf("fillPdf out=%q inp=%q keyvals=%q: %w", out, fillInp, fillKeyvals, err)
}
return nil
},
}
pdfCmd.Subcommands = append(pdfCmd.Subcommands, &fillPdfCmd)
}
func splitPdfZip(ctx context.Context, outfn, inpfn string, pages []uint16) error {
var changed bool
if inpfn, changed = ensureFilename(inpfn, false); changed {
defer func() { _ = os.Remove(inpfn) }()
}
filenames, cleanup, err := converter.PdfSplit(ctx, inpfn, pages)
if err != nil {
return err
}
defer func() { _ = cleanup() }()
outfh, err := openOut(outfn)
if err != nil {
return err
}
files := make([]converter.ArchFileItem, len(filenames))
for i, nm := range filenames {
files[i] = converter.ArchFileItem{Filename: nm}
}
ze := converter.ZipFiles(outfh, false, false, files...)
closeErr := outfh.Close()
if ze != nil {
return ze
}
return closeErr
}
func mergePdf(ctx context.Context, outfn string, inpfn []string, sortFiles bool) error {
if sortFiles {
sort.Strings(inpfn)
}
return converter.PdfMerge(ctx, outfn, inpfn...)
}
func cleanPdf(ctx context.Context, outfn, inpfn string) error {
var changed bool
fmt.Fprintf(os.Stderr, "inpfn=%s outfn=%s\n", inpfn, outfn)
if inpfn, changed = ensureFilename(inpfn, false); changed {
defer func() { _ = os.Remove(inpfn) }()
}
outfn, changed = ensureFilename(outfn, true)
fmt.Fprintf(os.Stderr, "inpfn=%s outfn=%s\n", inpfn, outfn)
if err := converter.PdfRewrite(ctx, outfn, inpfn); err != nil {
if changed {
_ = os.Remove(outfn)
}
return err
}
fh, err := os.Open(outfn)
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, fh)
_ = fh.Close()
return err
}
func toPdf(outfn, inpfn string, mime string) error {
return errors.New("not implemented")
}
func countPdf(ctx context.Context, inpfn string) error {
n, err := converter.PdfPageNum(ctx, inpfn)
if err != nil {
return err
}
fmt.Printf("%d\n", n)
return nil
}
func fillFdf(ctx context.Context, outfn, inpfn string, kv ...string) error {
values := make(map[string]string, len(kv))
for _, txt := range kv {
i := strings.IndexByte(txt, '=')
if i < 0 {
logger.Info("no = in key=value arg!")
continue
}
values[txt[:i]] = txt[i+1:]
}
return converter.PdfFillFdf(ctx, outfn, inpfn, values)
}