Skip to content

Commit

Permalink
Fix code bug and add documentation (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher authored Aug 2, 2024
1 parent 35d40c9 commit 8a057a1
Show file tree
Hide file tree
Showing 16 changed files with 374 additions and 37 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.1.0` is here! Try out:
#### Maroto `v2.1.1` is here! Try out:

* Installation with`go get`:

```bash
go get github.com/johnfercher/maroto/v2@v2.1.0
go get github.com/johnfercher/maroto/v2@v2.1.1
```

* 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.1.0`is here! Try out:
#### 3. Maroto`v2.1.1`is here! Try out:

* Installation with`go get`:

```bash
go get github.com/johnfercher/maroto/v2@v2.1.0
go get github.com/johnfercher/maroto/v2@v2.1.1
```

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.1.0</small>
# Maroto <small>v2.1.1</small>

> An open-source golang lib to create PDFs. Fast and Simple.
Expand Down
81 changes: 81 additions & 0 deletions docs/assets/examples/autorow/v2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"log"

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

"github.com/johnfercher/maroto/v2/pkg/core"

"github.com/johnfercher/maroto/v2"

"github.com/johnfercher/maroto/v2/pkg/config"
)

func main() {
m := GetMaroto()
document, err := m.Generate()
if err != nil {
log.Fatal(err.Error())
}

err = document.Save("docs/assets/pdf/autorow.pdf")
if err != nil {
log.Fatal(err.Error())
}

err = document.GetReport().Save("docs/assets/text/autorow.txt")
if err != nil {
log.Fatal(err.Error())
}
}

func GetMaroto() core.Maroto {
cfg := config.NewBuilder().
WithDebug(true).
Build()

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

m.AddAutoRow(
image.NewFromFileCol(5, "docs/assets/images/biplane.jpg"),
text.NewCol(7, intro),
)

m.AddAutoRow(
image.NewFromFileCol(5, "docs/assets/images/biplane.jpg"),
text.NewCol(7, intro, props.Text{
Size: 13,
}),
)

m.AddAutoRow(
code.NewBarCol(4, "code"),
text.NewCol(8, intro),
)

m.AddAutoRow(
code.NewMatrixCol(3, "code"),
text.NewCol(9, intro),
)

m.AddAutoRow(
code.NewQrCol(2, "code"),
text.NewCol(10, intro),
)

return m
}

var intro = `Numa toca no chão vivia um hobbit. Não uma toca nojenta, suja, úmida,
cheia de pontas de minhocas e um cheiro de limo, nem tam pouco uma toca seca, vazia, arenosa,
sem nenhum lugar onde se sentar ou onde comer: era uma toca de hobbit, e isso significa conforto.
Ela tinha uma porta perfeitamente redonda feito uma escotilha, pintada de verde, com uma maçaneta
amarela e brilhante de latão exatamente no meio. A porta se abria para um corredor em forma de tubo,
feito um túnel: um túnel muito confortável, sem fumaça, de paredes com painéis e assoalhos
azulejados e acarpetados, com cadeiras enceradas e montes e montes de cabieiros para chapéus e
casacos - o hobbit apreciava visitas.`
15 changes: 15 additions & 0 deletions docs/assets/examples/autorow/v2/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"testing"

"github.com/johnfercher/maroto/v2/pkg/test"
)

func TestGetMaroto(t *testing.T) {
// Act
sut := GetMaroto()

// Assert
test.New(t).Assert(sut.GetStructure()).Equals("examples/autorow.json")
}
2 changes: 1 addition & 1 deletion docs/assets/examples/textgrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func GetMaroto() core.Maroto {
)

m.AddAutoRow(
text.NewCol(12, longText+" "+longText+" "+longText, props.Text{Top: 15, Left: 3, Right: 3, Align: align.Justify, BreakLineStrategy: breakline.EmptySpaceStrategy}),
text.NewCol(12, longText+" "+longText+" "+longText, props.Text{Left: 3, Right: 3, Align: align.Justify, BreakLineStrategy: breakline.EmptySpaceStrategy}),
)
return m
}
Binary file added docs/assets/pdf/autorow.pdf
Binary file not shown.
Binary file modified docs/assets/pdf/textgridv2.pdf
Binary file not shown.
2 changes: 2 additions & 0 deletions docs/assets/text/autorow.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
generate -> avg: 2.94ms, executions: [2.94ms]
file_size -> 219.47Kb
6 changes: 3 additions & 3 deletions docs/assets/text/textgridv2.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
generate -> avg: 5.22ms, executions: [5.22ms]
add_row -> avg: 245.50ns, executions: [544.00ns, 209.00ns, 101.00ns, 81.00ns, 80.00ns, 458.00ns]
add_rows -> avg: 77.00ns, executions: [121.00ns, 87.00ns, 40.00ns, 90.00ns, 37.00ns, 87.00ns]
generate -> avg: 4.97ms, executions: [4.97ms]
add_row -> avg: 325.33ns, executions: [946.00ns, 211.00ns, 103.00ns, 91.00ns, 92.00ns, 509.00ns]
add_rows -> avg: 76.50ns, executions: [127.00ns, 85.00ns, 41.00ns, 87.00ns, 36.00ns, 83.00ns]
file_size -> 28.15Kb
1 change: 1 addition & 0 deletions docs/v2/features/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* **Features**
* [Basics](v2/features/basics.md?id=basics)
* [Add Page](v2/features/addpage.md?id=add-page)
* [Auto Row](v2/features/autorow.md?id=auto-row)
* [Background](v2/features/background.md?id=add-background)
* [Barcode](v2/features/barcode.md?id=barcode)
* [Cell Style](v2/features/cellstyle.md?id=cell-style)
Expand Down
20 changes: 20 additions & 0 deletions docs/v2/features/autorow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Add Row

