Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/providers/gofpdf/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gofpdf

import (
"slices"

"github.com/phpdave11/gofpdf"

"github.com/johnfercher/maroto/v2/internal/cache"
Expand Down Expand Up @@ -51,7 +53,7 @@ func (b *builder) Build(cfg *entity.Config, cache cache.Cache) *Dependencies {
})

for _, font := range cfg.CustomFonts {
fpdf.AddUTF8FontFromBytes(font.GetFamily(), string(font.GetStyle()), font.GetBytes())
fpdf.AddUTF8FontFromBytes(font.GetFamily(), string(font.GetStyle()), slices.Clone(font.GetBytes()))
}

if cfg.DisableAutoPageBreak {
Expand Down
33 changes: 31 additions & 2 deletions maroto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package maroto_test

import (
"fmt"
"os"
"runtime"
"testing"
"time"

"github.com/johnfercher/maroto/v2/pkg/components/code"
"github.com/johnfercher/maroto/v2/pkg/components/text"

"github.com/johnfercher/maroto/v2/pkg/components/col"
"github.com/johnfercher/maroto/v2/pkg/components/page"
"github.com/johnfercher/maroto/v2/pkg/components/row"
"github.com/johnfercher/maroto/v2/pkg/components/text"
"github.com/johnfercher/maroto/v2/pkg/config"
"github.com/johnfercher/maroto/v2/pkg/consts/fontstyle"
"github.com/johnfercher/maroto/v2/pkg/core"
"github.com/johnfercher/maroto/v2/pkg/fontrepository"
"github.com/johnfercher/maroto/v2/pkg/props"
"github.com/johnfercher/maroto/v2/pkg/test"

"github.com/johnfercher/maroto/v2"
Expand Down Expand Up @@ -597,4 +600,30 @@ func TestMaroto_RegisterFooter(t *testing.T) {
assert.Nil(t, err)
test.New(t).Assert(sut.GetStructure()).Equals("footer_auto_row.json")
})

t.Run("when running with concurrent mode, no race condition while accessing font", func(t *testing.T) {
fontData, err := os.ReadFile("docs/assets/fonts/arial-unicode-ms.ttf")
assert.NoError(t, err)
customFonts, err := fontrepository.New().
AddUTF8FontFromBytes("customFontName", fontstyle.Normal, fontData).
Load()

assert.NoError(t, err)

row := row.New(10).Add(col.New())
cfg := config.NewBuilder().
WithCustomFonts(customFonts).
WithDefaultFont(&props.Font{Family: "customFontName", Size: 16}).
WithConcurrentMode(4).
Build()

m := maroto.New(cfg)
for range 150 {
// Generate multiple pages
m.AddRows(row)
}

_, err = m.Generate()
assert.NoError(t, err)
})
}