-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
143 lines (123 loc) · 3.7 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
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
package main
import (
"flag"
"fmt"
"image/jpeg"
"log"
"os"
"path/filepath"
"sort"
"strings"
"github.com/signintech/gopdf"
)
// getImageDimensions retrieves the dimensions of a JPEG image
func getImageDimensions(filename string) (float64, float64, error) {
file, err := os.Open(filename)
if err != nil {
return 0, 0, err
}
defer file.Close()
img, err := jpeg.Decode(file)
if err != nil {
return 0, 0, err
}
bounds := img.Bounds()
width := float64(bounds.Dx())
height := float64(bounds.Dy())
return width, height, nil
}
// jpegToPDF converts JPEG images to a PDF file
func jpegToPDF(jpegFiles []string, outputFile string) error {
pdf := gopdf.GoPdf{}
// Start the PDF with A4 page size
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
for _, jpegFile := range jpegFiles {
// Get dimensions of the image
imgWidth, imgHeight, err := getImageDimensions(jpegFile)
if err != nil {
return fmt.Errorf("failed to get dimensions of image %s: %v", jpegFile, err)
}
// Calculate scaling factors to fit the image in the A4 page size
pageWidth, pageHeight := gopdf.PageSizeA4.W, gopdf.PageSizeA4.H
scale := 1.0
if imgWidth > pageWidth || imgHeight > pageHeight {
widthRatio := pageWidth / imgWidth
heightRatio := pageHeight / imgHeight
scale = min(widthRatio, heightRatio)
}
// Calculate the new dimensions of the image
newWidth := imgWidth * scale
newHeight := imgHeight * scale
// Center the image on the page
xOffset := (pageWidth - newWidth) / 2
yOffset := (pageHeight - newHeight) / 2
// Add a new page for each image and insert the image
pdf.AddPage()
err = pdf.Image(jpegFile, xOffset, yOffset, &gopdf.Rect{W: newWidth, H: newHeight})
if err != nil {
return fmt.Errorf("failed to add image %s to PDF: %v", jpegFile, err)
}
}
err := pdf.WritePdf(outputFile) // Write the PDF to the specified file
if err != nil {
return fmt.Errorf("failed to write PDF to file: %v", err)
}
fmt.Println("JPEGs converted to PDF successfully:", outputFile)
return nil
}
// min returns the smaller of two float64 values
func min(a, b float64) float64 {
if a < b {
return a
}
return b
}
// getJPEGFiles retrieves all JPEG files from the specified folder
func getJPEGFiles(folder string) ([]string, error) {
var jpegFiles []string
err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Check if the file is a JPEG
if !info.IsDir() && (filepath.Ext(path) == ".jpg" || filepath.Ext(path) == ".jpeg") {
jpegFiles = append(jpegFiles, path)
}
return nil
})
if err != nil {
return nil, err
}
sort.Strings(jpegFiles) // Sort the JPEG files alphabetically
return jpegFiles, nil
}
func main() {
// Command-line flags
folderPtr := flag.String("folder", "", "Path to the folder containing JPEG files")
filesPtr := flag.String("files", "", "Comma-separated list of JPEG filenames")
outputFile := flag.String("output", "merged_pdf.pdf", "Output PDF filename")
flag.Parse()
var jpegFiles []string
var err error
// Determine if we should use folder or list of files
if *folderPtr != "" {
jpegFiles, err = getJPEGFiles(*folderPtr)
if err != nil {
log.Fatal("Error retrieving JPEG files:", err)
}
} else if *filesPtr != "" {
// Split the comma-separated list into an array
jpegFiles = strings.Split(*filesPtr, ",")
for i := range jpegFiles {
jpegFiles[i] = strings.TrimSpace(jpegFiles[i]) // Trim whitespace
}
sort.Strings(jpegFiles) // Sort the files alphabetically
} else {
log.Fatal("Please provide either a folder path or a list of JPEG files.")
}
// Convert JPEG to PDF
err = jpegToPDF(jpegFiles, *outputFile)
if err != nil {
log.Println("Error converting JPEGs to PDF:", err)
}
}