Skip to content

Commit

Permalink
Merge pull request #30 from johnfercher/dev
Browse files Browse the repository at this point in the history
feature/remove-label-add-line-config
  • Loading branch information
johnfercher authored Jun 16, 2019
2 parents e27d4cc + 03dca90 commit b2352e0
Show file tree
Hide file tree
Showing 13 changed files with 714 additions and 430 deletions.
49 changes: 27 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Maroto [![Travis](https://img.shields.io/badge/coverage-79.4%25-brightgreen.svg)][travis]
# Maroto [![Travis](https://img.shields.io/badge/coverage-84.4%25-brightgreen.svg)][travis]
A Maroto way to create PDFs. Maroto is inspired in Bootstrap and uses [Gofpdf](https://github.com/jung-kurt/gofpdf). Fast and simple.

> Maroto definition: Brazilian expression, means an astute/clever/intelligent person.
Expand All @@ -14,29 +14,31 @@ Here is the [pdf](assets/pdf/maroto.pdf) generated.
```go
func main() {
m := maroto.NewMaroto(maroto.Portrait, maroto.A4)
//m.SetDebugMode(true)

byteSlices, _ := ioutil.ReadFile("assets/images/gopher2.png")

base64 := base64.StdEncoding.EncodeToString(byteSlices)

header, contents := getContents()

m.Row("Codes", 20, func() {
m.Col("Logo", func() {
// Header 1
m.Row(20, func() {
m.Col(func() {
m.Base64Image(base64, maroto.Png, &maroto.RectProp{
Percent: 85,
})
})

m.ColSpaces(2)

m.Col("Link", func() {
m.Col(func() {
m.QrCode("https://github.com/johnfercher/maroto", &maroto.RectProp{
Percent: 75,
})
})

m.Col("Barcode", func() {
m.Col(func() {
id := "123456789"
_ = m.Barcode(id, &maroto.RectProp{
Percent: 70,
Expand All @@ -49,39 +51,41 @@ func main() {
})
})

m.Line()
m.Line(1.0)

m.Row("Logo", 12, func() {
m.Col("Logo", func() {
// Header 2
m.Row(12, func() {
m.Col(func() {
m.FileImage("assets/images/gopher1.jpg", nil)
})

m.ColSpace()

m.Col("Definition", func() {
m.Col(func() {
m.Text("PDFGenerator: Maroto", &maroto.TextProp{
Top: 4,
})
m.Text("Type: Easy & Fast", &maroto.TextProp{
Top: 9.5,
Top: 10,
})
})

m.ColSpace()

m.Col("Speed", func() {
m.Col(func() {
m.Text("GPL3", &maroto.TextProp{
Size: 16,
Style: maroto.Bold,
Top: 7.5,
Top: 8,
})
})
})

m.Line()
m.Line(1.0)

m.Row("SubTitle", 22, func() {
m.Col("Packages", func() {
// Header 3
m.Row(22, func() {
m.Col(func() {
m.Text("Grid System", &maroto.TextProp{
Size: 18,
Style: maroto.Bold,
Expand All @@ -91,25 +95,26 @@ func main() {
m.Text("Bootstrap Like", &maroto.TextProp{
Size: 12,
Align: maroto.Center,
Top: 16,
Top: 17,
})
})
})

m.Line()
m.Line(1.0)

m.RowTableList("List", header, contents, nil)
m.TableList(header, contents, nil)

m.Row("Signature", 30, func() {
m.Col("Nick", func() {
// Signatures
m.Row(30, func() {
m.Col(func() {
m.Signature("Nick Fury", nil)
})

m.Col("Thanos", func() {
m.Col(func() {
m.Signature("Thanos", nil)
})

m.Col("Collector", func() {
m.Col(func() {
m.Signature("Collector", nil)
})
})
Expand Down
18 changes: 9 additions & 9 deletions code.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ func NewCode(pdf gofpdf.Pdf, math Math) Code {
}
}

func (c *code) AddQr(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) {
key := barcode.RegisterQR(c.pdf, code, qr.H, qr.Unicode)
func (self *code) AddQr(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) {
key := barcode.RegisterQR(self.pdf, code, qr.H, qr.Unicode)

actualWidthPerCol := c.math.GetWidthPerCol(qtdCols)
actualWidthPerCol := self.math.GetWidthPerCol(qtdCols)

x, y, w, h := c.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol, qtdCols, colHeight, indexCol, percent)
x, y, w, h := self.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol, qtdCols, colHeight, indexCol, percent)

barcode.Barcode(c.pdf, key, x, y+marginTop, w, h, false)
barcode.Barcode(self.pdf, key, x, y+marginTop, w, h, false)
}

func (c *code) AddBar(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) (err error) {
func (self *code) AddBar(code string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) (err error) {
bcode, err := code128.Encode(code)

if err != nil {
return
}

actualWidthPerCol := c.math.GetWidthPerCol(qtdCols)
actualWidthPerCol := self.math.GetWidthPerCol(qtdCols)

x, y, w, h := c.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol*0.33, qtdCols, colHeight, indexCol, percent)
x, y, w, h := self.math.GetRectCenterColProperties(actualWidthPerCol, actualWidthPerCol*0.33, qtdCols, colHeight, indexCol, percent)

barcode.Barcode(c.pdf, barcode.Register(bcode), x, y+marginTop, w, h, false)
barcode.Barcode(self.pdf, barcode.Register(bcode), x, y+marginTop, w, h, false)
return
}
34 changes: 17 additions & 17 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "github.com/johnfercher/maroto"
func ExamplePdfMaroto_Line() {
m := maroto.NewMaroto(maroto.Portrait, maroto.A4)

m.Line()
m.Line(1.0)

// Do more things and save...
}
Expand All @@ -17,7 +17,7 @@ func ExamplePdfMaroto_Row() {
m := maroto.NewMaroto(maroto.Portrait, maroto.A4)
rowHeight := 5.0

m.Row("MyRow", rowHeight, func() {
m.Row(rowHeight, func() {
// ... Add some columns
})

Expand All @@ -30,7 +30,7 @@ func ExamplePdfMaroto_ColSpace() {
m := maroto.NewMaroto(maroto.Portrait, maroto.A4)
rowHeight := 5.0

m.Row("MyRow", rowHeight, func() {
m.Row(rowHeight, func() {
m.ColSpace()
})

Expand All @@ -43,7 +43,7 @@ func ExamplePdfMaroto_ColSpaces() {
m := maroto.NewMaroto(maroto.Portrait, maroto.A4)
rowHeight := 5.0

m.Row("MyRow", rowHeight, func() {
m.Row(rowHeight, func() {
m.ColSpaces(2)
})

Expand All @@ -56,8 +56,8 @@ func ExamplePdfMaroto_Col() {
m := maroto.NewMaroto(maroto.Portrait, maroto.A4)
rowHeight := 5.0

m.Row("MyRow", rowHeight, func() {
m.Col("MyCol", func() {
m.Row(rowHeight, func() {
m.Col(func() {
// Add Image, Text, Signature, QrCode or Barcode...
})
})
Expand Down Expand Up @@ -109,8 +109,8 @@ func ExamplePdfMaroto_Text() {
m := maroto.NewMaroto(maroto.Portrait, maroto.A4)
rowHeight := 5.0

m.Row("MyRow", rowHeight, func() {
m.Col("MyCol", func() {
m.Row(rowHeight, func() {
m.Col(func() {
m.Text("TextContent", &maroto.TextProp{
Size: 12.0,
Style: maroto.BoldItalic,
Expand All @@ -134,8 +134,8 @@ func ExamplePdfMaroto_Signature() {
m := maroto.NewMaroto(maroto.Portrait, maroto.A4)
rowHeight := 5.0

m.Row("MyRow", rowHeight, func() {
m.Col("MyCol", func() {
m.Row(rowHeight, func() {
m.Col(func() {
m.Signature("LabelForSignature", &maroto.SignatureProp{
Size: 12.0,
Style: maroto.BoldItalic,
Expand All @@ -147,9 +147,9 @@ func ExamplePdfMaroto_Signature() {
// Do more things and save...
}

// ExamplePdfMaroto_RowTableList demonstrates how to add a table
// ExamplePdfMaroto_TableList demonstrates how to add a table
// with multiple rows and columns
func ExamplePdfMaroto_RowTableList() {
func ExamplePdfMaroto_TableList() {
m := maroto.NewMaroto(maroto.Portrait, maroto.A4)

headers := []string{"Header1", "Header2"}
Expand All @@ -161,7 +161,7 @@ func ExamplePdfMaroto_RowTableList() {
// 1 Row of header
// 2 Rows of contents
// Each row have 2 columns
m.RowTableList("RowTableList1", headers, contents, nil)
m.TableList(headers, contents, nil)

// Do more things and save...
}
Expand All @@ -179,8 +179,8 @@ func ExamplePdfMaroto_FileImage() {
m := maroto.NewMaroto(maroto.Portrait, maroto.A4)
rowHeight := 5.0

m.Row("MyRow", rowHeight, func() {
m.Col("MyCol", func() {
m.Row(rowHeight, func() {
m.Col(func() {
m.FileImage("path/Image.jpg", &maroto.RectProp{
Left: 5,
Top: 5,
Expand All @@ -207,8 +207,8 @@ func ExamplePdfMaroto_Base64Image() {
rowHeight := 5.0
base64String := "y7seWGHE923Sdgs..."

m.Row("MyRow", rowHeight, func() {
m.Col("MyCol", func() {
m.Row(rowHeight, func() {
m.Col(func() {
m.Base64Image(base64String, maroto.Png, &maroto.RectProp{
Left: 5,
Top: 5,
Expand Down
44 changes: 22 additions & 22 deletions font.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,50 +34,50 @@ func NewFont(pdf gofpdf.Pdf, size float64, family Family, style Style) Font {
}

// Get the currently Font family configured
func (f *font) GetFamily() Family {
return f.family
func (self *font) GetFamily() Family {
return self.family
}

// Get the currently Font style configured
func (f *font) GetStyle() Style {
return f.style
func (self *font) GetStyle() Style {
return self.style
}

// Get the currently Font size configured
func (f *font) GetSize() float64 {
return f.size
func (self *font) GetSize() float64 {
return self.size
}

// Get all the currently Font properties configured
func (f *font) GetFont() (Family, Style, float64) {
return f.family, f.style, f.size
func (self *font) GetFont() (Family, Style, float64) {
return self.family, self.style, self.size
}

// Set the Font family
func (f *font) SetFamily(family Family) {
f.family = family
func (self *font) SetFamily(family Family) {
self.family = family

f.pdf.SetFont(string(f.family), string(f.style), f.size)
self.pdf.SetFont(string(self.family), string(self.style), self.size)
}

// Set the Font style
func (f *font) SetStyle(style Style) {
f.style = style
func (self *font) SetStyle(style Style) {
self.style = style

f.pdf.SetFontStyle(string(f.style))
self.pdf.SetFontStyle(string(self.style))
}

// Set the Font size
func (f *font) SetSize(size float64) {
f.size = size
f.pdf.SetFontSize(f.size)
func (self *font) SetSize(size float64) {
self.size = size
self.pdf.SetFontSize(self.size)
}

// Set all the Font properties
func (f *font) SetFont(family Family, style Style, size float64) {
f.family = family
f.style = style
f.size = size
func (self *font) SetFont(family Family, style Style, size float64) {
self.family = family
self.style = style
self.size = size

f.pdf.SetFont(string(f.family), string(f.style), f.size)
self.pdf.SetFont(string(self.family), string(self.style), self.size)
}
18 changes: 9 additions & 9 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ func NewImage(pdf gofpdf.Pdf, math Math) Image {
}
}

func (i *image) AddFromFile(path string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) {
info := i.pdf.RegisterImageOptions(path, gofpdf.ImageOptions{
func (self *image) AddFromFile(path string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64) {
info := self.pdf.RegisterImageOptions(path, gofpdf.ImageOptions{
ReadDpi: false,
ImageType: "",
})

i.addImageToPdf(path, info, marginTop, qtdCols, colHeight, indexCol, percent)
self.addImageToPdf(path, info, marginTop, qtdCols, colHeight, indexCol, percent)
}

func (i *image) AddFromBase64(b64 string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64, extension Extension) {
func (self *image) AddFromBase64(b64 string, marginTop float64, indexCol float64, qtdCols float64, colHeight float64, percent float64, extension Extension) {
imageId, _ := uuid.NewRandom()

ss, _ := base64.StdEncoding.DecodeString(b64)

info := i.pdf.RegisterImageOptionsReader(
info := self.pdf.RegisterImageOptionsReader(
imageId.String(),
gofpdf.ImageOptions{
ReadDpi: false,
Expand All @@ -49,10 +49,10 @@ func (i *image) AddFromBase64(b64 string, marginTop float64, indexCol float64, q
bytes.NewReader(ss),
)

i.addImageToPdf(imageId.String(), info, marginTop, qtdCols, colHeight, indexCol, percent)
self.addImageToPdf(imageId.String(), info, marginTop, qtdCols, colHeight, indexCol, percent)
}

func (i *image) addImageToPdf(imageLabel string, info *gofpdf.ImageInfoType, marginTop, qtdCols, colHeight, indexCol, percent float64) {
x, y, w, h := i.math.GetRectCenterColProperties(info.Width(), info.Height(), qtdCols, colHeight, indexCol, percent)
i.pdf.Image(imageLabel, x, y+marginTop, w, h, false, "", 0, "")
func (self *image) addImageToPdf(imageLabel string, info *gofpdf.ImageInfoType, marginTop, qtdCols, colHeight, indexCol, percent float64) {
x, y, w, h := self.math.GetRectCenterColProperties(info.Width(), info.Height(), qtdCols, colHeight, indexCol, percent)
self.pdf.Image(imageLabel, x, y+marginTop, w, h, false, "", 0, "")
}
Loading

0 comments on commit b2352e0

Please sign in to comment.