Skip to content

Commit

Permalink
Fix bug in alternated background
Browse files Browse the repository at this point in the history
* Expand header and footer behavior to simulate a margin top
* Add guard closure in header and footer to turn background white
  • Loading branch information
johnferchermeli committed Feb 21, 2020
1 parent 824b357 commit 76f3913
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 37 deletions.
Binary file modified internal/examples/pdfs/certificate.pdf
Binary file not shown.
Binary file modified internal/examples/pdfs/sample1.pdf
Binary file not shown.
Binary file modified internal/examples/pdfs/zpl.pdf
Binary file not shown.
4 changes: 1 addition & 3 deletions internal/examples/sample1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func main() {
begin := time.Now()
m := pdf.NewMaroto(consts.Portrait, consts.A4)
m.SetPageMargins(10, 15, 10)
//m.SetBorder(true)

byteSlices, err := ioutil.ReadFile("internal/assets/images/biplane.jpg")
Expand Down Expand Up @@ -204,9 +205,6 @@ func getSmallContent() ([]string, [][]string) {
contents = append(contents, []string{"São Paulo", "Rio de Janeiro", "", "R$ 31,00"})
contents = append(contents, []string{"São Carlos", "Petrópolis", "", "R$ 42,00"})
contents = append(contents, []string{"Florianópolis", "Osasco", "", "R$ 19,00"})
contents = append(contents, []string{"Osasco", "São Paulo", "", "R$ 7,00"})
contents = append(contents, []string{"Congonhas", "Fortaleza", "", "R$ 113,00"})
contents = append(contents, []string{"Natal", "Santo André", "", "R$ 198,00"})
contents = append(contents, []string{"Rio Grande do Norte", "Sorocaba", "", "R$ 42,00"})
contents = append(contents, []string{"Campinas", "Recife", "", "R$ 58,00"})
contents = append(contents, []string{"Florianópolis", "Osasco", "", "R$ 21,00"})
Expand Down
56 changes: 48 additions & 8 deletions pkg/pdf/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type PdfMaroto struct {
TableListHelper internal.TableList
pageIndex int
offsetY float64
marginTop float64
rowHeight float64
rowColCount float64
backgroundColor color.Color
Expand Down Expand Up @@ -126,6 +127,42 @@ func (s *PdfMaroto) RegisterHeader(closure func()) {
s.headerClosure = closure
}

func (s *PdfMaroto) footer() {
backgroundColor := s.backgroundColor
s.SetBackgroundColor(color.NewWhite())

_, pageHeight := s.Pdf.GetPageSize()
_, top, _, bottom := s.Pdf.GetMargins()

totalOffsetY := int(s.offsetY + s.footerHeight)
maxOffsetPage := int(pageHeight - bottom - top)

s.Row(float64(maxOffsetPage-totalOffsetY), func() {
s.ColSpace()
})

if s.footerClosure != nil {
s.footerClosure()
}

s.SetBackgroundColor(backgroundColor)
}

func (s *PdfMaroto) header() {
backgroundColor := s.backgroundColor
s.SetBackgroundColor(color.NewWhite())

s.Row(s.marginTop, func() {
s.ColSpace()
})

if s.headerClosure != nil {
s.headerClosure()
}

s.SetBackgroundColor(backgroundColor)
}

// RegisterFooter define a sequence of Rows, Lines ou TableLists
// which will be added in every new page
func (s *PdfMaroto) RegisterFooter(closure func()) {
Expand Down Expand Up @@ -153,7 +190,12 @@ func (s *PdfMaroto) GetCurrentOffset() float64 {
// SetPageMargins overrides default margins (10,10,10)
// the new page margin will affect all PDF pages
func (s *PdfMaroto) SetPageMargins(left, top, right float64) {
s.Pdf.SetMargins(left, top, right)
if top <= 10 {
s.Pdf.SetMargins(left, top, right)
} else {
s.marginTop = top - 10
s.Pdf.SetMargins(left, 10, right)
}
}

// GetPageMargins returns the set page margins. Comes in order of Left, Top, Right, Bottom
Expand Down Expand Up @@ -244,21 +286,19 @@ func (s *PdfMaroto) Row(height float64, closure func()) {
// height of the footer, add the footer
if totalOffsetY > maxOffsetPage {
if !s.headerFooterContextActive {
if s.footerClosure != nil {
s.headerFooterContextActive = true
s.footerClosure()
s.headerFooterContextActive = false
}
s.headerFooterContextActive = true
s.footer()
s.headerFooterContextActive = false
s.offsetY = 0
s.pageIndex++
}
}

// If is a new page, add the header
if !s.headerFooterContextActive && s.headerClosure != nil {
if !s.headerFooterContextActive {
if s.offsetY == 0 {
s.headerFooterContextActive = true
s.headerClosure()
s.header()
s.headerFooterContextActive = false
}
}
Expand Down
48 changes: 24 additions & 24 deletions pkg/pdf/pdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,9 +1033,9 @@ func TestPdfMaroto_Row(t *testing.T) {
assert.Equal(t, calledTimes, 1)
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "GetMargins", 1)
pdf.AssertNumberOfCalls(t, "GetPageSize", 1)
pdf.AssertNumberOfCalls(t, "Ln", 1)
pdf.AssertNumberOfCalls(t, "GetMargins", 2)
pdf.AssertNumberOfCalls(t, "GetPageSize", 2)
pdf.AssertNumberOfCalls(t, "Ln", 2)
pdf.AssertCalled(t, "Ln", 30.0)
},
},
Expand All @@ -1053,9 +1053,9 @@ func TestPdfMaroto_Row(t *testing.T) {
assert.Equal(t, calledTimes, 2)
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "GetMargins", 2)
pdf.AssertNumberOfCalls(t, "GetPageSize", 2)
pdf.AssertNumberOfCalls(t, "Ln", 2)
pdf.AssertNumberOfCalls(t, "GetMargins", 3)
pdf.AssertNumberOfCalls(t, "GetPageSize", 3)
pdf.AssertNumberOfCalls(t, "Ln", 3)

pdf.AssertCalled(t, "Ln", 30.0)
pdf.AssertCalled(t, "Ln", 40.0)
Expand All @@ -1078,9 +1078,9 @@ func TestPdfMaroto_Row(t *testing.T) {
assert.Equal(t, calledTimes, 3)
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "GetMargins", 3)
pdf.AssertNumberOfCalls(t, "GetPageSize", 3)
pdf.AssertNumberOfCalls(t, "Ln", 3)
pdf.AssertNumberOfCalls(t, "GetMargins", 4)
pdf.AssertNumberOfCalls(t, "GetPageSize", 4)
pdf.AssertNumberOfCalls(t, "Ln", 4)

pdf.AssertCalled(t, "Ln", 30.0)
pdf.AssertCalled(t, "Ln", 40.0)
Expand All @@ -1104,9 +1104,9 @@ func TestPdfMaroto_Row(t *testing.T) {
assert.Equal(t, calledTimes, 3)
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "GetMargins", 3)
pdf.AssertNumberOfCalls(t, "GetPageSize", 3)
pdf.AssertNumberOfCalls(t, "Ln", 3)
pdf.AssertNumberOfCalls(t, "GetMargins", 7)
pdf.AssertNumberOfCalls(t, "GetPageSize", 7)
pdf.AssertNumberOfCalls(t, "Ln", 6)

pdf.AssertCalled(t, "Ln", 50.0)
pdf.AssertCalled(t, "Ln", 40.0)
Expand Down Expand Up @@ -1148,8 +1148,8 @@ func TestPdfMaroto_Line(t *testing.T) {
return pdf
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "GetMargins", 2)
pdf.AssertNumberOfCalls(t, "GetPageSize", 2)
pdf.AssertNumberOfCalls(t, "GetMargins", 3)
pdf.AssertNumberOfCalls(t, "GetPageSize", 3)

pdf.AssertNumberOfCalls(t, "Line", 1)
pdf.AssertCalled(t, "Line", 10.0, 10.5, 90.0, 10.5)
Expand All @@ -1166,8 +1166,8 @@ func TestPdfMaroto_Line(t *testing.T) {
return pdf
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "GetMargins", 4)
pdf.AssertNumberOfCalls(t, "GetPageSize", 4)
pdf.AssertNumberOfCalls(t, "GetMargins", 5)
pdf.AssertNumberOfCalls(t, "GetPageSize", 5)

pdf.AssertNumberOfCalls(t, "Line", 2)
pdf.AssertCalled(t, "Line", 10.0, 11.0, 90.0, 11.0)
Expand Down Expand Up @@ -1210,7 +1210,7 @@ func TestPdfMaroto_ColSpace(t *testing.T) {
})
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "CellFormat", 1)
pdf.AssertNumberOfCalls(t, "CellFormat", 2)
pdf.AssertCalled(t, "CellFormat", 20, 40, "", "", 0, "C", false, 0, "")
},
},
Expand All @@ -1223,7 +1223,7 @@ func TestPdfMaroto_ColSpace(t *testing.T) {
})
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "CellFormat", 2)
pdf.AssertNumberOfCalls(t, "CellFormat", 3)
pdf.AssertCalled(t, "CellFormat", 20, 40, "", "", 0, "C", false, 0, "")
},
},
Expand All @@ -1238,7 +1238,7 @@ func TestPdfMaroto_ColSpace(t *testing.T) {
})
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "CellFormat", 2)
pdf.AssertNumberOfCalls(t, "CellFormat", 3)
pdf.AssertCalled(t, "CellFormat", 20, 40, "", "", 0, "C", false, 0, "")
pdf.AssertCalled(t, "CellFormat", 20, 35, "", "", 0, "C", false, 0, "")
},
Expand All @@ -1252,7 +1252,7 @@ func TestPdfMaroto_ColSpace(t *testing.T) {
})
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "CellFormat", 1)
pdf.AssertNumberOfCalls(t, "CellFormat", 2)
pdf.AssertCalled(t, "CellFormat", 20, 40, "", "1", 0, "C", false, 0, "")
},
},
Expand Down Expand Up @@ -1288,7 +1288,7 @@ func TestPdfMaroto_ColSpaces(t *testing.T) {
})
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "CellFormat", 2)
pdf.AssertNumberOfCalls(t, "CellFormat", 3)
pdf.AssertCalled(t, "CellFormat", 20, 40, "", "", 0, "C", false, 0, "")
},
},
Expand All @@ -1301,7 +1301,7 @@ func TestPdfMaroto_ColSpaces(t *testing.T) {
})
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "CellFormat", 4)
pdf.AssertNumberOfCalls(t, "CellFormat", 5)
pdf.AssertCalled(t, "CellFormat", 20, 40, "", "", 0, "C", false, 0, "")
},
},
Expand All @@ -1316,7 +1316,7 @@ func TestPdfMaroto_ColSpaces(t *testing.T) {
})
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "CellFormat", 4)
pdf.AssertNumberOfCalls(t, "CellFormat", 5)
pdf.AssertCalled(t, "CellFormat", 20, 40, "", "", 0, "C", false, 0, "")
pdf.AssertCalled(t, "CellFormat", 20, 35, "", "", 0, "C", false, 0, "")
},
Expand All @@ -1330,7 +1330,7 @@ func TestPdfMaroto_ColSpaces(t *testing.T) {
})
},
func(t *testing.T, pdf *mocks.Pdf) {
pdf.AssertNumberOfCalls(t, "CellFormat", 2)
pdf.AssertNumberOfCalls(t, "CellFormat", 3)
pdf.AssertCalled(t, "CellFormat", 20, 40, "", "1", 0, "C", false, 0, "")
},
},
Expand Down
2 changes: 0 additions & 2 deletions pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<!-- Please, do not create a Pull Request directly to master branch, create to dev always. -->

<!-- Please follow the PR naming pattern. -->
<!-- For features: feature/name -->
<!-- For fixes: fix/name -->
Expand Down

0 comments on commit 76f3913

Please sign in to comment.