-
Notifications
You must be signed in to change notification settings - Fork 53
/
header_footer.go
120 lines (100 loc) · 2.67 KB
/
header_footer.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
package generator
import (
"fmt"
"github.com/creasty/defaults"
"github.com/go-pdf/fpdf"
)
// HeaderFooter define header or footer informations on document
type HeaderFooter struct {
UseCustomFunc bool `json:"-"`
Text string `json:"text,omitempty"`
FontSize float64 `json:"font_size,omitempty" default:"7"`
Pagination bool `json:"pagination,omitempty"`
}
type fnc func()
// ApplyFunc allow user to apply custom func
func (hf *HeaderFooter) ApplyFunc(pdf *fpdf.Fpdf, fn fnc) {
pdf.SetHeaderFunc(fn)
}
// applyHeader apply header to document
func (hf *HeaderFooter) applyHeader(doc *Document) error {
if err := defaults.Set(hf); err != nil {
return err
}
if !hf.UseCustomFunc {
doc.pdf.SetHeaderFunc(func() {
currentY := doc.pdf.GetY()
currentX := doc.pdf.GetX()
doc.pdf.SetTopMargin(HeaderMarginTop)
doc.pdf.SetY(HeaderMarginTop)
doc.pdf.SetLeftMargin(BaseMargin)
doc.pdf.SetRightMargin(BaseMargin)
// Parse Text as html (simple)
doc.pdf.SetFont(doc.Options.Font, "", hf.FontSize)
_, lineHt := doc.pdf.GetFontSize()
html := doc.pdf.HTMLBasicNew()
html.Write(lineHt, doc.encodeString(hf.Text))
// Apply pagination
if !hf.Pagination {
doc.pdf.AliasNbPages("") // Will replace {nb} with total page count
doc.pdf.SetY(HeaderMarginTop + 8)
doc.pdf.SetX(195)
doc.pdf.CellFormat(
10,
5,
doc.encodeString(fmt.Sprintf("Page %d/{nb}", doc.pdf.PageNo())),
"0",
0,
"R",
false,
0,
"",
)
}
doc.pdf.SetY(currentY)
doc.pdf.SetX(currentX)
doc.pdf.SetMargins(BaseMargin, BaseMarginTop, BaseMargin)
})
}
return nil
}
// applyFooter apply footer to document
func (hf *HeaderFooter) applyFooter(doc *Document) error {
if err := defaults.Set(hf); err != nil {
return err
}
if !hf.UseCustomFunc {
doc.pdf.SetFooterFunc(func() {
currentY := doc.pdf.GetY()
currentX := doc.pdf.GetX()
doc.pdf.SetTopMargin(HeaderMarginTop)
doc.pdf.SetY(287 - HeaderMarginTop)
// Parse Text as html (simple)
doc.pdf.SetFont(doc.Options.Font, "", hf.FontSize)
_, lineHt := doc.pdf.GetFontSize()
html := doc.pdf.HTMLBasicNew()
html.Write(lineHt, doc.encodeString(hf.Text))
// Apply pagination
if hf.Pagination {
doc.pdf.AliasNbPages("") // Will replace {nb} with total page count
doc.pdf.SetY(287 - HeaderMarginTop - 8)
doc.pdf.SetX(195)
doc.pdf.CellFormat(
10,
5,
doc.encodeString(fmt.Sprintf("Page %d/{nb}", doc.pdf.PageNo())),
"0",
0,
"R",
false,
0,
"",
)
}
doc.pdf.SetY(currentY)
doc.pdf.SetX(currentX)
doc.pdf.SetMargins(BaseMargin, BaseMarginTop, BaseMargin)
})
}
return nil
}