Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in alternated background #138

Merged
merged 1 commit into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 204 additions & 0 deletions internal/examples/billing/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package main

import (
"fmt"
"github.com/johnfercher/maroto/pkg/color"
"github.com/johnfercher/maroto/pkg/consts"
"github.com/johnfercher/maroto/pkg/pdf"
"github.com/johnfercher/maroto/pkg/props"
"os"
"time"
)

func main() {
begin := time.Now()

darkGrayColor := color.Color{
Red: 144,
Green: 144,
Blue: 144,
}

grayColor := color.Color{
Red: 200,
Green: 200,
Blue: 200,
}

whiteColor := color.NewWhite()

m := pdf.NewMaroto(consts.Portrait, consts.A4)
m.SetPageMargins(10, 15, 10)
//m.SetBorder(true)

m.RegisterHeader(func() {
m.Row(20, func() {
m.Col(func() {
_ = m.FileImage("internal/assets/images/biplane.jpg", props.Rect{
Center: true,
Percent: 80,
})
})
m.ColSpaces(2)
m.Col(func() {
m.Text("AnyCompany Name Inc. 851 Any Street Name, Suite 120, Any City, CA 45123.", props.Text{
Top: 4,
Size: 8,
Align: consts.Right,
Extrapolate: false,
})
m.Text("Tel: 55 024 12345-1234", props.Text{
Top: 16,
Style: consts.BoldItalic,
Size: 8,
Align: consts.Right,
})
m.Text("www.mycompany.com", props.Text{
Top: 19,
Style: consts.BoldItalic,
Size: 8,
Align: consts.Right,
})
})
})
m.Row(5, func() {
m.ColSpace()
})
})

m.RegisterFooter(func() {
m.Row(20, func() {
m.Col(func() {
m.Text("Tel: 55 024 12345-1234", props.Text{
Top: 13,
Style: consts.BoldItalic,
Size: 8,
Align: consts.Left,
})
m.Text("www.mycompany.com", props.Text{
Top: 16,
Style: consts.BoldItalic,
Size: 8,
Align: consts.Left,
})
})
})
})

m.Row(10, func() {
m.Col(func() {
m.Text("Invoice ABC123456789", props.Text{
Top: 6,
Style: consts.Bold,
Align: consts.Center,
})
})
})

m.SetBackgroundColor(darkGrayColor)
m.Row(7, func() {
m.Col(func() {
m.Text("Transactions", props.Text{
Top: 4.5,
Size: 9,
Style: consts.Bold,
Align: consts.Center,
})
})
m.ColSpaces(3)
})
m.SetBackgroundColor(whiteColor)

header := []string{"", "Product", "Quantity", "Price"}
contents := [][]string{
{"", "Swamp", "12", "R$ 4,00"},
{"", "Sorin, A Planeswalker", "4", "R$ 90,00"},
{"", "Tassa", "4", "R$ 30,00"},
{"", "Skinrender", "4", "R$ 9,00"},
{"", "Island", "12", "R$ 4,00"},
{"", "Mountain", "12", "R$ 4,00"},
{"", "Plain", "12", "R$ 4,00"},
{"", "Black Lotus", "1", "R$ 1.000,00"},
{"", "Time Walk", "1", "R$ 1.000,00"},
{"", "Emberclave", "4", "R$ 44,00"},
{"", "Anax", "4", "R$ 32,00"},
{"", "Murderous Rider", "4", "R$ 22,00"},
{"", "Gray Merchant of Asphodel", "4", "R$ 2,00"},
{"", "Ajani's Pridemate", "4", "R$ 2,00"},
{"", "Renan, Chatuba", "4", "R$ 19,00"},
{"", "Tymarett", "4", "R$ 13,00"},
{"", "Doom Blade", "4", "R$ 5,00"},
{"", "Dark Lord", "3", "R$ 7,00"},
{"", "Memory of Thanatos", "3", "R$ 32,00"},
{"", "Poring", "4", "R$ 1,00"},
{"", "Deviling", "4", "R$ 99,00"},
{"", "Seiya", "4", "R$ 45,00"},
{"", "Harry Potter", "4", "R$ 62,00"},
{"", "Goku", "4", "R$ 77,00"},
{"", "Phreoni", "4", "R$ 22,00"},
{"", "Katheryn High Wizard", "4", "R$ 25,00"},
{"", "Lord Seyren", "4", "R$ 55,00"},
}

m.TableList(header, contents, props.TableList{
HeaderProp: props.Font{
Size: 9,
},
ContentProp: props.Font{
Size: 8,
},
Align: consts.Center,
AlternatedBackground: &grayColor,
HeaderContentSpace: 1,
Line: false,
})

m.Row(20, func() {
m.ColSpaces(2)
m.Col(func() {
m.Text("Total:", props.Text{
Top: 5,
Style: consts.Bold,
Size: 8,
Align: consts.Right,
})
})
m.Col(func() {
m.Text("R$ 2.567,00", props.Text{
Top: 5,
Style: consts.Bold,
Size: 8,
Align: consts.Center,
})
})
})

m.Row(15, func() {
m.Col(func() {
_ = m.Barcode("5123.151231.512314.1251251.123215", props.Barcode{
Percent: 0,
Proportion: props.Proportion{
Width: 20,
Height: 2,
},
})
m.Text("5123.151231.512314.1251251.123215", props.Text{
Top: 12,
Family: "",
Style: consts.Bold,
Size: 9,
Align: consts.Center,
})
})
m.ColSpace()
})

err := m.OutputFileAndClose("internal/examples/pdfs/billing.pdf")
if err != nil {
fmt.Println("Could not save PDF:", err)
os.Exit(1)
}

end := time.Now()
fmt.Println(end.Sub(begin))
}
Binary file added internal/examples/pdfs/billing.pdf
Binary file not shown.
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
Loading