diff --git a/README.md b/README.md index ab38fd93..397e05ad 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/internal/examples/billing/main.go b/internal/examples/billing/main.go index 0d9716fb..34e85496 100644 --- a/internal/examples/billing/main.go +++ b/internal/examples/billing/main.go @@ -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() { diff --git a/internal/examples/pdfs/barcodegrid.pdf b/internal/examples/pdfs/barcodegrid.pdf index 1c1b87af..bd55c474 100644 Binary files a/internal/examples/pdfs/barcodegrid.pdf and b/internal/examples/pdfs/barcodegrid.pdf differ diff --git a/internal/examples/pdfs/billing.pdf b/internal/examples/pdfs/billing.pdf index 02cd05f2..f20c428c 100644 Binary files a/internal/examples/pdfs/billing.pdf and b/internal/examples/pdfs/billing.pdf differ diff --git a/internal/examples/pdfs/sample1.pdf b/internal/examples/pdfs/sample1.pdf index f96da552..7a78db8e 100644 Binary files a/internal/examples/pdfs/sample1.pdf and b/internal/examples/pdfs/sample1.pdf differ diff --git a/internal/examples/pdfs/zpl.pdf b/internal/examples/pdfs/zpl.pdf index aad361e1..79dee089 100644 Binary files a/internal/examples/pdfs/zpl.pdf and b/internal/examples/pdfs/zpl.pdf differ diff --git a/internal/examples/sample1/main.go b/internal/examples/sample1/main.go index cb945aff..3b699cdd 100644 --- a/internal/examples/sample1/main.go +++ b/internal/examples/sample1/main.go @@ -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{ @@ -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 } diff --git a/pkg/pdf/example_test.go b/pkg/pdf/example_test.go index d73d12b4..ebbc0925 100644 --- a/pkg/pdf/example_test.go +++ b/pkg/pdf/example_test.go @@ -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) diff --git a/pkg/pdf/pdf.go b/pkg/pdf/pdf.go index 84142f57..1cf0ee78 100644 --- a/pkg/pdf/pdf.go +++ b/pkg/pdf/pdf.go @@ -38,6 +38,7 @@ type Maroto interface { Output() (bytes.Buffer, error) // Helpers + AddPage() SetBorder(on bool) SetBackgroundColor(color color.Color) GetBorder() bool @@ -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()) { @@ -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() { diff --git a/pkg/pdf/pdf_test.go b/pkg/pdf/pdf_test.go index 8fd0eaf1..0b8bf3b5 100644 --- a/pkg/pdf/pdf_test.go +++ b/pkg/pdf/pdf_test.go @@ -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) +}