-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.go
39 lines (34 loc) · 862 Bytes
/
compile.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
package main
import (
"os"
"path/filepath"
"strconv"
gopdf "github.com/signintech/gopdf"
)
func addImage(pdf *gopdf.GoPdf, path string) {
pdf.AddPage()
pdf.Image(path, 0, 0, nil)
}
// Compile a directory of images into a PDF
// inpath: The directory of images
// outpath: The path to save the PDF
func CompilePdf(inpath string, outpath string) {
println("Compiling PDF")
pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
// Iterate through the directory
files, err := os.ReadDir(inpath)
if err != nil {
panic(err)
}
// get number of files in directory
amount := len(files)
for i := 0; i < amount; i++ {
filename := strconv.Itoa(i) + ".jpg"
addImage(&pdf, filepath.Join(inpath, filename))
}
outpath = filepath.Join(outpath, "output.pdf")
pdf.WritePdf(outpath)
// delete the directory
os.RemoveAll(inpath)
}