1package main
2
3import (
4 "strconv"
5
6 "github.com/signintech/gopdf"
7)
8
9const quantityColumnOffset = 360
10const rateColumnOffset = 405
11const amountColumnOffset = 480
12
13const (
14 subtotalLabel = "Subtotal"
15 discountLabel = "Discount"
16 taxLabel = "Tax"
17 totalLabel = "Total"
18)
19
20func writeLogo(pdf *gopdf.GoPdf, logo string, from string) {
21 if logo != "" {
22 _ = pdf.Image(logo, pdf.GetX(), pdf.GetY(), &gopdf.Rect{W: 44, H: 44})
23 pdf.Br(64)
24 }
25 _ = pdf.SetFont("Inter", "", 12)
26 pdf.SetTextColor(155, 155, 155)
27 _ = pdf.Cell(nil, from)
28 pdf.Br(36)
29 pdf.SetStrokeColor(225, 225, 225)
30 pdf.Line(pdf.GetX(), pdf.GetY(), 100, pdf.GetY())
31 pdf.Br(36)
32}
33
34func writeTitle(pdf *gopdf.GoPdf, title, id string) {
35 _ = pdf.SetFont("Inter", "", 24)
36 pdf.SetTextColor(0, 0, 0)
37 _ = pdf.Cell(nil, title)
38 pdf.Br(36)
39 _ = pdf.SetFont("Inter", "", 12)
40 pdf.SetTextColor(225, 225, 225)
41 _ = pdf.Cell(nil, "#")
42 pdf.SetTextColor(175, 175, 175)
43 _ = pdf.Cell(nil, id)
44 pdf.Br(48)
45}
46
47func writeBillTo(pdf *gopdf.GoPdf, to string) {
48 pdf.SetTextColor(200, 200, 200)
49 _ = pdf.SetFont("Inter", "", 9)
50 _ = pdf.Cell(nil, "BILL TO")
51 pdf.Br(14)
52 pdf.SetTextColor(175, 175, 175)
53 _ = pdf.SetFont("Inter", "", 15)
54 _ = pdf.Cell(nil, to)
55 pdf.Br(64)
56}
57
58func writeHeaderRow(pdf *gopdf.GoPdf) {
59 _ = pdf.SetFont("Inter", "", 9)
60 pdf.SetTextColor(175, 175, 175)
61 _ = pdf.Cell(nil, "ITEM")
62 pdf.SetX(quantityColumnOffset)
63 _ = pdf.Cell(nil, "QTY")
64 pdf.SetX(rateColumnOffset)
65 _ = pdf.Cell(nil, "RATE")
66 pdf.SetX(amountColumnOffset)
67 _ = pdf.Cell(nil, "AMOUNT")
68 pdf.Br(24)
69}
70
71func writeNotes(pdf *gopdf.GoPdf, notes string) {
72 pdf.SetY(650)
73
74 _ = pdf.SetFont("Inter", "", 10)
75 pdf.SetTextColor(155, 155, 155)
76 _ = pdf.Cell(nil, "Notes")
77 pdf.Br(18)
78 pdf.SetTextColor(0, 0, 0)
79 _ = pdf.Cell(nil, notes)
80 pdf.Br(48)
81}
82
83func writeFooter(pdf *gopdf.GoPdf, id string) {
84 pdf.SetY(800)
85
86 _ = pdf.SetFont("Inter", "", 10)
87 pdf.SetTextColor(155, 155, 155)
88 _ = pdf.Cell(nil, id)
89 pdf.SetStrokeColor(225, 225, 225)
90 pdf.Line(pdf.GetX()+10, pdf.GetY()+6, 550, pdf.GetY()+6)
91 pdf.Br(48)
92}
93
94func writeRow(pdf *gopdf.GoPdf, item string, quantity int, rate float64) {
95 _ = pdf.SetFont("Inter", "", 11)
96 pdf.SetTextColor(0, 0, 0)
97
98 total := float64(quantity) * rate
99 amount := strconv.FormatFloat(total, 'f', 2, 64)
100
101 _ = pdf.Cell(nil, item)
102 pdf.SetX(quantityColumnOffset)
103 _ = pdf.Cell(nil, strconv.Itoa(quantity))
104 pdf.SetX(rateColumnOffset)
105 _ = pdf.Cell(nil, currencySymbols[currency]+strconv.FormatFloat(rate, 'f', 2, 64))
106 pdf.SetX(amountColumnOffset)
107 _ = pdf.Cell(nil, currencySymbols[currency]+amount)
108 pdf.Br(24)
109}
110
111func writeTotals(pdf *gopdf.GoPdf, subtotal float64, tax float64, discount float64) {
112 pdf.SetY(650)
113
114 writeTotal(pdf, subtotalLabel, subtotal)
115 if tax > 0 {
116 writeTotal(pdf, taxLabel, tax)
117 }
118 if discount > 0 {
119 writeTotal(pdf, discountLabel, discount)
120 }
121 writeTotal(pdf, totalLabel, subtotal+tax-discount)
122}
123
124func writeTotal(pdf *gopdf.GoPdf, label string, total float64) {
125 _ = pdf.SetFont("Inter", "", 9)
126 pdf.SetTextColor(175, 175, 175)
127 pdf.SetX(rateColumnOffset)
128 _ = pdf.Cell(nil, label)
129 pdf.SetTextColor(0, 0, 0)
130 _ = pdf.SetFontSize(12)
131 pdf.SetX(amountColumnOffset - 15)
132 _ = pdf.Cell(nil, currencySymbols[currency]+strconv.FormatFloat(total, 'f', 2, 64))
133 pdf.Br(24)
134}