Skip to content

Commit

Permalink
Allow to customize colors via options
Browse files Browse the repository at this point in the history
  • Loading branch information
angelodlfrtr committed Feb 10, 2022
1 parent 0f597a9 commit 5b02664
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 296 deletions.
390 changes: 211 additions & 179 deletions build.go

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,4 @@ var (

// LargeTextFontSize define the large font size for text in document
LargeTextFontSize float64 = 10

// BaseTextColor define the base color used for text in document
BaseTextColor = []int{35, 35, 35}

// GreyTextColor define the base color used for text in document
GreyTextColor = []int{82, 82, 82}

// GreyBgColor define the grey background color used for text in document
GreyBgColor = []int{232, 232, 232}

// DarkBgColor define the grey background color used for text in document
DarkBgColor = []int{212, 212, 212}
)
54 changes: 32 additions & 22 deletions contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ type Contact struct {
Address *Address `json:"address,omitempty"`
}

func (c *Contact) appendContactTODoc(x float64, y float64, fill bool, logoAlign string, pdf *gofpdf.Fpdf) float64 {
pdf.SetXY(x, y)
func (c *Contact) appendContactTODoc(
x float64,
y float64,
fill bool,
logoAlign string,
doc *Document,
) float64 {
doc.pdf.SetXY(x, y)

// Logo
if c.Logo != nil {
Expand All @@ -27,37 +33,41 @@ func (c *Contact) appendContactTODoc(x float64, y float64, fill bool, logoAlign
// Get image format
_, format, _ := image.DecodeConfig(bytes.NewReader(*c.Logo))
// Register image in pdf
imageInfo := pdf.RegisterImageOptionsReader(fileName, gofpdf.ImageOptions{
imageInfo := doc.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, "")
doc.pdf.ImageOptions(fileName, doc.pdf.GetX(), y, 0, 30, false, imageOpt, 0, "")

pdf.SetY(y + 30)
doc.pdf.SetY(y + 30)
}
}

// Name
if fill {
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
doc.pdf.SetFillColor(
doc.Options.GreyBgColor[0],
doc.Options.GreyBgColor[1],
doc.Options.GreyBgColor[2],
)
} else {
pdf.SetFillColor(255, 255, 255)
doc.pdf.SetFillColor(255, 255, 255)
}

// Reset x
pdf.SetX(x)
doc.pdf.SetX(x)

// Name rect
pdf.Rect(x, pdf.GetY(), 70, 8, "F")
doc.pdf.Rect(x, doc.pdf.GetY(), 70, 8, "F")

// Set name
pdf.SetFont("Helvetica", "B", 10)
pdf.Cell(40, 8, c.Name)
pdf.SetFont("Helvetica", "", 10)
doc.pdf.SetFont("Helvetica", "B", 10)
doc.pdf.Cell(40, 8, c.Name)
doc.pdf.SetFont("Helvetica", "", 10)

if c.Address != nil {
// Address rect
Expand All @@ -71,22 +81,22 @@ func (c *Contact) appendContactTODoc(x float64, y float64, fill bool, logoAlign
addrRectHeight = addrRectHeight - 5
}

pdf.Rect(x, pdf.GetY()+9, 70, addrRectHeight, "F")
doc.pdf.Rect(x, doc.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)
doc.pdf.SetFont("Helvetica", "", 10)
doc.pdf.SetXY(x, doc.pdf.GetY()+10)
doc.pdf.MultiCell(70, 5, c.Address.ToString(), "0", "L", false)
}

return pdf.GetY()
return doc.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) appendCompanyContactToDoc(doc *Document) float64 {
x, y, _, _ := doc.pdf.GetMargins()
return c.appendContactTODoc(x, y, true, "L", doc)
}

func (c *Contact) appendCustomerContactToDoc(pdf *gofpdf.Fpdf) float64 {
return c.appendContactTODoc(130, BaseMarginTop+25, true, "R", pdf)
func (c *Contact) appendCustomerContactToDoc(doc *Document) float64 {
return c.appendContactTODoc(130, BaseMarginTop+25, true, "R", doc)
}
4 changes: 1 addition & 3 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (

// New return a new documents with provided types and defaults
func New(docType string, options *Options) (*Document, error) {
if err := defaults.Set(options); err != nil {
return nil, err
}
_ = defaults.Set(options)

doc := &Document{
Options: options,
Expand Down
4 changes: 4 additions & 0 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ func TestNew(t *testing.T) {
TextTypeInvoice: "FACTURE",
TextRefTitle: "Réàf.",
AutoPrint: true,
BaseTextColor: []int{6, 63, 156},
GreyTextColor: []int{161, 96, 149},
GreyBgColor: []int{171, 240, 129},
DarkBgColor: []int{176, 12, 20},
})

doc.SetHeader(&HeaderFooter{
Expand Down
68 changes: 34 additions & 34 deletions header_footer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,75 +22,75 @@ func (hf *HeaderFooter) ApplyFunc(pdf *gofpdf.Fpdf, fn fnc) {
pdf.SetHeaderFunc(fn)
}

func (hf *HeaderFooter) applyHeader(d *Document, pdf *gofpdf.Fpdf) error {
func (hf *HeaderFooter) applyHeader(doc *Document) error {
if err := defaults.Set(hf); err != nil {
return err
}

if !hf.UseCustomFunc {
pdf.SetHeaderFunc(func() {
currentY := pdf.GetY()
currentX := pdf.GetX()
doc.pdf.SetHeaderFunc(func() {
currentY := doc.pdf.GetY()
currentX := doc.pdf.GetX()

pdf.SetTopMargin(HeaderMarginTop)
pdf.SetY(HeaderMarginTop)
doc.pdf.SetTopMargin(HeaderMarginTop)
doc.pdf.SetY(HeaderMarginTop)

pdf.SetLeftMargin(BaseMargin)
pdf.SetRightMargin(BaseMargin)
doc.pdf.SetLeftMargin(BaseMargin)
doc.pdf.SetRightMargin(BaseMargin)

// Parse Text as html (simple)
pdf.SetFont("Helvetica", "", hf.FontSize)
_, lineHt := pdf.GetFontSize()
html := pdf.HTMLBasicNew()
doc.pdf.SetFont("Helvetica", "", hf.FontSize)
_, lineHt := doc.pdf.GetFontSize()
html := doc.pdf.HTMLBasicNew()
html.Write(lineHt, hf.Text)

// Apply pagination
if !hf.Pagination {
pdf.AliasNbPages("") // Will replace {nb} with total page count
pdf.SetY(HeaderMarginTop + 8)
pdf.SetX(195)
pdf.CellFormat(10, 5, fmt.Sprintf("Page %d/{nb}", pdf.PageNo()), "0", 0, "R", false, 0, "")
doc.pdf.AliasNbPages("") // Will replace {nb} with total page count
doc.pdf.SetY(HeaderMarginTop + 8)
doc.pdf.SetX(195)
doc.pdf.CellFormat(10, 5, fmt.Sprintf("Page %d/{nb}", doc.pdf.PageNo()), "0", 0, "R", false, 0, "")
}

pdf.SetY(currentY)
pdf.SetX(currentX)
pdf.SetMargins(BaseMargin, BaseMarginTop, BaseMargin)
doc.pdf.SetY(currentY)
doc.pdf.SetX(currentX)
doc.pdf.SetMargins(BaseMargin, BaseMarginTop, BaseMargin)
})
}

return nil
}

func (hf *HeaderFooter) applyFooter(d *Document, pdf *gofpdf.Fpdf) error {
func (hf *HeaderFooter) applyFooter(doc *Document) error {
if err := defaults.Set(hf); err != nil {
return err
}

if !hf.UseCustomFunc {
pdf.SetFooterFunc(func() {
currentY := pdf.GetY()
currentX := pdf.GetX()
doc.pdf.SetFooterFunc(func() {
currentY := doc.pdf.GetY()
currentX := doc.pdf.GetX()

pdf.SetTopMargin(HeaderMarginTop)
pdf.SetY(287 - HeaderMarginTop)
doc.pdf.SetTopMargin(HeaderMarginTop)
doc.pdf.SetY(287 - HeaderMarginTop)

// Parse Text as html (simple)
pdf.SetFont("Helvetica", "", hf.FontSize)
_, lineHt := pdf.GetFontSize()
html := pdf.HTMLBasicNew()
doc.pdf.SetFont("Helvetica", "", hf.FontSize)
_, lineHt := doc.pdf.GetFontSize()
html := doc.pdf.HTMLBasicNew()
html.Write(lineHt, hf.Text)

// Apply pagination
if hf.Pagination {
pdf.AliasNbPages("") // Will replace {nb} with total page count
pdf.SetY(287 - HeaderMarginTop - 8)
pdf.SetX(195)
pdf.CellFormat(10, 5, fmt.Sprintf("Page %d/{nb}", pdf.PageNo()), "0", 0, "R", false, 0, "")
doc.pdf.AliasNbPages("") // Will replace {nb} with total page count
doc.pdf.SetY(287 - HeaderMarginTop - 8)
doc.pdf.SetX(195)
doc.pdf.CellFormat(10, 5, fmt.Sprintf("Page %d/{nb}", doc.pdf.PageNo()), "0", 0, "R", false, 0, "")
}

pdf.SetY(currentY)
pdf.SetX(currentX)
pdf.SetMargins(BaseMargin, BaseMarginTop, BaseMargin)
doc.pdf.SetY(currentY)
doc.pdf.SetX(currentX)
doc.pdf.SetMargins(BaseMargin, BaseMarginTop, BaseMargin)
})
}

Expand Down
Loading

0 comments on commit 5b02664

Please sign in to comment.