-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
100 lines (85 loc) · 2.37 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
package htmlPDF
import (
"fmt"
"github.com/jung-kurt/gofpdf"
"io/ioutil"
)
//global pointer to pdf
var pdf *gofpdf.Fpdf
func Generate(html string, css string, out string) {
xmlFile, err := ioutil.ReadFile(html)
if err != nil {
return
}
//parse html to Node tree
n := ParseHtml(string(xmlFile))
fmt.Println("\x1b[41m\x1b[1mprint Node\x1b[0m")
n.print(0)
fmt.Println("\x1b[41m\x1b[1mend print Node\x1b[0m\n")
cssFile, err := ioutil.ReadFile(css)
if err != nil {
panic(err)
return
}
cssStyle := string(cssFile)
p2 := CssParser(cssStyle)
stylesheet := p2.parseRules()
styletree := styleTree(n, &stylesheet)
fmt.Println("\x1b[41m\x1b[1mprint StyleTree\x1b[0m")
styletree.print(0)
fmt.Println("\x1b[41m\x1b[1mend print StyleTree\x1b[0m\n")
viewport := Dimensions{}
viewport.content.width = 210
viewport.content.height = 600
layoutTree := layoutTree(styletree, viewport)
fmt.Println("\n\x1b[41m\x1b[1mprint LayoutTree\x1b[0m")
layoutTree.print(0)
fmt.Println("\x1b[41m\x1b[1mend print LayoutTree\x1b[0m")
list := buildDisplayList(layoutTree)
fmt.Println(layoutTree)
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "", 16)
for i := 0; i < len(list); i++ {
list[i].draw(pdf)
}
err = pdf.OutputFileAndClose(out)
if err != nil {
fmt.Println("Error pdf", err)
}
pdf.Close()
}
func GenerateFromString(html string, css string, out string) {
//parse html to Node tree
n := ParseHtml(string(html))
fmt.Println("\x1b[41m\x1b[1mprint Node\x1b[0m")
n.print(0)
fmt.Println("\x1b[41m\x1b[1mend print Node\x1b[0m\n")
cssStyle := string(css)
p2 := CssParser(cssStyle)
stylesheet := p2.parseRules()
styletree := styleTree(n, &stylesheet)
fmt.Println("\x1b[41m\x1b[1mprint StyleTree\x1b[0m")
styletree.print(0)
fmt.Println("\x1b[41m\x1b[1mend print StyleTree\x1b[0m\n")
viewport := Dimensions{}
viewport.content.width = 210
viewport.content.height = 600
layoutTree := layoutTree(styletree, viewport)
fmt.Println("\n\x1b[41m\x1b[1mprint LayoutTree\x1b[0m")
layoutTree.print(0)
fmt.Println("\x1b[41m\x1b[1mend print LayoutTree\x1b[0m")
list := buildDisplayList(layoutTree)
fmt.Println(layoutTree)
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "", 16)
for i := 0; i < len(list); i++ {
list[i].draw(pdf)
}
err := pdf.OutputFileAndClose(out)
if err != nil {
fmt.Println("Error pdf", err)
}
pdf.Close()
}