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

Add width parameter in col #139

Merged
merged 13 commits into from
Mar 28, 2020
178 changes: 126 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,99 +66,173 @@ with the code to generate the PDFs.

#### Code
```go
// Billing example
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() {
m := pdf.NewMaroto(consts.Portrait, consts.Letter)

m.Row(40, func() {
m.Col(func() {
_ = m.FileImage("internal/assets/images/biplane.jpg", props.Rect{
Center: true,
Percent: 80,
})
})
m.Col(func() {
m.Text("Gopher International Shipping, Inc.", props.Text{
Top: 15,
Size: 20,
Extrapolate: true,
begin := time.Now()

darkGrayColor := getDarkGrayColor()
grayColor := getGrayColor()
whiteColor := color.NewWhite()
header := getHeader()
contents := getContents()

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

m.RegisterHeader(func() {
m.Row(20, func() {
m.Col(3, func() {
_ = m.FileImage("internal/assets/images/biplane.jpg", props.Rect{
Center: true,
Percent: 80,
})
})
m.Text("1000 Shipping Gopher Golang TN 3691234 GopherLand (GL)", props.Text{
Size: 12,
Top: 21,

m.ColSpace(6)

m.Col(3, func() {
m.Text("AnyCompany Name Inc. 851 Any Street Name, Suite 120, Any City, CA 45123.", props.Text{
Size: 8,
Align: consts.Right,
Extrapolate: false,
})
m.Text("Tel: 55 024 12345-1234", props.Text{
Top: 12,
Style: consts.BoldItalic,
Size: 8,
Align: consts.Right,
})
m.Text("www.mycompany.com", props.Text{
Top: 15,
Style: consts.BoldItalic,
Size: 8,
Align: consts.Right,
})
})
})
m.ColSpace()
})

m.Line(10)

m.Row(40, func() {
m.Col(func() {
m.Text("João Sant'Ana 100 Main Street Stringfield TN 39021 United Stats (USA)", props.Text{
Size: 15,
Top: 14,
m.RegisterFooter(func() {
m.Row(20, func() {
m.Col(12, 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.ColSpace()
m.Col(func() {
m.QrCode("https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 75,
})

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

m.Line(10)
m.SetBackgroundColor(darkGrayColor)

m.Row(100, func() {
m.Col(func() {
_ = m.Barcode("https://github.com/johnfercher/maroto", props.Barcode{
Center: true,
Percent: 70,
})
m.Text("https://github.com/johnfercher/maroto", props.Text{
Size: 20,
m.Row(7, func() {
m.Col(3, func() {
m.Text("Transactions", props.Text{
Top: 1.5,
Size: 9,
Style: consts.Bold,
Align: consts.Center,
Top: 80,
})
})
m.ColSpace(9)
})

m.SetBorder(true)
m.SetBackgroundColor(whiteColor)

m.TableList(header, contents, props.TableList{
HeaderProp: props.TableListContent{
Size: 9,
GridSizes: []uint{3, 4, 2, 3},
},
ContentProp: props.TableListContent{
Size: 8,
GridSizes: []uint{3, 4, 2, 3},
},
Align: consts.Center,
AlternatedBackground: &grayColor,
HeaderContentSpace: 1,
Line: false,
})

m.Row(40, func() {
m.Col(func() {
m.Text("CODE: 123412351645231245564 DATE: 20-07-1994 20:20:33", props.Text{
Size: 15,
Top: 19,
m.Row(20, func() {
m.ColSpace(7)
m.Col(2, func() {
m.Text("Total:", props.Text{
Top: 5,
Style: consts.Bold,
Size: 8,
Align: consts.Right,
})
})
m.Col(func() {
m.Text("CA", props.Text{
Top: 30,
Size: 85,
m.Col(3, func() {
m.Text("R$ 2.567,00", props.Text{
Top: 5,
Style: consts.Bold,
Size: 8,
Align: consts.Center,
})
})
})

m.SetBorder(false)
m.Row(15, func() {
m.Col(6, 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(6)
})

err := m.OutputFileAndClose("internal/examples/pdfs/zpl.pdf")
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))
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The following Go Code generates a simple PDF file.
m := pdf.NewMaroto(consts.Portrait, consts.Letter)

m.Row(10, func() {
m.Col(func() {
m.Col(12, func() {
m.Text(props.Text{
Size: 18,
Style: consts.Bold,
Expand Down
9 changes: 9 additions & 0 deletions internal/cell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package internal

// Cell represents a cell inside the PDF
type Cell struct {
X float64
Y float64
Width float64
Height float64
}
23 changes: 10 additions & 13 deletions internal/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

// Code is the abstraction which deals of how to add QrCodes or Barcode in a PDF
type Code interface {
AddQr(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Rect)
AddBar(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Barcode) (err error)
AddQr(code string, cell Cell, prop props.Rect)
AddBar(code string, cell Cell, prop props.Barcode) (err error)
}

type code struct {
Expand All @@ -28,39 +28,36 @@ func NewCode(pdf gofpdf.Pdf, math Math) *code {
}

// AddQr create a QrCode inside a cell
func (s *code) AddQr(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Rect) {
func (s *code) AddQr(code string, cell Cell, prop props.Rect) {
key := barcode.RegisterQR(s.pdf, code, qr.H, qr.Unicode)

actualWidthPerCol := s.math.GetWidthPerCol(qtdCols)

var x, y, w, h float64
if prop.Center {
x, y, w, h = s.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol, qtdCols, colHeight, indexCol, prop.Percent)
x, y, w, h = s.math.GetRectCenterColProperties(cell.Width, cell.Width, cell.Width, cell.Height, cell.X, prop.Percent)
} else {
x, y, w, h = s.math.GetRectNonCenterColProperties(actualWidthPerCol, actualWidthPerCol, qtdCols, colHeight, indexCol, prop)
x, y, w, h = s.math.GetRectNonCenterColProperties(cell.Width, cell.Width, cell.Width, cell.Height, cell.X, prop)
}

barcode.Barcode(s.pdf, key, x, y+marginTop, w, h, false)
barcode.Barcode(s.pdf, key, x, y+cell.Y, w, h, false)
}

// AddBar create a Barcode inside a cell
func (s *code) AddBar(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, prop props.Barcode) (err error) {
func (s *code) AddBar(code string, cell Cell, prop props.Barcode) (err error) {
bcode, err := code128.Encode(code)

if err != nil {
return
}

actualWidthPerCol := s.math.GetWidthPerCol(qtdCols)
heightPercentFromWidth := prop.Proportion.Height / prop.Proportion.Width
var x, y, w, h float64
if prop.Center {
x, y, w, h = s.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol*heightPercentFromWidth, qtdCols, colHeight, indexCol, prop.Percent)
x, y, w, h = s.math.GetRectCenterColProperties(cell.Width, cell.Width*heightPercentFromWidth, cell.Width, cell.Height, cell.X, prop.Percent)
} else {
rectProps := props.Rect{Left: prop.Left, Top: prop.Top, Center: prop.Center, Percent: prop.Percent}
x, y, w, h = s.math.GetRectNonCenterColProperties(actualWidthPerCol, actualWidthPerCol*heightPercentFromWidth, qtdCols, colHeight, indexCol, rectProps)
x, y, w, h = s.math.GetRectNonCenterColProperties(cell.Width, cell.Width*heightPercentFromWidth, cell.Width, cell.Height, cell.X, rectProps)
}

barcode.Barcode(s.pdf, barcode.Register(bcode), x, y+marginTop, w, h, false)
barcode.Barcode(s.pdf, barcode.Register(bcode), x, y+cell.Y, w, h, false)
return
}
Loading