Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 providertype #263

Merged
merged 3 commits into from
Sep 15, 2023
Merged
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
17 changes: 6 additions & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,27 @@ import (
"github.com/johnfercher/maroto/pkg/v2/grid/col"
"github.com/johnfercher/maroto/pkg/v2/grid/row"
"github.com/johnfercher/maroto/pkg/v2/image"
"github.com/johnfercher/maroto/pkg/v2/providers"
"github.com/johnfercher/maroto/pkg/v2/signature"
"github.com/johnfercher/maroto/pkg/v2/size"
"github.com/johnfercher/maroto/pkg/v2/text"
"log"
"os"
)

func main() {
pageSize := size.A4
pdf := buildMarotoPDF(pageSize)
html := buildMarotoHTML(pageSize)
pdf := buildMarotoPDF()
html := buildMarotoHTML()

gen(pdf)
gen(html)
}

func buildMarotoPDF(pageSize size.PageSize) domain.MarotoMetrified {
provider := providers.NewGofpdf(pageSize)
m := v2.NewMaroto(provider, "v2.pdf")
func buildMarotoPDF() domain.MarotoMetrified {
m := v2.NewMaroto("v2.pdf")
return v2.NewMarotoMetrified(m)
}

func buildMarotoHTML(pageSize size.PageSize) domain.MarotoMetrified {
provider := providers.NewHTML(pageSize)
m := v2.NewMaroto(provider, "v2.html")
func buildMarotoHTML() domain.MarotoMetrified {
m := v2.NewMaroto("v2.html", v2.Config{ProviderType: domain.HTML})
return v2.NewMarotoMetrified(m)
}

Expand Down
2 changes: 1 addition & 1 deletion docs/_coverpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

# Maroto <small>PDF</small>

[Github](https://github.com/johnfercher/maroto)
[Github](https://github.com/johnfercher/maroto/tree/v2)
[See](README.md)
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
window.$docsify = {
search: 'auto',
name: 'Maroto',
repo: 'https://github.com/johnfercher/maroto',
repo: 'https://github.com/johnfercher/maroto/tree/v2',
loadSidebar: '_sidebar.md',
loadFooter: true,
coverpage: ['/', '/pt-br/'],
Expand Down
5 changes: 1 addition & 4 deletions internal/examples/barcodegrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ import (
"github.com/johnfercher/maroto/pkg/v2/code/barcode"
"github.com/johnfercher/maroto/pkg/v2/grid/col"
"github.com/johnfercher/maroto/pkg/v2/grid/row"
"github.com/johnfercher/maroto/pkg/v2/providers"
"github.com/johnfercher/maroto/pkg/v2/size"
"os"
)

func main() {
provider := providers.NewGofpdf(size.A4)
maroto := v2.NewMaroto(provider, "internal/examples/pdfs/barcodegridv2.pdf")
maroto := v2.NewMaroto("internal/examples/pdfs/barcodegridv2.pdf")
m := v2.NewMarotoMetrified(maroto)

c1 := col.New(2).Add(barcode.New("https://github.com/johnfercher/maroto", props.Barcode{
Expand Down
Binary file modified internal/examples/pdfs/barcodegridv2.pdf
Binary file not shown.
39 changes: 36 additions & 3 deletions pkg/v2/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,25 @@ import (
"github.com/johnfercher/maroto/pkg/v2/grid/col"
"github.com/johnfercher/maroto/pkg/v2/grid/page"
"github.com/johnfercher/maroto/pkg/v2/grid/row"
"github.com/johnfercher/maroto/pkg/v2/providers"
"github.com/johnfercher/maroto/pkg/v2/size"
)

type Config struct {
PageSize size.PageSize
ProviderType domain.ProviderType
}

func (c *Config) MakeValid() {
if c.PageSize == "" {
c.PageSize = size.A4
}

if c.ProviderType == "" {
c.ProviderType = domain.Gofpdf
}
}

type document struct {
file string
cell internal.Cell
Expand All @@ -20,7 +37,21 @@ type document struct {
currentHeight float64
}

func NewMaroto(provider domain.Provider, file string) *document {
func NewMaroto(file string, config ...Config) *document {
cfg := Config{}
if len(config) > 0 {
cfg = config[0]
}

cfg.MakeValid()

var provider domain.Provider
if cfg.ProviderType == domain.Gofpdf {
provider = providers.NewGofpdf(cfg.PageSize)
} else {
provider = providers.NewHTML(cfg.PageSize)
}

width, height := provider.GetDimensions()
left, top, right, bottom := provider.GetMargins()

Expand Down Expand Up @@ -87,14 +118,16 @@ func (d *document) Generate() error {
}*/

func (d *document) GetStructure() *tree.Node[domain.Structure] {
d.fillPage()

str := domain.Structure{
Type: "document",
Value: d.file,
}
node := tree.NewNode(str)

for _, r := range d.rows {
inner := r.GetStructure()
for _, p := range d.pages {
inner := p.GetStructure()
node.AddNext(inner)
}

Expand Down
18 changes: 5 additions & 13 deletions pkg/v2/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/johnfercher/maroto/pkg/v2/grid/row"
"github.com/johnfercher/maroto/pkg/v2/image"
"github.com/johnfercher/maroto/pkg/v2/signature"
"github.com/johnfercher/maroto/pkg/v2/test"
"github.com/johnfercher/maroto/pkg/v2/text"
"github.com/stretchr/testify/assert"
"testing"
Expand All @@ -27,7 +28,7 @@ func TestNewDocument(t *testing.T) {

func TestDocument_GetStructure(t *testing.T) {
// Arrange
p := v2.NewMaroto("file.txt")
maroto := v2.NewMaroto("file.txt")

r1 := row.New(10)
r1c1 := col.New(4).Add(barcode.New("barcode"))
Expand All @@ -42,17 +43,8 @@ func TestDocument_GetStructure(t *testing.T) {
r2c4 := col.New(3).Add(text.New("text"))
r2.Add(r2c1, r2c2, r2c3, r2c4)

p.Add(r1, r2)
maroto.Add(r1, r2)

// Act
nodeDocument := p.GetStructure()

// Assert Document
assert.NotNil(t, nodeDocument)
_, document := nodeDocument.Get()
assert.Equal(t, "document", document.Type)
assert.Equal(t, "file.txt", document.Value)

nodeRows := nodeDocument.GetNexts()
assert.Equal(t, 2, len(nodeRows))
// Assert
test.New(t).Assert(maroto).JSON(`{"type":"document","nodes":[{"type":"page","nodes":[{"type":"row","nodes":[{"type":"col","nodes":[{"type":"barcode"}]},{"type":"col","nodes":[{"type":"matrixcode"}]},{"type":"col","nodes":[{"type":"qrcode"}]}]},{"type":"row","nodes":[{"type":"col","nodes":[{"type":"fileimage"}]},{"type":"col","nodes":[{"type":"base64image"}]},{"type":"col","nodes":[{"type":"signature"}]},{"type":"col","nodes":[{"type":"text"}]}]},{"type":"row","nodes":[{"type":"col"}]}]}]}`)
}
7 changes: 7 additions & 0 deletions pkg/v2/domain/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import (
"github.com/johnfercher/maroto/pkg/props"
)

type ProviderType string

const (
Gofpdf ProviderType = "gofpdf"
HTML ProviderType = "html"
)

type Provider interface {
// Grid
CreateRow(height float64)
Expand Down
51 changes: 51 additions & 0 deletions pkg/v2/test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package test

import (
"encoding/json"
"github.com/johnfercher/go-tree/tree"
"github.com/johnfercher/maroto/pkg/v2/domain"
"github.com/stretchr/testify/assert"
"testing"
)

type Node struct {
Type string `json:"type"`
Nodes []*Node `json:"nodes,omitempty"`
}

type MarotoTest struct {
t *testing.T
node *tree.Node[domain.Structure]
}

func New(t *testing.T) *MarotoTest {
return &MarotoTest{
t: t,
}
}

func (m *MarotoTest) Assert(maroto domain.Maroto) *MarotoTest {
m.node = maroto.GetStructure()
return m
}

func (m *MarotoTest) JSON(expect string) {
actual := m.buildNode(m.node)
actualBytes, _ := json.Marshal(actual)

assert.Equal(m.t, expect, string(actualBytes))
}

func (m *MarotoTest) buildNode(node *tree.Node[domain.Structure]) *Node {
data := node.GetData()
actual := &Node{
Type: data.Type,
}

nexts := node.GetNexts()
for _, next := range nexts {
actual.Nodes = append(actual.Nodes, m.buildNode(next))
}

return actual
}
Binary file modified v2.pdf
Binary file not shown.