pdf.go

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