forked from pdfcpu/pdfcpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
153 lines (128 loc) · 4.45 KB
/
process.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
package pdflib
import "github.com/hhrutter/pdflib/types"
type commandMode int
// The available commands for the CLI.
const (
VALIDATE commandMode = iota
OPTIMIZE
SPLIT
MERGE
EXTRACTIMAGES
EXTRACTFONTS
EXTRACTPAGES
EXTRACTCONTENT
TRIM
)
// Command represents an execution context.
type Command struct {
Mode commandMode // VALIDATE OPTIMIZE SPLIT MERGE EXTRACT TRIM
InFile *string // * * * - * *
InFiles *[]string // - - - * - -
OutFile *string // - * - * - *
OutDir *string // - - * - * -
PageSelection *[]string // - - - - * *
Config *types.Configuration //
}
// ValidateCommand creates a new ValidateCommand.
func ValidateCommand(pdfFileName string, config *types.Configuration) Command {
return Command{
Mode: VALIDATE,
InFile: &pdfFileName,
Config: config}
}
// OptimizeCommand creates a new OptimizeCommand.
func OptimizeCommand(pdfFileNameIn, pdfFileNameOut string, config *types.Configuration) Command {
return Command{
Mode: OPTIMIZE,
InFile: &pdfFileNameIn,
OutFile: &pdfFileNameOut,
Config: config}
}
// SplitCommand creates a new SplitCommand.
func SplitCommand(pdfFileNameIn, dirNameOut string, config *types.Configuration) Command {
return Command{
Mode: SPLIT,
InFile: &pdfFileNameIn,
OutDir: &dirNameOut,
Config: config}
}
// MergeCommand creates a new MergeCommand.
func MergeCommand(pdfFileNamesIn []string, pdfFileNameOut string, config *types.Configuration) Command {
return Command{
Mode: MERGE,
InFiles: &pdfFileNamesIn,
OutFile: &pdfFileNameOut,
Config: config}
}
// ExtractImagesCommand creates a new ExtractImagesCommand.
// (experimental)
func ExtractImagesCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *types.Configuration) Command {
return Command{
Mode: EXTRACTIMAGES,
InFile: &pdfFileNameIn,
OutDir: &dirNameOut,
PageSelection: &pageSelection,
Config: config}
}
// ExtractFontsCommand creates a new ExtractFontsCommand.
// (experimental)
func ExtractFontsCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *types.Configuration) Command {
return Command{
Mode: EXTRACTFONTS,
InFile: &pdfFileNameIn,
OutDir: &dirNameOut,
PageSelection: &pageSelection,
Config: config}
}
// ExtractPagesCommand creates a new ExtractPagesCommand.
func ExtractPagesCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *types.Configuration) Command {
return Command{
Mode: EXTRACTPAGES,
InFile: &pdfFileNameIn,
OutDir: &dirNameOut,
PageSelection: &pageSelection,
Config: config}
}
// ExtractContentCommand creates a new ExtractContentCommand.
func ExtractContentCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *types.Configuration) Command {
return Command{
Mode: EXTRACTCONTENT,
InFile: &pdfFileNameIn,
OutDir: &dirNameOut,
PageSelection: &pageSelection,
Config: config}
}
// TrimCommand creates a new TrimCommand.
func TrimCommand(pdfFileNameIn, pdfFileNameOut string, pageSelection []string, config *types.Configuration) Command {
// A slice parameter can be called with nil => empty slice.
return Command{
Mode: TRIM,
InFile: &pdfFileNameIn,
OutFile: &pdfFileNameOut,
PageSelection: &pageSelection,
Config: config}
}
// Process executes a pdflib command.
func Process(cmd *Command) (err error) {
switch cmd.Mode {
case VALIDATE:
err = Validate(*cmd.InFile, cmd.Config)
case OPTIMIZE:
err = Optimize(*cmd.InFile, *cmd.OutFile, cmd.Config)
case SPLIT:
err = Split(*cmd.InFile, *cmd.OutDir, cmd.Config)
case MERGE:
err = Merge(*cmd.InFiles, *cmd.OutFile, cmd.Config)
case EXTRACTIMAGES:
err = ExtractImages(*cmd.InFile, *cmd.OutDir, cmd.PageSelection, cmd.Config)
case EXTRACTFONTS:
err = ExtractFonts(*cmd.InFile, *cmd.OutDir, cmd.PageSelection, cmd.Config)
case EXTRACTPAGES:
err = ExtractPages(*cmd.InFile, *cmd.OutDir, cmd.PageSelection, cmd.Config)
case EXTRACTCONTENT:
err = ExtractContent(*cmd.InFile, *cmd.OutDir, cmd.PageSelection, cmd.Config)
case TRIM:
err = Trim(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.Config)
}
return
}