-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmold.go
65 lines (56 loc) · 1.37 KB
/
mold.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
package mold
import (
"bytes"
"html/template"
wkhtmltopdf "github.com/SebastiaanKlippert/go-wkhtmltopdf"
)
//Mold model
type Mold struct {
HTMLTemplate string //html template string
HTMLPath string //html template file path
buff []byte
*wkhtmltopdf.PDFGenerator
}
//NewHTMLTemplate will create new return new html template mold
func NewHTMLTemplate(tmpl ...string) (m *Mold, err error) {
val := ""
if len(tmpl) > 0 {
val = tmpl[0]
}
m = &Mold{
HTMLTemplate: val,
}
m.PDFGenerator, err = wkhtmltopdf.NewPDFGenerator()
return
}
//Execute will execute the template
func (m *Mold) Execute(data interface{}) (err error) {
var buff bytes.Buffer
if m.HTMLTemplate == "" {
tmpl := template.Must(template.ParseFiles(m.HTMLPath))
err = tmpl.Execute(&buff, data)
} else {
tmpl := template.Must(template.New("").Parse(m.HTMLTemplate))
err = tmpl.Execute(&buff, data)
}
m.buff = buff.Bytes()
return
}
//String will return string from mold buffer
func (m *Mold) String() string {
return string(m.buff)
}
//Bytes will return bytes from mold buffer
func (m *Mold) Bytes() []byte {
return m.buff
}
//PDF will create pdf
func (m *Mold) PDF(path, name string) (err error) {
m.Dpi.Set(300)
// m.Grayscale.Set(true)
m.AddPage(wkhtmltopdf.NewPageReader(bytes.NewReader(m.Bytes())))
if err = m.Create(); err == nil {
err = m.WriteFile(path + "/" + name)
}
return
}