forked from angelodlfrtr/go-invoice-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_footer.go
97 lines (77 loc) · 2.16 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
package generator
import (
"fmt"
"github.com/creasty/defaults"
"github.com/jung-kurt/gofpdf"
)
type HeaderFooter struct {
UseCustomFunc bool
Text string
FontSize float64 `default:"7"`
Pagination bool
}
type fnc func()
// ApplyFunc allow user to apply custom func
func (hf *HeaderFooter) ApplyFunc(pdf *gofpdf.Fpdf, fn fnc) {
pdf.SetHeaderFunc(fn)
}
func (hf *HeaderFooter) applyHeader(d *Document, pdf *gofpdf.Fpdf) error {
if err := defaults.Set(hf); err != nil {
return err
}
if !hf.UseCustomFunc {
pdf.SetHeaderFunc(func() {
currentY := pdf.GetY()
currentX := pdf.GetX()
pdf.SetTopMargin(HeaderMarginTop)
pdf.SetY(HeaderMarginTop)
pdf.SetLeftMargin(BaseMargin)
pdf.SetRightMargin(BaseMargin)
// Parse Text as html (simple)
pdf.SetFont("Helvetica", "", hf.FontSize)
_, lineHt := pdf.GetFontSize()
html := pdf.HTMLBasicNew()
html.Write(lineHt, hf.Text)
// Apply pagination
if !hf.Pagination {
pdf.AliasNbPages("") // Will replace {nb} with total page count
pdf.SetY(HeaderMarginTop + 8)
pdf.SetX(195)
pdf.CellFormat(10, 5, fmt.Sprintf("Page %d/{nb}", pdf.PageNo()), "0", 0, "R", false, 0, "")
}
pdf.SetY(currentY)
pdf.SetX(currentX)
pdf.SetMargins(BaseMargin, BaseMarginTop, BaseMargin)
})
}
return nil
}
func (hf *HeaderFooter) applyFooter(d *Document, pdf *gofpdf.Fpdf) error {
if err := defaults.Set(hf); err != nil {
return err
}
if !hf.UseCustomFunc {
pdf.SetFooterFunc(func() {
currentY := pdf.GetY()
currentX := pdf.GetX()
pdf.SetTopMargin(HeaderMarginTop)
pdf.SetY(287 - HeaderMarginTop)
// Parse Text as html (simple)
pdf.SetFont("Helvetica", "", hf.FontSize)
_, lineHt := pdf.GetFontSize()
html := pdf.HTMLBasicNew()
html.Write(lineHt, hf.Text)
// Apply pagination
if hf.Pagination {
pdf.AliasNbPages("") // Will replace {nb} with total page count
pdf.SetY(287 - HeaderMarginTop - 8)
pdf.SetX(195)
pdf.CellFormat(10, 5, fmt.Sprintf("Page %d/{nb}", pdf.PageNo()), "0", 0, "R", false, 0, "")
}
pdf.SetY(currentY)
pdf.SetX(currentX)
pdf.SetMargins(BaseMargin, BaseMarginTop, BaseMargin)
})
}
return nil
}