/
testTemplate.go
54 lines (45 loc) · 1.1 KB
/
testTemplate.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"github.com/fatih/structs"
"github.com/hoisie/mustache"
)
var (
templateHtml *mustache.Template
tmpDir = "./"
)
type Data struct {
Name string
}
type PeakItem struct {
MoM string
PeakItem int
}
func main() {
templateHtml, err := mustache.ParseFile("./report.html")
if err != nil {
log.Fatal("Parse Html failed, ", err)
}
var tmpfile = tmpDir + "test_report.html"
tmpHtmlFile, err := os.Create(tmpfile)
if err != nil {
log.Fatal("Create tmp file failed, ", err)
}
var data Data
data.Name = "!!! XXXX !!!!"
mapping := structs.Map(data)
var str = templateHtml.Render(mapping)
filelength, err := tmpHtmlFile.WriteString(str)
fmt.Printf("wrote %d bytes\n", filelength)
tmpHtmlFile.Sync()
cmd := "wkhtmltopdf"
args := []string{"--print-media-type", "--page-size", "A4", "-T", "0mm", "-B", "0mm", "-L", "0mm", "-R", "0mm", "--dpi", "600", "test_report.html", "test_report.pdf"}
if err := exec.Command(cmd, args...).Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Successfully Generates the report of PDF format")
}