-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
62 lines (50 loc) · 1.36 KB
/
header.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
/*
Package invoice_generator
Copyright 2021 @chunvv. All Rights Reserved.
Created by Trung Vu on AD 2021/07/10.
*/
package invoice_generator
import (
"fmt"
"github.com/creasty/defaults"
"github.com/jung-kurt/gofpdf"
)
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()
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)
pdf.SetFont("deja", "", hf.FontSize)
_, lineHt := pdf.GetFontSize()
html := pdf.HTMLBasicNew()
html.Write(lineHt, hf.Text)
if !hf.Pagination {
pdf.AliasNbPages("")
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
}