Skip to content

Commit

Permalink
Bottom margin (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher authored May 28, 2024
1 parent 7899180 commit 8cc69ea
Show file tree
Hide file tree
Showing 18 changed files with 479 additions and 82 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ You can write your PDFs like you are creating a site using Bootstrap. A Row may
Besides that, pages will be added when content may extrapolate the useful area. You can define a header which will be added
always when a new page appear, in this case, a header may have many rows, lines or tablelist.

#### Maroto `v2.0.6` is here! Try out:
#### Maroto `v2.0.7` is here! Try out:

* Installation with`go get`:

```bash
go get github.com/johnfercher/maroto/v2@v2.0.6
go get github.com/johnfercher/maroto/v2@v2.0.7
```

* You can see the full `v2` documentation [here](https://maroto.io/).
Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

* We are about to create a document processor to generate PDFs by interpreting serialized data as: yml, json or html. Please contribute with your ideas in [this discussion](https://github.com/johnfercher/maroto/discussions/390).

#### 3. Maroto`v2.0.6`is here! Try out:
#### 3. Maroto`v2.0.7`is here! Try out:

* Installation with`go get`:

```bash
go get github.com/johnfercher/maroto/v2@v2.0.6
go get github.com/johnfercher/maroto/v2@v2.0.7
```

The public API was completely redesigned with the aim of enhancing the
Expand Down
2 changes: 1 addition & 1 deletion docs/_coverpage.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![logo](assets/images/logo.png)

# Maroto <small>v2.0.6</small>
# Maroto <small>v2.0.7</small>

> An open-source golang lib to create PDFs. Fast and Simple.
Expand Down
4 changes: 3 additions & 1 deletion docs/assets/examples/background/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ func GetMaroto(image string) core.Maroto {
log.Fatal(err)
}
b := config.NewBuilder().
WithMargins(0, 0, 0).
WithTopMargin(0).
WithLeftMargin(0).
WithRightMargin(0).
WithOrientation(orientation.Horizontal).
WithMaxGridSize(20).
WithBackgroundImage(bytes, extension.Png)
Expand Down
4 changes: 3 additions & 1 deletion docs/assets/examples/billing/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func main() {
func GetMaroto() core.Maroto {
cfg := config.NewBuilder().
WithPageNumber().
WithMargins(10, 15, 10).
WithLeftMargin(10).
WithTopMargin(15).
WithRightMargin(10).
Build()

darkGrayColor := getDarkGrayColor()
Expand Down
4 changes: 3 additions & 1 deletion docs/assets/examples/disablepagebreak/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func GetMaroto(image string) core.Maroto {
log.Fatal(err)
}
b := config.NewBuilder().
WithMargins(0, 0, 0).
WithTopMargin(0).
WithRightMargin(0).
WithLeftMargin(0).
WithDimensions(361.8, 203.2).
WithDisableAutoPageBreak(true).
WithOrientation(orientation.Horizontal).
Expand Down
35 changes: 24 additions & 11 deletions docs/assets/examples/margins/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"log"

"github.com/johnfercher/maroto/v2/pkg/components/row"

"github.com/johnfercher/maroto/v2"
"github.com/johnfercher/maroto/v2/pkg/components/col"
"github.com/johnfercher/maroto/v2/pkg/components/image"
Expand Down Expand Up @@ -32,24 +34,35 @@ func main() {

func GetMaroto() core.Maroto {
cfg := config.NewBuilder().
WithMargins(20, 20, 20).
WithTopMargin(20).
WithLeftMargin(20).
WithRightMargin(20).
WithDebug(true).
Build()

mrt := maroto.New(cfg)
m := maroto.NewMetricsDecorator(mrt)

m.AddRow(40,
image.NewFromFileCol(4, "docs/assets/images/gopherbw.png", props.Rect{
Center: true,
Percent: 50,
}),
text.NewCol(4, "Margins Test", props.Text{
Top: 12,
Size: 12,
}),
col.New(4),
err := m.RegisterHeader(
row.New(40).Add(
image.NewFromFileCol(4, "docs/assets/images/gopherbw.png", props.Rect{
Center: true,
Percent: 50,
}),
text.NewCol(4, "Margins Test", props.Text{
Top: 12,
Size: 12,
}),
col.New(4),
),
)
if err != nil {
log.Fatal(err)
}

for i := 0; i < 10; i++ {
m.AddRows(text.NewRow(30, "any text"))
}

return m
}
Binary file modified docs/assets/pdf/backgroundv2.pdf
Binary file not shown.
Binary file modified docs/assets/pdf/marginsv2.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions docs/assets/text/backgroundv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 2169.54ms, executions: [2169.54ms]
add_page -> avg: 8596.00ns, executions: [8.60μs]
generate -> avg: 541.29ms, executions: [541.29ms]
add_page -> avg: 2216.00ns, executions: [2.22μs]
file_size -> 897.39Kb
7 changes: 4 additions & 3 deletions docs/assets/text/marginsv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
generate -> avg: 605.72ms, executions: [605.72ms]
add_row -> avg: 1543.00ns, executions: [1.54μs]
file_size -> 253.43Kb
generate -> avg: 356.27ms, executions: [356.27ms]
header -> avg: 549.00ns, executions: [549.00ns]
add_rows -> avg: 94.10ns, executions: [131.00ns, 54.00ns, 26.00ns, 83.00ns, 16.00ns, 16.00ns, 16.00ns, 76.00ns, 478.00ns, 45.00ns]
file_size -> 256.71Kb
2 changes: 2 additions & 0 deletions internal/providers/gofpdf/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func (b *builder) Build(cfg *entity.Config, cache cache.Cache) *Dependencies {

if cfg.DisableAutoPageBreak {
fpdf.SetAutoPageBreak(false, 0)
} else {
fpdf.SetAutoPageBreak(true, cfg.Margins.Bottom)
}

fpdf.SetMargins(cfg.Margins.Left, cfg.Margins.Top, cfg.Margins.Right)
Expand Down
85 changes: 59 additions & 26 deletions internal/providers/gofpdf/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"testing"

"github.com/johnfercher/maroto/v2/internal/fixture"
"github.com/johnfercher/maroto/v2/internal/providers/gofpdf"
"github.com/johnfercher/maroto/v2/pkg/consts/fontfamily"
"github.com/johnfercher/maroto/v2/pkg/core/entity"

"github.com/johnfercher/maroto/v2/internal/providers/gofpdf"
"github.com/stretchr/testify/assert"
)

Expand All @@ -21,32 +22,64 @@ func TestNewBuilder(t *testing.T) {
}

func TestBuilder_Build(t *testing.T) {
// Arrange
sut := gofpdf.NewBuilder()
font := fixture.FontProp()
cfg := &entity.Config{
Dimensions: &entity.Dimensions{
Width: 100,
Height: 200,
},
Margins: &entity.Margins{
Left: 10,
Top: 10,
Right: 10,
Bottom: 10,
},
DefaultFont: &font,
CustomFonts: []*entity.CustomFont{
{
Family: fontfamily.Arial,
t.Run("when DisableAutoPageBreak true, should build correctly", func(t *testing.T) {
// Arrange
sut := gofpdf.NewBuilder()
font := fixture.FontProp()
cfg := &entity.Config{
Dimensions: &entity.Dimensions{
Width: 100,
Height: 200,
},
},
DisableAutoPageBreak: true,
}
Margins: &entity.Margins{
Left: 10,
Top: 10,
Right: 10,
Bottom: 10,
},
DefaultFont: &font,
CustomFonts: []*entity.CustomFont{
{
Family: fontfamily.Arial,
},
},
DisableAutoPageBreak: true,
}

// Act
dep := sut.Build(cfg, nil)
// Act
dep := sut.Build(cfg, nil)

// Assert
assert.NotNil(t, dep)
// Assert
assert.NotNil(t, dep)
})
t.Run("when DisableAutoPageBreak false, should build correctly", func(t *testing.T) {
// Arrange
sut := gofpdf.NewBuilder()
font := fixture.FontProp()
cfg := &entity.Config{
Dimensions: &entity.Dimensions{
Width: 100,
Height: 200,
},
Margins: &entity.Margins{
Left: 10,
Top: 10,
Right: 10,
Bottom: 10,
},
DefaultFont: &font,
CustomFonts: []*entity.CustomFont{
{
Family: fontfamily.Arial,
},
},
DisableAutoPageBreak: false,
}

// Act
dep := sut.Build(cfg, nil)

// Assert
assert.NotNil(t, dep)
})
}
36 changes: 29 additions & 7 deletions pkg/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import (
type Builder interface {
WithPageSize(size pagesize.Type) Builder
WithDimensions(width float64, height float64) Builder
WithMargins(left float64, top float64, right float64) Builder
WithLeftMargin(left float64) Builder
WithTopMargin(top float64) Builder
WithRightMargin(right float64) Builder
WithBottomMargin(bottom float64) Builder
WithConcurrentMode(chunkWorkers int) Builder
WithSequentialMode() Builder
WithSequentialLowMemoryMode(chunkWorkers int) Builder
Expand Down Expand Up @@ -114,24 +117,43 @@ func (b *CfgBuilder) WithDimensions(width float64, height float64) Builder {
return b
}

// WithMargins defines custom margins, bottom margin is not customizable due to gofpdf limitations.
func (b *CfgBuilder) WithMargins(left float64, top float64, right float64) Builder {
// WithLeftMargin customize margin.
func (b *CfgBuilder) WithLeftMargin(left float64) Builder {
if left < pagesize.MinLeftMargin {
return b
}

if top < pagesize.MinRightMargin {
b.margins.Left = left
return b
}

// WithTopMargin customize margin.
func (b *CfgBuilder) WithTopMargin(top float64) Builder {
if top < pagesize.MinTopMargin {
return b
}

if right < pagesize.MinTopMargin {
b.margins.Top = top
return b
}

// WithRightMargin customize margin.
func (b *CfgBuilder) WithRightMargin(right float64) Builder {
if right < pagesize.MinRightMargin {
return b
}

b.margins.Left = left
b.margins.Top = top
b.margins.Right = right
return b
}

// WithBottomMargin customize margin.
func (b *CfgBuilder) WithBottomMargin(bottom float64) Builder {
if bottom < pagesize.MinBottomMargin {
return b
}

b.margins.Bottom = bottom
return b
}

Expand Down
Loading

0 comments on commit 8cc69ea

Please sign in to comment.