forked from maaslalani/invoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf.go
209 lines (182 loc) · 4.91 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
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"image"
"os"
"strconv"
"strings"
"github.com/signintech/gopdf"
)
const (
quantityColumnOffset = 360
rateColumnOffset = 405
amountColumnOffset = 480
)
const (
subtotalLabel = "Subtotal"
discountLabel = "Less: TDS at 10%"
taxLabel = "Tax"
totalLabel = "Total"
)
func writeLogo(pdf *gopdf.GoPdf, logo string, from string) {
if logo != "" {
width, height := getImageDimension(logo)
scaledWidth := 100.0
scaledHeight := float64(height) * scaledWidth / float64(width)
_ = pdf.Image(logo, pdf.GetX(), pdf.GetY(), &gopdf.Rect{W: scaledWidth, H: scaledHeight})
pdf.Br(scaledHeight + 24)
}
pdf.SetTextColor(55, 55, 55)
formattedFrom := strings.ReplaceAll(from, `\n`, "\n")
fromLines := strings.Split(formattedFrom, "\n")
for i := 0; i < len(fromLines); i++ {
if i == 0 {
_ = pdf.SetFont("Inter", "", 12)
_ = pdf.Cell(nil, fromLines[i])
pdf.Br(18)
} else {
_ = pdf.SetFont("Inter", "", 10)
_ = pdf.Cell(nil, fromLines[i])
pdf.Br(15)
}
}
pdf.Br(21)
pdf.SetStrokeColor(225, 225, 225)
pdf.Line(pdf.GetX(), pdf.GetY(), 260, pdf.GetY())
pdf.Br(36)
}
func writeTitle(pdf *gopdf.GoPdf, title, id, date string) {
_ = pdf.SetFont("Inter-Bold", "", 24)
pdf.SetTextColor(0, 0, 0)
_ = pdf.Cell(nil, title)
pdf.Br(36)
_ = pdf.SetFont("Inter", "", 12)
pdf.SetTextColor(100, 100, 100)
_ = pdf.Cell(nil, "#")
_ = pdf.Cell(nil, id)
pdf.SetTextColor(150, 150, 150)
_ = pdf.Cell(nil, " · ")
pdf.SetTextColor(100, 100, 100)
_ = pdf.Cell(nil, date)
pdf.Br(48)
}
func writeDueDate(pdf *gopdf.GoPdf, due string) {
_ = pdf.SetFont("Inter", "", 9)
pdf.SetTextColor(75, 75, 75)
pdf.SetX(rateColumnOffset)
_ = pdf.Cell(nil, "Due Date")
pdf.SetTextColor(0, 0, 0)
_ = pdf.SetFontSize(11)
pdf.SetX(amountColumnOffset - 15)
_ = pdf.Cell(nil, due)
pdf.Br(12)
}
func writeBillTo(pdf *gopdf.GoPdf, to string) {
pdf.SetTextColor(75, 75, 75)
_ = pdf.SetFont("Inter", "", 9)
_ = pdf.Cell(nil, "BILL TO")
pdf.Br(18)
pdf.SetTextColor(75, 75, 75)
formattedTo := strings.ReplaceAll(to, `\n`, "\n")
toLines := strings.Split(formattedTo, "\n")
for i := 0; i < len(toLines); i++ {
if i == 0 {
_ = pdf.SetFont("Inter", "", 15)
_ = pdf.Cell(nil, toLines[i])
pdf.Br(20)
} else {
_ = pdf.SetFont("Inter", "", 10)
_ = pdf.Cell(nil, toLines[i])
pdf.Br(15)
}
}
pdf.Br(64)
}
func writeHeaderRow(pdf *gopdf.GoPdf) {
_ = pdf.SetFont("Inter", "", 9)
pdf.SetTextColor(55, 55, 55)
_ = pdf.Cell(nil, "ITEM")
pdf.SetX(quantityColumnOffset)
_ = pdf.Cell(nil, "QTY")
pdf.SetX(rateColumnOffset)
_ = pdf.Cell(nil, "RATE")
pdf.SetX(amountColumnOffset)
_ = pdf.Cell(nil, "AMOUNT")
pdf.Br(24)
}
func writeNotes(pdf *gopdf.GoPdf, notes string) {
pdf.SetY(600)
_ = pdf.SetFont("Inter", "", 9)
pdf.SetTextColor(55, 55, 55)
_ = pdf.Cell(nil, "NOTES")
pdf.Br(18)
_ = pdf.SetFont("Inter", "", 9)
pdf.SetTextColor(0, 0, 0)
formattedNotes := strings.ReplaceAll(notes, `\n`, "\n")
notesLines := strings.Split(formattedNotes, "\n")
for i := 0; i < len(notesLines); i++ {
_ = pdf.Cell(nil, notesLines[i])
pdf.Br(15)
}
pdf.Br(48)
}
func writeFooter(pdf *gopdf.GoPdf, id string) {
pdf.SetY(800)
_ = pdf.SetFont("Inter", "", 10)
pdf.SetTextColor(55, 55, 55)
_ = pdf.Cell(nil, id)
pdf.SetStrokeColor(225, 225, 225)
pdf.Line(pdf.GetX()+10, pdf.GetY()+6, 550, pdf.GetY()+6)
pdf.Br(48)
}
func writeRow(pdf *gopdf.GoPdf, item string, quantity int, rate float64) {
_ = pdf.SetFont("Inter", "", 11)
pdf.SetTextColor(0, 0, 0)
total := float64(quantity) * rate
amount := strconv.FormatFloat(total, 'f', 2, 64)
_ = pdf.Cell(nil, item)
pdf.SetX(quantityColumnOffset)
_ = pdf.Cell(nil, strconv.Itoa(quantity))
pdf.SetX(rateColumnOffset)
_ = pdf.Cell(nil, currencySymbols[file.Currency]+strconv.FormatFloat(rate, 'f', 2, 64))
pdf.SetX(amountColumnOffset)
_ = pdf.Cell(nil, currencySymbols[file.Currency]+amount)
pdf.Br(24)
}
func writeTotals(pdf *gopdf.GoPdf, subtotal float64, tax float64, discount float64) {
pdf.SetY(600)
writeTotal(pdf, subtotalLabel, subtotal)
if tax > 0 {
writeTotal(pdf, taxLabel, tax)
}
if discount > 0 {
writeTotal(pdf, discountLabel, discount)
}
writeTotal(pdf, totalLabel, subtotal+tax-discount)
}
func writeTotal(pdf *gopdf.GoPdf, label string, total float64) {
_ = pdf.SetFont("Inter", "", 9)
pdf.SetTextColor(75, 75, 75)
pdf.SetX(rateColumnOffset)
_ = pdf.Cell(nil, label)
pdf.SetTextColor(0, 0, 0)
_ = pdf.SetFontSize(12)
pdf.SetX(amountColumnOffset - 15)
if label == totalLabel {
_ = pdf.SetFont("Inter-Bold", "", 11.5)
}
_ = pdf.Cell(nil, currencySymbols[file.Currency]+strconv.FormatFloat(total, 'f', 2, 64))
pdf.Br(24)
}
func getImageDimension(imagePath string) (int, int) {
file, err := os.Open(imagePath)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
defer file.Close()
image, _, err := image.DecodeConfig(file)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", imagePath, err)
}
return image.Width, image.Height
}