-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (71 loc) · 1.88 KB
/
main.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
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/pdfcpu/pdfcpu/pkg/api"
)
var formatRegex = regexp.MustCompile(`_(A\d+|Letter|Legal)_(\d+)`)
type FileFormatCount struct {
File string
Format string
Count int
}
func main() {
folder := "/Users/johannes/Monte Cloud/05 Veranstaltungen/01 Fasentsonntag/2025/Preislisten/PDF"
files, err := os.ReadDir(folder)
if err != nil {
fmt.Println("Error reading directory:", err)
return
}
// Map to store format to file associations
formatMap := make(map[string][]string)
for _, file := range files {
if file.IsDir() || !strings.HasSuffix(file.Name(), ".pdf") {
continue
}
formats := extractFormats(file.Name())
if len(formats) == 0 {
fmt.Println("Skipping file with no format info:", file.Name())
continue
}
for _, f := range formats {
for i := 0; i < f.Count; i++ {
formatMap[f.Format] = append(formatMap[f.Format], filepath.Join(folder, file.Name()))
}
}
}
// Merge PDFs by format
for format, pdfFiles := range formatMap {
sort.Strings(pdfFiles) // Sorting ensures consistent order
outputFile := filepath.Join(folder, fmt.Sprintf("merged_%s.pdf", format))
fmt.Println("Merging PDFs for", format, "into", outputFile)
err := mergePDFs(pdfFiles, outputFile)
if err != nil {
fmt.Println("Error merging PDFs for", format, ":", err)
}
}
}
func extractFormats(filename string) []FileFormatCount {
matches := formatRegex.FindAllStringSubmatch(filename, -1)
if matches == nil {
return nil
}
var results []FileFormatCount
for _, match := range matches {
count := 1
fmt.Sscanf(match[2], "%d", &count)
results = append(results, FileFormatCount{
File: filename,
Format: match[1],
Count: count,
})
}
return results
}
func mergePDFs(inputFiles []string, outputFile string) error {
return api.MergeCreateFile(inputFiles, outputFile, false, nil)
}