-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf.go
196 lines (165 loc) · 4.43 KB
/
pdf.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package pdflexgo
//go:generate go run generator/main.go
import (
"io"
"github.com/jung-kurt/gofpdf"
)
type defaultProps struct {
fontFamily string
fontStyle FontStyle
fontSize float64
fontColor rgba
}
type Pdf struct {
fpdf *gofpdf.Fpdf
pages []*PageElement
fontsLoaded []FontLoadInformation
defaultProps *defaultProps
header HeaderBuilder
footer FooterBuilder
}
func NewPdf() *Pdf {
pdf := &Pdf{
fpdf: gofpdf.New(string(DefaultOrientation), string(DefaultUnit), string(DefaultSize), ""),
defaultProps: &defaultProps{
fontStyle: DefaultFontStyle,
fontFamily: DefaultFontFamily,
fontSize: DefaultFontSize,
fontColor: DefaultFontColor,
},
}
pdf.fpdf.SetAutoPageBreak(false, 0)
return pdf
}
func (pdf *Pdf) Header(header HeaderBuilder) *Pdf {
pdf.header = header
return pdf
}
func (pdf *Pdf) Footer(footer FooterBuilder) *Pdf {
pdf.footer = footer
return pdf
}
func (pdf *Pdf) Pages(pages ...*PageElement) *Pdf {
pdf.pages = append(pdf.pages, pages...)
return pdf
}
func renderPageRecursive(page *PageElement, pdf *Pdf, pageNumber int, overflowedContinuation bool) int {
nextPageOverflowed := page.render(pdf, pageNumber, overflowedContinuation)
if nextPageOverflowed != nil {
// Recursive call with overflowed nodes and incremented page number
return renderPageRecursive(nextPageOverflowed, pdf, pageNumber+1, true)
}
return pageNumber + 1
}
func (pdf *Pdf) Render() *Pdf {
pageNumber := 1
for _, page := range pdf.pages {
pageNumber = renderPageRecursive(page, pdf, pageNumber, false)
}
return pdf
}
func (pdf *Pdf) OutputFileAndClose(filePath string) error {
return pdf.fpdf.OutputFileAndClose(filePath)
}
func (pdf *Pdf) Output(writer io.Writer) error {
return pdf.fpdf.Output(writer)
}
func (pdf *Pdf) AddFont(family string, style FontStyle, filePath string) *Pdf {
pdf.fontsLoaded = append(pdf.fontsLoaded, FontLoadInformation{
fontFamily: family,
style: style,
filePath: filePath,
})
_family, _style := getFontFamilyAndStyle(family, style)
pdf.fpdf.AddUTF8Font(_family, _style, filePath)
return pdf
}
func (pdf *Pdf) AddFontFromBytes(family string, style FontStyle, bytes []byte) *Pdf {
_family, _style := getFontFamilyAndStyle(family, style)
pdf.fpdf.AddUTF8FontFromBytes(_family, _style, bytes)
return pdf
}
func (pdf *Pdf) FontThin() *Pdf {
pdf.defaultProps.fontStyle = FontStyleThin
return pdf
}
func (pdf *Pdf) FontExtraLight() *Pdf {
pdf.defaultProps.fontStyle = FontStyleExtraLight
return pdf
}
func (pdf *Pdf) FontLight() *Pdf {
pdf.defaultProps.fontStyle = FontStyleLight
return pdf
}
func (pdf *Pdf) FontRegular() *Pdf {
pdf.defaultProps.fontStyle = FontStyleRegular
return pdf
}
func (pdf *Pdf) FontMedium() *Pdf {
pdf.defaultProps.fontStyle = FontStyleMedium
return pdf
}
func (pdf *Pdf) FontSemiBold() *Pdf {
pdf.defaultProps.fontStyle = FontStyleSemiBold
return pdf
}
func (pdf *Pdf) FontBold() *Pdf {
pdf.defaultProps.fontStyle = FontStyleBold
return pdf
}
func (pdf *Pdf) FontExtraBold() *Pdf {
pdf.defaultProps.fontStyle = FontStyleExtraBold
return pdf
}
func (pdf *Pdf) FontBlack() *Pdf {
pdf.defaultProps.fontStyle = FontStyleBlack
return pdf
}
func (pdf *Pdf) FontThinItalic() *Pdf {
pdf.defaultProps.fontStyle = FontStyleThinItalic
return pdf
}
func (pdf *Pdf) FontExtraLightItalic() *Pdf {
pdf.defaultProps.fontStyle = FontStyleExtraLightItalic
return pdf
}
func (pdf *Pdf) FontLightItalic() *Pdf {
pdf.defaultProps.fontStyle = FontStyleLightItalic
return pdf
}
func (pdf *Pdf) FontRegularItalic() *Pdf {
pdf.defaultProps.fontStyle = FontStyleRegularItalic
return pdf
}
func (pdf *Pdf) FontMediumItalic() *Pdf {
pdf.defaultProps.fontStyle = FontStyleMediumItalic
return pdf
}
func (pdf *Pdf) FontSemiBoldItalic() *Pdf {
pdf.defaultProps.fontStyle = FontStyleSemiBoldItalic
return pdf
}
func (pdf *Pdf) FontBoldItalic() *Pdf {
pdf.defaultProps.fontStyle = FontStyleBoldItalic
return pdf
}
func (pdf *Pdf) FontExtraBoldItalic() *Pdf {
pdf.defaultProps.fontStyle = FontStyleExtraBoldItalic
return pdf
}
func (pdf *Pdf) FontBlackItalic() *Pdf {
pdf.defaultProps.fontStyle = FontStyleBlackItalic
return pdf
}
func (pdf *Pdf) FontFamily(family string) *Pdf {
pdf.defaultProps.fontFamily = family
return pdf
}
func (pdf *Pdf) FontSize(size float64) *Pdf {
pdf.defaultProps.fontSize = size
return pdf
}
func (pdf *Pdf) FontColor(red int, green int, blue int, alpha ...float64) *Pdf {
pdf.defaultProps.fontColor = getRgba(red, green, blue, alpha...)
return pdf
}