-
Notifications
You must be signed in to change notification settings - Fork 4
/
export_pdf.go
89 lines (70 loc) · 1.24 KB
/
export_pdf.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/devlights/goxcel"
"github.com/devlights/goxcel/constants"
)
// flag parameters
var (
src string
dst string
)
// logs
var (
appLog = log.New(os.Stdout, ">>> ", 0)
)
func main() {
var (
returnCode int
)
if err := run(); err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
returnCode = -1
}
appLog.Println("done")
os.Exit(returnCode)
}
func run() error {
abs := func(p string) string {
v, _ := filepath.Abs(p)
return v
}
flag.StringVar(&src, "src", "", "source file")
flag.StringVar(&dst, "dst", "result.pdf", "output pdf name")
flag.Parse()
if src == "" {
flag.Usage()
return nil
}
quitFn, _ := goxcel.InitGoxcel()
defer quitFn()
g, r, _ := goxcel.NewGoxcel()
defer r()
_ = g.Silent(false)
wbs, err := g.Workbooks()
if err != nil {
return err
}
absPath := abs(src)
wb, wbReleaseFn, err := wbs.Open(abs(src))
if err != nil {
return err
}
defer wbReleaseFn()
appLog.Printf("WorkBook Open: %s", absPath)
ws, err := wb.Sheets(1)
if err != nil {
return err
}
absPath = abs(src)
err = ws.ExportAsFixedFormat(constants.XlTypePDF, absPath)
if err != nil {
return err
}
appLog.Printf("Export PDF : %s", absPath)
return nil
}