-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathback.go
199 lines (156 loc) · 3.88 KB
/
back.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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
)
// CombinePDFs combines the specified PDF files into a single PDF file.
func combinePDFs(output string, input []string) error {
config := model.NewDefaultConfiguration()
err := api.MergeCreateFile(input, output, false, config)
if err != nil {
println(err.Error())
return err
}
return nil
}
// RotatePDF90 rotates the first page of the specified PDF file by 90 degrees clockwise.
// The rotated PDF is saved as a new file with ".rotated.pdf" appended to the original filename.
//
// Parameters:
// - input: The file path of the PDF to be rotated.
//
// Returns:
// - error: An error object if the rotation fails, otherwise nil.
func RotatePDF90(input string, pages []string) error {
outputFolder := filepath.Dir(input)
temp := strings.Split(input, "/")
length := len(temp)
outputfile := temp[length-1] + ".rotated.pdf"
output := outputFolder + "/" + outputfile
fmt.Println(output)
config := model.NewDefaultConfiguration()
err := api.RotateFile(input, output, 90, pages, config)
if err != nil {
println(err.Error())
return err
}
return nil
}
func rotateFile(input string) error {
pagecout, err := api.PageCountFile(input)
if err != nil {
println(err.Error())
return err
}
pages := make([]string, pagecout)
for i := 0; i < pagecout; i++ {
pages[i] = fmt.Sprint(i + 1)
}
fmt.Println(pages)
err = RotatePDF90(input, pages)
if err != nil {
println(err.Error())
return err
}
return nil
}
// DOES NOT WORK also not needed
// func MakeLandScape(input string) error {
// config := model.NewDefaultConfiguration()
// resize := model.Resize{Scale: 0.0, Unit: types.INCHES, PageDim: &types.Dim{Width: 11.0, Height: 8.5}}
// err := api.ResizeFile(input, "resized"+input, []string{"1"}, &resize, config)
// if err != nil {
// println(err.Error())
// return err
// }
// return nil
// }
func CombinePDFBasedOnDate(input []PDF, output string) error {
var Paths []string
for _, pdf := range input {
Paths = append(Paths, pdf.path)
}
err := combinePDFs(output, Paths)
if err != nil {
println(err.Error())
return err
}
return nil
}
func GetCreationDate(path string) string {
file, err := os.Open(path)
if err != nil {
fmt.Println("Error opening PDF file")
return ""
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "CreationDate") {
fmt.Println(line)
return line
}
}
if err := scanner.Err(); err != nil {
fmt.Println("Error scanning PDF file")
}
return ""
}
type PDF struct {
path string
creationDate string
modDate string
}
func (p *PDF) GetCreationDate() {
p.creationDate = GetCreationDate(p.path)
}
func (p *PDF) string() string {
rstring := p.path + "\t" + p.creationDate
return rstring
}
func CombineMainMethod(argument string) {
var sfpd []PDF
filepath.Walk(argument, func(path string, info os.FileInfo, err error) error {
if strings.Contains(path, ".pdf") && !strings.Contains(path, "CombinedOutput.pdf") {
fdp := PDF{path: path}
sfpd = append(sfpd, fdp)
}
return nil
})
for _, pdf := range sfpd {
pdf.GetCreationDate()
}
sort.Slice(sfpd, func(i, j int) bool {
return sfpd[i].creationDate < sfpd[j].creationDate
})
err := CombinePDFBasedOnDate(sfpd, argument+"/CombinedOutput.pdf")
if err != nil {
fmt.Println(err.Error())
}
}
func main() {
args := os.Args[1:]
if len(args) == 0 {
fmt.Println("No arguments provided.")
return
} else if args[0] == "-R" {
err := rotateFile(args[1])
if err != nil {
fmt.Println(err.Error())
os.Exit(125)
}
os.Exit(0)
} else if args[0] == "-CR" {
CombineMainMethod(args[1])
get := strings.Split(args[1], "/")
got := strings.Join(get[:len(get)], "/")
rotateFile(got + "/CombinedOutput.pdf")
}
}