## GoDoc
* [constructor : New](https://pkg.go.dev/github.com/johnfercher/maroto/v2/pkg/components/page#New)
* [interface : Page](https://pkg.go.dev/github.com/johnfercher/maroto/v2/pkg/core#Page)
* [props : Page](https://pkg.go.dev/github.com/johnfercher/maroto/v2/pkg/props#Page)

## Code Example
[filename](../../assets/examples/autorow/v2/main.go ':include :type=code')

## PDF Generated
```pdf
assets/pdf/autorow.pdf
```

## Time Execution
[filename](../../assets/text/autorow.txt ':include :type=code')

## Test File
[filename](https://raw.githubusercontent.com/johnfercher/maroto/master/test/maroto/examples/autorow.json ':include :type=code')
9 changes: 5 additions & 4 deletions internal/providers/gofpdf/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gofpdf
import (
"bytes"
"errors"
"fmt"
"path/filepath"
"strings"

Expand Down Expand Up @@ -62,7 +63,7 @@ func (g *provider) AddLine(cell *entity.Cell, prop *props.Line) {
}

func (g *provider) AddMatrixCode(code string, cell *entity.Cell, prop *props.Rect) {
img, err := g.loadCode(code, g.code.GenDataMatrix)
img, err := g.loadCode(fmt.Sprintf("matrix-code-%s", code), g.code.GenDataMatrix)
if err != nil {
g.text.Add("could not generate matrixcode", cell, merror.DefaultErrorText)
return
Expand All @@ -76,7 +77,7 @@ func (g *provider) AddMatrixCode(code string, cell *entity.Cell, prop *props.Rec
}

func (g *provider) AddQrCode(code string, cell *entity.Cell, prop *props.Rect) {
img, err := g.loadCode(code, g.code.GenQr)
img, err := g.loadCode(fmt.Sprintf("qr-code-%s", code), g.code.GenQr)
if err != nil {
g.text.Add("could not generate qrcode", cell, merror.DefaultErrorText)
return
Expand All @@ -90,7 +91,7 @@ func (g *provider) AddQrCode(code string, cell *entity.Cell, prop *props.Rect) {
}

func (g *provider) AddBarCode(code string, cell *entity.Cell, prop *props.Barcode) {
image, err := g.cache.GetImage(g.getBarcodeImageName(code, prop), extension.Jpg)
image, err := g.cache.GetImage(g.getBarcodeImageName(fmt.Sprintf("bar-code-%s", code), prop), extension.Jpg)
if err != nil {
image, err = g.code.GenBar(code, cell, prop)
}
Expand All @@ -99,7 +100,7 @@ func (g *provider) AddBarCode(code string, cell *entity.Cell, prop *props.Barcod
return
}

g.cache.AddImage(g.getBarcodeImageName(code, prop), image)
g.cache.AddImage(g.getBarcodeImageName(fmt.Sprintf("bar-code-%s", code), prop), image)
err = g.image.Add(image, cell, g.cfg.Margins, prop.ToRectProp(), extension.Jpg, false)
if err != nil {
g.fpdf.ClearError()
Expand Down
40 changes: 20 additions & 20 deletions internal/providers/gofpdf/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ func TestProvider_AddMatrixCode(t *testing.T) {
prop := fixture.RectProp()

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(nil, errors.New("anyError1"))
cache.EXPECT().GetImage("matrix-code-code", extension.Jpg).Return(nil, errors.New("anyError1"))

code := mocks.NewCode(t)
code.EXPECT().GenDataMatrix(codeContent).Return(nil, errors.New("anyError2"))
code.EXPECT().GenDataMatrix("matrix-code-code").Return(nil, errors.New("anyError2"))

text := mocks.NewText(t)
text.EXPECT().Add("could not generate matrixcode", cell, merror.DefaultErrorText)
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestProvider_AddMatrixCode(t *testing.T) {
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(img, nil)
cache.EXPECT().GetImage("matrix-code-code", extension.Jpg).Return(img, nil)

code := mocks.NewCode(t)

Expand Down Expand Up @@ -173,7 +173,7 @@ func TestProvider_AddMatrixCode(t *testing.T) {
sut := gofpdf.New(dep)

// Act
sut.AddMatrixCode(codeContent, cell, &prop)
sut.AddMatrixCode("code", cell, &prop)

// Assert
cache.AssertNumberOfCalls(t, "GetImage", 1)
Expand All @@ -190,7 +190,7 @@ func TestProvider_AddMatrixCode(t *testing.T) {
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(img, nil)
cache.EXPECT().GetImage("matrix-code-code", extension.Jpg).Return(img, nil)

code := mocks.NewCode(t)

Expand All @@ -216,7 +216,7 @@ func TestProvider_AddMatrixCode(t *testing.T) {
sut := gofpdf.New(dep)

// Act
sut.AddMatrixCode(codeContent, cell, &prop)
sut.AddMatrixCode("code", cell, &prop)

// Assert
cache.AssertNumberOfCalls(t, "GetImage", 1)
Expand All @@ -233,10 +233,10 @@ func TestProvider_AddQrCode(t *testing.T) {
prop := fixture.RectProp()

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(nil, errors.New("anyError1"))
cache.EXPECT().GetImage("qr-code-code", extension.Jpg).Return(nil, errors.New("anyError1"))

code := mocks.NewCode(t)
code.EXPECT().GenQr(codeContent).Return(nil, errors.New("anyError2"))
code.EXPECT().GenQr("qr-code-code").Return(nil, errors.New("anyError2"))

text := mocks.NewText(t)
text.EXPECT().Add("could not generate qrcode", cell, merror.DefaultErrorText)
Expand All @@ -250,7 +250,7 @@ func TestProvider_AddQrCode(t *testing.T) {
sut := gofpdf.New(dep)

// Act
sut.AddQrCode(codeContent, cell, &prop)
sut.AddQrCode("code", cell, &prop)

// Assert
cache.AssertNumberOfCalls(t, "GetImage", 1)
Expand All @@ -266,7 +266,7 @@ func TestProvider_AddQrCode(t *testing.T) {
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(img, nil)
cache.EXPECT().GetImage("qr-code-code", extension.Jpg).Return(img, nil)

code := mocks.NewCode(t)

Expand Down Expand Up @@ -300,7 +300,7 @@ func TestProvider_AddQrCode(t *testing.T) {
sut := gofpdf.New(dep)

// Act
sut.AddQrCode(codeContent, cell, &prop)
sut.AddQrCode("code", cell, &prop)

// Assert
cache.AssertNumberOfCalls(t, "GetImage", 1)
Expand All @@ -317,7 +317,7 @@ func TestProvider_AddQrCode(t *testing.T) {
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(img, nil)
cache.EXPECT().GetImage("qr-code-code", extension.Jpg).Return(img, nil)

code := mocks.NewCode(t)

Expand All @@ -343,7 +343,7 @@ func TestProvider_AddQrCode(t *testing.T) {
sut := gofpdf.New(dep)

// Act
sut.AddQrCode(codeContent, cell, &prop)
sut.AddQrCode("code", cell, &prop)

// Assert
cache.AssertNumberOfCalls(t, "GetImage", 1)
Expand All @@ -360,7 +360,7 @@ func TestProvider_AddBarCode(t *testing.T) {
prop := fixture.BarcodeProp()

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent+"code128", extension.Jpg).Return(nil, errors.New("anyError1"))
cache.EXPECT().GetImage("bar-code-codecode128", extension.Jpg).Return(nil, errors.New("anyError1"))

code := mocks.NewCode(t)
code.EXPECT().GenBar(codeContent, cell, &prop).Return(nil, errors.New("anyError2"))
Expand Down Expand Up @@ -392,8 +392,8 @@ func TestProvider_AddBarCode(t *testing.T) {
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent+"code128", extension.Jpg).Return(img, nil)
cache.EXPECT().AddImage(codeContent+"code128", img)
cache.EXPECT().GetImage("bar-code-codecode128", extension.Jpg).Return(img, nil)
cache.EXPECT().AddImage("bar-code-codecode128", img)

text := mocks.NewText(t)
text.EXPECT().Add("could not add barcode to document", cell, merror.DefaultErrorText)
Expand Down Expand Up @@ -440,8 +440,8 @@ func TestProvider_AddBarCode(t *testing.T) {
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent+"code128", extension.Jpg).Return(img, nil)
cache.EXPECT().AddImage(codeContent+"code128", img)
cache.EXPECT().GetImage("bar-code-codecode128", extension.Jpg).Return(img, nil)
cache.EXPECT().AddImage("bar-code-codecode128", img)

cfg := &entity.Config{
Margins: &entity.Margins{
Expand Down Expand Up @@ -480,8 +480,8 @@ func TestProvider_AddBarCode(t *testing.T) {
img := &entity.Image{Bytes: []byte{1, 2, 3}}

cache := mocks.NewCache(t)
cache.EXPECT().GetImage(codeContent+"ean", extension.Jpg).Return(img, nil)
cache.EXPECT().AddImage(codeContent+"ean", img)
cache.EXPECT().GetImage("bar-code-codeean", extension.Jpg).Return(img, nil)
cache.EXPECT().AddImage("bar-code-codeean", img)

cfg := &entity.Config{
Margins: &entity.Margins{
Expand Down
Loading

0 comments on commit 8a057a1

Please sign in to comment.