-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (87 loc) · 1.71 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
package main
import (
"flag"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"github.com/devlights/gord"
"github.com/devlights/gord/constants"
)
type target struct {
filePath string
absPath string
pdfPath string
verbose bool
}
func (me *target) abs() string {
if me.absPath == "" {
v, _ := filepath.Abs(me.filePath)
me.absPath = v
}
if me.verbose {
slog.Info("abs", "path", me.absPath)
}
return me.absPath
}
func (me *target) convert() string {
if me.absPath == "" {
me.abs()
}
me.pdfPath = me.absPath[:strings.Index(me.absPath, filepath.Ext(me.absPath))] + ".pdf"
if me.verbose {
slog.Info("convert", "abs", me.absPath, "pdf", me.pdfPath)
}
return me.pdfPath
}
func main() {
var (
verbose bool
)
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "usage: word2pdf.exe [-v] word-file-path")
flag.PrintDefaults()
}
flag.BoolVar(&verbose, "v", false, "verbose log output")
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
return
}
if err := run(&target{filePath: flag.Arg(0), verbose: verbose}); err != nil {
slog.Error(err.Error())
}
}
func run(p *target) error {
if p.verbose {
slog.Info("start")
defer slog.Info("done")
}
quitFn := gord.MustInitGord()
defer quitFn()
g, gordReleaseFn := gord.MustNewGord()
defer gordReleaseFn()
if err := g.SetVisible(false); err != nil {
return err
}
docs, err := g.Documents()
if err != nil {
return err
}
doc, docReleaseFn, err := docs.Open(p.abs())
if err != nil {
return err
}
defer docReleaseFn()
if p.verbose {
slog.Info("Export start")
}
if err := doc.ExportAsFixedFormat(p.convert(), constants.WdExportFormatPDF); err != nil {
return err
}
if p.verbose {
slog.Info("Export end")
}
return nil
}