-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (114 loc) · 2.73 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
package main
import (
"flag"
"fmt"
"github.com/kpawlik/gofpdf"
"io/ioutil"
"path/filepath"
"strings"
"time"
)
const (
// ScaleFactor is default scale factor to fit svg into PDF
ScaleFactor = 0.1
)
var (
format string
orientation string
out string
dir string
lineW float64
scale float64
timing bool
)
func init() {
flag.StringVar(&format, "format", "A4", "Page format (A5|A4|A3)")
flag.StringVar(&orientation, "orientation", "P", "Page orientation (P|L)")
flag.StringVar(&out, "out", "result.pdf", "output file path")
flag.StringVar(&dir, "dir", "", "directory wiith SVG files")
flag.Float64Var(&lineW, "linew", 0, "line width")
flag.Float64Var(&scale, "scale", ScaleFactor, "Scale")
flag.BoolVar(&timing, "time", false, "Print convert time")
}
func svg(format, orientation string, lineW float64, scale float64, files []string) {
var (
layerName string
)
pdf := gofpdf.New(orientation, "mm", format, "")
if lineW > 0 {
pdf.SetLineWidth(lineW)
}
pdf.AddPage()
pdf.SetFont("Helvetica", "", 1)
for _, file := range files {
_, fileName := filepath.Split(file)
if strs := strings.Split(fileName, "."); len(strs) > 0 {
layerName = strs[0]
} else {
layerName = fileName
}
layer := pdf.AddLayer(layerName, true)
pdf.BeginLayer(layer)
writeSvg(file, scale, pdf)
pdf.EndLayer()
}
//pdf.OpenLayerPane()
if err := pdf.OutputFileAndClose(out); err != nil {
fmt.Printf("Error generating PDF: %v\n", err)
}
}
func writeSvg(file string, scale float64, pdf *gofpdf.Fpdf) (err error) {
var (
sig gofpdf.SVGBasicType
)
t1 := time.Now()
if sig, err = gofpdf.SVGBasicFileParse(file); err != nil {
fmt.Println(err)
pdf.SetError(err)
return
}
t2 := time.Now()
pdf.SetLineCapStyle("round")
pdf.SetXY(0, 0)
pdf.SVGBasicWrite(&sig, scale)
t3 := time.Now()
pdf.SVGWriteTexts(&sig, scale)
t4 := time.Now()
if timing {
fmt.Printf("Parse: %v\nWrite geoms: %v\nWrite texts: %v\n", t2.Sub(t1), t3.Sub(t2), t4.Sub(t3))
}
return
}
//
// Returns list of paths to SVG files from directory
//
func readFilesFromDir(dir string) []string {
var files []string
filesInfo, _ := ioutil.ReadDir(dir)
for _, fi := range filesInfo {
if name := fi.Name(); strings.HasSuffix(name, ".svg") {
files = append(files, filepath.Join(dir, name))
}
}
return files
}
func main() {
var (
files []string
)
t := time.Now()
flag.Parse()
if flag.NArg() == 0 && len(dir) == 0 {
flag.PrintDefaults()
return
}
if files = flag.Args(); len(files) == 0 && len(dir) != 0 {
files = readFilesFromDir(dir)
}
orientation = strings.ToUpper(orientation)
format = strings.ToUpper(format)
svg(format, orientation, lineW, scale, files)
if timing {
fmt.Printf("Total time: %v\n", time.Now().Sub(t))
}
}