forked from angelodlfrtr/go-invoice-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact.go
91 lines (71 loc) · 2.01 KB
/
contact.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
package generator
import (
"bytes"
b64 "encoding/base64"
"image"
"github.com/jung-kurt/gofpdf"
)
type Contact struct {
Name string `validate:"required,min=1,max=64"`
Logo *[]byte // Logo byte array
Address *Address
}
func (c *Contact) appendContactTODoc(x float64, y float64, fill bool, logoAlign string, pdf *gofpdf.Fpdf) float64 {
pdf.SetXY(x, y)
// Logo
if c.Logo != nil {
// Create filename
fileName := b64.StdEncoding.EncodeToString([]byte(c.Name))
// Create reader from logo bytes
ioReader := bytes.NewReader(*c.Logo)
// Get image format
_, format, _ := image.DecodeConfig(bytes.NewReader(*c.Logo))
// Register image in pdf
imageInfo := pdf.RegisterImageOptionsReader(fileName, gofpdf.ImageOptions{
ImageType: format,
}, ioReader)
if imageInfo != nil {
var imageOpt gofpdf.ImageOptions
imageOpt.ImageType = format
pdf.ImageOptions(fileName, pdf.GetX(), y, 0, 30, false, imageOpt, 0, "")
pdf.SetY(y + 30)
}
}
// Name
if fill {
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
} else {
pdf.SetFillColor(255, 255, 255)
}
// Reset x
pdf.SetX(x)
// Name rect
pdf.Rect(x, pdf.GetY(), 70, 8, "F")
// Set name
pdf.SetFont("Helvetica", "B", 10)
pdf.Cell(40, 8, c.Name)
pdf.SetFont("Helvetica", "", 10)
if c.Address != nil {
// Address rect
var addrRectHeight float64 = 17
if len(c.Address.Address2) > 0 {
addrRectHeight = addrRectHeight + 5
}
if len(c.Address.Country) == 0 {
addrRectHeight = addrRectHeight - 5
}
pdf.Rect(x, pdf.GetY()+9, 70, addrRectHeight, "F")
// Set address
pdf.SetFont("Helvetica", "", 10)
pdf.SetXY(x, pdf.GetY()+10)
pdf.MultiCell(70, 5, c.Address.ToString(), "0", "L", false)
}
return pdf.GetY()
}
func (c *Contact) appendCompanyContactToDoc(pdf *gofpdf.Fpdf) float64 {
x, y, _, _ := pdf.GetMargins()
return c.appendContactTODoc(x, y, true, "L", pdf)
}
func (c *Contact) appendCustomerContactToDoc(pdf *gofpdf.Fpdf) float64 {
return c.appendContactTODoc(130, BaseMarginTop+25, true, "R", pdf)
}