Skip to content

Commit

Permalink
Solve bytes of file issue (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher authored Sep 15, 2023
1 parent fb1dbc8 commit 8ebd4a9
Show file tree
Hide file tree
Showing 17 changed files with 214 additions and 225 deletions.
42 changes: 12 additions & 30 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,35 @@ import (
"github.com/johnfercher/maroto/pkg/props"
"github.com/johnfercher/maroto/pkg/v2"
"github.com/johnfercher/maroto/pkg/v2/code"
"github.com/johnfercher/maroto/pkg/v2/config"
"github.com/johnfercher/maroto/pkg/v2/domain"
"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/provider"
"github.com/johnfercher/maroto/pkg/v2/signature"
"github.com/johnfercher/maroto/pkg/v2/text"
"log"
"os"
)

func main() {
pdf := buildMarotoPDF()
html := buildMarotoHTML()
maroto := v2.NewMaroto()
m := v2.NewMetricsDecorator(maroto)

gen(pdf)
gen(html)
}

func buildMarotoPDF() domain.MarotoMetrified {
m := v2.NewMaroto("v2.pdf")
return v2.NewMarotoMetrified(m)
}

func buildMarotoHTML() domain.MarotoMetrified {
builder := config.NewBuilder().
WithPageSize(config.A4).
WithProvider(provider.HTML)

m := v2.NewMaroto("v2.html", builder)
return v2.NewMarotoMetrified(m)
}

func gen(m domain.MarotoMetrified) {
for _ = range [10]int{} {
m.Add(buildCodesRow(), buildImagesRow(), buildTextsRow())
}

report, err := m.GenerateWithReport()
document, err := m.Generate()
if err != nil {
log.Fatal(err.Error())
}

err = document.Save("v2.pdf")
if err != nil {
log.Fatal(err.Error())
}

report.Print()
document.Report.Print()
}

func buildCodesRow() domain.Row {
Expand All @@ -71,7 +55,7 @@ func buildCodesRow() domain.Row {
}

func buildImagesRow() domain.Row {
row := row.New(70)
r := row.New(70)

col1 := col.New(6)
col1.Add(image.NewFromFile("internal/assets/images/frontpage.png"))
Expand All @@ -85,9 +69,8 @@ func buildImagesRow() domain.Row {
col2 := col.New(6)
col2.Add(image.NewFromBase64(stringBase64, consts.Png))

row.Add(col1, col2)

return row
r.Add(col1, col2)
return r
}

func buildTextsRow() domain.Row {
Expand All @@ -107,6 +90,5 @@ func buildTextsRow() domain.Row {
}))

row.Add(col1, col2)

return row
}
2 changes: 1 addition & 1 deletion docs/assets/go/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func main() {

// Add things

err := m.Generate()
err := m.GenerateConcurrently()
if err != nil {
log.Fatal(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion docs/assets/go/createmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func main() {
m := v2.NewMaroto("v2.pdf")
metrics := v2.NewMarotoMetrified(m)
metrics := v2.NewMetricsDecorator(m)

// Add things

Expand Down
2 changes: 1 addition & 1 deletion internal/examples/barcodegrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func main() {
maroto := v2.NewMaroto("internal/examples/pdfs/barcodegridv2.pdf")
m := v2.NewMarotoMetrified(maroto)
m := v2.NewMetricsDecorator(maroto)

c1 := col.New(2).Add(code.NewBar("https://github.com/johnfercher/maroto", props.Barcode{
Percent: 50,
Expand Down
2 changes: 1 addition & 1 deletion internal/examples/datamatrixgrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func main() {
maroto := v2.NewMaroto("internal/examples/pdfs/datamatrixgridv2.pdf")
m := v2.NewMarotoMetrified(maroto)
m := v2.NewMetricsDecorator(maroto)

c1 := col.New(2).Add(code.NewMatrix("https://github.com/johnfercher/maroto", props.Rect{
Percent: 50,
Expand Down
2 changes: 1 addition & 1 deletion internal/examples/imagegrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func main() {
maroto := v2.NewMaroto("internal/examples/pdfs/imagegridv2.pdf")
m := v2.NewMarotoMetrified(maroto)
m := v2.NewMetricsDecorator(maroto)

c1 := col.New(2).Add(image.NewFromFile("internal/assets/images/biplane.jpg", props.Rect{
Center: true,
Expand Down
2 changes: 1 addition & 1 deletion internal/examples/qrgrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func main() {
maroto := v2.NewMaroto("internal/examples/pdfs/qrgridv2.pdf")
m := v2.NewMarotoMetrified(maroto)
m := v2.NewMetricsDecorator(maroto)

c1 := col.New(2).Add(code.NewQr("https://github.com/johnfercher/maroto", props.Rect{
Percent: 50,
Expand Down
2 changes: 1 addition & 1 deletion internal/examples/signaturegrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func main() {
maroto := v2.NewMaroto("internal/examples/pdfs/signaturegridv2.pdf")
m := v2.NewMarotoMetrified(maroto)
m := v2.NewMetricsDecorator(maroto)

c1 := col.New(2).Add(signature.New("Signature 1"))
c2 := col.New(4).Add(signature.New("Signature 2", props.Font{Family: consts.Courier}))
Expand Down
2 changes: 1 addition & 1 deletion internal/examples/textgrid/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func main() {
maroto := v2.NewMaroto("internal/examples/pdfs/textgridv2.pdf")
m := v2.NewMarotoMetrified(maroto)
m := v2.NewMetricsDecorator(maroto)

longText := "This is a longer sentence that will be broken into multiple lines " +
"as it does not fit into the column otherwise."
Expand Down
53 changes: 26 additions & 27 deletions pkg/v2/document.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package v2

import (
"bytes"
"github.com/f-amaral/go-async/pool"
"github.com/johnfercher/go-tree/tree"
"github.com/johnfercher/maroto/internal"
"github.com/johnfercher/maroto/pkg/color"
Expand All @@ -19,12 +17,9 @@ import (
"github.com/johnfercher/maroto/pkg/v2/providers"
"github.com/pdfcpu/pdfcpu/pkg/api"
"io"
"log"
"os"
)

type document struct {
file string
cell internal.Cell
provider domain.Provider
pages []domain.Page
Expand All @@ -34,15 +29,14 @@ type document struct {
config []config.Builder
}

func NewMaroto(file string, config ...config.Builder) *document {
func NewMaroto(config ...config.Builder) *document {
cache := cache.New()
provider := getProvider(cache, config...)

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

return &document{
file: file,
provider: provider,
cell: context.NewRootContext(width, height, context.Margins{
Left: left,
Expand All @@ -63,26 +57,32 @@ func (d *document) Add(rows ...domain.Row) {
d.addRows(rows...)
}

//func (d *document) Generate() error {
// d.fillPage()
// innerCtx := d.cell.Copy()
//
// for _, page := range d.pages {
// //fmt.Printf("render page: %d\n", page.GetNumber())
// page.Render(d.provider, innerCtx)
// }
//
// return d.provider.Generate(d.file)
//}

func (d *document) Generate() error {
func (d *document) Generate() (*domain.Document, error) {
d.fillPage()
innerCtx := d.cell.Copy()

p := pool.NewPool(10, func(i domain.Page) (bytes.Buffer, error) {
for _, page := range d.pages {
page.Render(d.provider, innerCtx)
}

bytes, err := d.provider.GenerateBytes()
if err != nil {
return nil, err
}

return &domain.Document{
Bytes: bytes,
}, nil
}

/*func (d *document) GenerateConcurrently() error {
d.fillPage()
innerCtx := d.cell.Copy()
p := pool.NewPool(10, func(i domain.Page) ([]byte, error) {
innerProvider := getProvider(d.imageCache, d.config...)
i.Render(innerProvider, innerCtx)
return innerProvider.GenerateAndOutput()
return innerProvider.GenerateBytes()
})
processed := p.Process(d.pages)
Expand All @@ -91,8 +91,8 @@ func (d *document) Generate() error {
}
readers := make([]io.ReadSeeker, len(processed.Results))
for i, result := range processed.Results {
buffer := result.Output.(bytes.Buffer)
readers[i] = bytes.NewReader(buffer.Bytes())
b := result.Output.([]byte)
readers[i] = bytes.NewReader(b)
}
writer, _ := os.Create(d.file)
defer writer.Close()
Expand All @@ -112,14 +112,13 @@ func (d *document) Generate() error {
}
return writer.Close()
}
}*/

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

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

Expand Down
28 changes: 22 additions & 6 deletions pkg/v2/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,36 @@ package domain
import (
"github.com/johnfercher/go-tree/tree"
"github.com/johnfercher/maroto/internal"
"os"
)

type Document struct {
Bytes []byte
Report *Report
}

func (r *Document) Save(file string) error {
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()

_, err = f.Write(r.Bytes)
if err != nil {
return err
}

return nil
}

type Maroto interface {
Generate() error
Generate() (*Document, error)
ForceAddPage(pages ...Page)
Add(rows ...Row)
GetStructure() *tree.Node[Structure]
}

type MarotoMetrified interface {
Maroto
GenerateWithReport() (*Report, error)
}

type Node interface {
Render(fpdf Provider, ctx internal.Cell)
GetStructure() *tree.Node[Structure]
Expand Down
5 changes: 2 additions & 3 deletions pkg/v2/domain/provider.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package domain

import (
"bytes"
"github.com/johnfercher/maroto/internal"
"github.com/johnfercher/maroto/pkg/consts"
"github.com/johnfercher/maroto/pkg/props"
Expand All @@ -24,7 +23,7 @@ type Provider interface {
// General
GetDimensions() (width float64, height float64)
GetMargins() (left float64, top float64, right float64, bottom float64)
Generate(file string) error
GenerateAndOutput() (bytes.Buffer, error)
GenerateFile(file string) error
GenerateBytes() ([]byte, error)
SetCache(cache cache.Cache)
}
Loading

0 comments on commit 8ebd4a9

Please sign in to comment.