Skip to content

Commit

Permalink
Implement Add Page
Browse files Browse the repository at this point in the history
  • Loading branch information
johnferchermeli committed May 16, 2020
1 parent 34ad35b commit 1d1396b
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 25 deletions.
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,37 @@ go get -u github.com/johnfercher/maroto
```

## Features

![result](internal/assets/images/diagram.png)

#### Grid System
* [Row](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-Row)
* [Col](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-Col)
* [ColSpace](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-ColSpace)
* [ColSpaces](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-ColSpaces)
* [Row](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.Row)
* [Col](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.Col)
* [ColSpace](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.ColSpace)
* [AddPage](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.AddPage)

#### Components To Use Inside a Col
* [Text w/ automatic new lines](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-Text)
* [Signature](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-Signature)
* Image ([From file](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-FileImage) or [Base64](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-Base64Image))
* [QrCode](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#PdfMaroto.QrCode)
* [Barcode](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#PdfMaroto.Barcode)
* [Text w/ automatic new lines](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.Text)
* [Signature](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.Signature)
* Image ([From file](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.FileImage) or [Base64](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.Base64Image))
* [QrCode](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.QrCode)
* [Barcode](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.Barcode)

#### Components To Use Outside a Row
* [TableList](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-TableList)
* [Line](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-Line)
* [TableList](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.TableList)
* [Line](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.Line)

#### Components To Wrap Row, TableList and Line
* [RegisterHeader](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#PdfMaroto.RegisterHeader)
* [RegisterFooter](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#PdfMaroto.RegisterFooter)
* [RegisterHeader](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.RegisterHeader)
* [RegisterFooter](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.RegisterFooter)

#### Others
* [Properties](https://godoc.org/github.com/johnfercher/maroto/pkg/props): most of the components has properties which you can use to customize appearance and behavior.
* [SetBorder](https://godoc.org/github.com/johnfercher/maroto#PdfMaroto.SetDebugMode): Used to draw rectangles in every row and column
* [Properties](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/props?tab=doc): most of the components has properties which you can use to customize appearance and behavior.
* [SetBorder](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.SetBorder): Used to draw rectangles in every row and column
* [SetBackgroundColor](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.SetBackgroundColor): Used to change the background color of one cell.
* [SetPageMargins](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.SetPageMargins): Customize the page margins
* Automatic New Page: New pages are generated automatically when needed.
* 100% Unicode
* Save: You can [save on disk](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-OutputFileAndClose) or export to a [base64 string](https://godoc.org/github.com/johnfercher/maroto/pkg/pdf#example-PdfMaroto-Output)
* Save: You can [save on disk](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.OutputFileAndClose) or export to a [base64 string](https://pkg.go.dev/github.com/johnfercher/maroto/pkg/pdf?tab=doc#PdfMaroto.Output)

#### Roadmap
* Updated in [Issues](https://github.com/johnfercher/maroto/issues)
Expand Down
1 change: 0 additions & 1 deletion internal/examples/billing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func main() {

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

m.RegisterHeader(func() {
m.Row(20, func() {
Expand Down
Binary file modified internal/examples/pdfs/barcodegrid.pdf
Binary file not shown.
Binary file modified internal/examples/pdfs/billing.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.
8 changes: 2 additions & 6 deletions internal/examples/sample1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ func main() {
},
})

m.AddPage()

m.Row(15, func() {
m.Col(12, func() {
m.Text(fmt.Sprintf("Medium Packages / %du.", len(mediumContent)), props.Text{
Expand Down Expand Up @@ -218,12 +220,6 @@ func getSmallContent() ([]string, [][]string) {
contents = append(contents, []string{"São Vicente", "Juiz de Fora", "R$ 35,00"})
contents = append(contents, []string{"Taubaté", "Rio de Janeiro", "R$ 82,00"})
contents = append(contents, []string{"Suzano", "Petrópolis", "R$ 62,00"})
contents = append(contents, []string{"Jundiaí", "Florianópolis", "R$ 21,00"})
contents = append(contents, []string{"Natal", "Jundiaí", "R$ 12,00"})
contents = append(contents, []string{"Niterói", "Itapevi", "R$ 21,00"})
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"})

return header, contents
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/pdf/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ func ExampleNewMaroto() {
// Do more things and save...
}

// ExamplePdfMaroto_AddPage how to force add a new page
func ExamplePdfMaroto_AddPage() {
m := pdf.NewMaroto(consts.Portrait, consts.A4)

// Add rows, cols and components
m.AddPage()

// Add rows, col and components in a new page
// Do more things and save...
}

// ExamplePdfMaroto_Row demonstrates how to define a row.
func ExamplePdfMaroto_Row() {
m := pdf.NewMaroto(consts.Portrait, consts.A4)
Expand Down
16 changes: 15 additions & 1 deletion pkg/pdf/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Maroto interface {
Output() (bytes.Buffer, error)

// Helpers
AddPage()
SetBorder(on bool)
SetBackgroundColor(color color.Color)
GetBorder() bool
Expand Down Expand Up @@ -120,6 +121,19 @@ func NewMaroto(orientation consts.Orientation, pageSize consts.PageSize) Maroto
return maroto
}

// AddPage adds a new page in the PDF
func (s *PdfMaroto) AddPage() {
_, 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(12)
})
}

// RegisterHeader define a sequence of Rows, Lines ou TableLists
// which will be added in every new page
func (s *PdfMaroto) RegisterHeader(closure func()) {
Expand Down Expand Up @@ -435,7 +449,7 @@ func (s *PdfMaroto) createColSpace(actualWidthPerCol float64) {
border = "1"
}

s.Pdf.CellFormat(actualWidthPerCol, s.rowHeight, "", border, 0.0, "C", !s.backgroundColor.IsWhite(), 0.0, "")
s.Pdf.CellFormat(actualWidthPerCol, s.rowHeight, "", border, 0, "C", !s.backgroundColor.IsWhite(), 0, "")
}

func (s *PdfMaroto) drawLastFooter() {
Expand Down
14 changes: 14 additions & 0 deletions pkg/pdf/pdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1845,3 +1845,17 @@ func TestPdfMaroto_GetPageMargins(t *testing.T) {
assert.Equal(t, 0.0, right)
assert.Equal(t, 0.0, bottom)
}

func TestPdfMaroto_AddPage(t *testing.T) {
// Arrange
pdf := basePdfTest(10.0, 10.0, 10.0)
m := newMarotoTest(pdf, nil, nil, nil, nil, nil, nil, nil)

// Act
m.AddPage()

// Assert
pdf.AssertCalled(t, "CellFormat", 80, 0, "", "", 0, "C", false, 0, "")
pdf.AssertCalled(t, "SetFillColor", 255, 255, 255)
pdf.AssertCalled(t, "Ln", 90.0)
}

0 comments on commit 1d1396b

Please sign in to comment.