Skip to content

Commit

Permalink
keep
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher committed Sep 12, 2023
1 parent 0dd544c commit 5cc7521
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 66 deletions.
14 changes: 9 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import (
"github.com/johnfercher/maroto/pkg/v2/col"
"github.com/johnfercher/maroto/pkg/v2/image"
"github.com/johnfercher/maroto/pkg/v2/row"
"log"
)

func main() {
pdf := v2.NewDocument("pdfzin")
pdf := v2.NewDocument()

header := buildRow()
content := buildRow()
footer := buildRow()
//content := buildRow()

Check failure on line 15 in cmd/main.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
//footer := buildRow()

pdf.Add(header, content, footer)
pdf.Add(header /*content, footer*/)

pdf.Render()
err := pdf.Generate("v2.pdf")
if err != nil {
log.Fatal(err.Error())
}
}

func buildRow() v2.Component {
Expand Down
20 changes: 10 additions & 10 deletions pkg/v2/col/col.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package col

import (
"fmt"
"github.com/johnfercher/maroto/internal/fpdf"
"github.com/johnfercher/maroto/pkg/v2"

Check failure on line 5 in pkg/v2/col/col.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
)

Expand All @@ -18,21 +18,21 @@ func New(size int) *col {
}
}

func (d *col) Render() {
fmt.Println(d.size)
for _, component := range d.components {
component.Render()
func (c *col) Render(fpdf fpdf.Fpdf, ctx v2.Context) {
ctx.Print(c.size)
for _, component := range c.components {
component.Render(fpdf, ctx)
}
}

func (d *col) GetType() string {
return d._type.String()
func (c *col) GetType() string {
return c._type.String()
}

func (d *col) Add(components ...v2.Component) {
func (c *col) Add(components ...v2.Component) {
for _, component := range components {
if d._type.Accept(component.GetType()) {
d.components = append(d.components, component)
if c._type.Accept(component.GetType()) {
c.components = append(c.components, component)
}
}
}
4 changes: 3 additions & 1 deletion pkg/v2/component.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package v2

import "github.com/johnfercher/maroto/internal/fpdf"

type Component interface {
Render()
Render(fpdf fpdf.Fpdf, ctx Context)
GetType() string
Add(component ...Component)
}
47 changes: 47 additions & 0 deletions pkg/v2/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package v2

import "fmt"

type Context struct {
Coordinate *Coordinate
Dimensions *Dimensions
Margins *Margins
}

func (c *Context) Print(label interface{}) {
fmt.Println(label)
c.Margins.Print()
}

type Dimensions struct {
Width float64
Height float64
}

func (c *Dimensions) Print() {

Check failure on line 22 in pkg/v2/context.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
}

type Coordinate struct {
X float64
Y float64
}

func (c *Coordinate) Print() {

Check failure on line 31 in pkg/v2/context.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
}

type Margins struct {
Left float64
Right float64
Top float64
Bottom float64
}

func (c *Margins) Print() {
if c == nil {
return
}

fmt.Printf("Left: %1.f, Right: %1.f, Top: %1.f, Bottom: %1.f\n", c.Left, c.Right, c.Left, c.Bottom)
}
56 changes: 37 additions & 19 deletions pkg/v2/document.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,57 @@
package v2

import "fmt"
import (
"github.com/johnfercher/maroto/internal/fpdf"
"github.com/jung-kurt/gofpdf"
)

type Maroto interface {
Add(component ...Component)
Generate(file string) error
}

type document struct {
value string
ctx Context
_type DocumentType
fpdf fpdf.Fpdf
components []Component
}

func NewDocument(value string) *document {
func NewDocument() *document {
fpdf := gofpdf.NewCustom(&gofpdf.InitType{
OrientationStr: "P",
UnitStr: "mm",
SizeStr: "A4",
FontDirStr: "",
})

width, height := fpdf.GetPageSize()
left, top, right, bottom := fpdf.GetMargins()

return &document{
fpdf: fpdf,
_type: Document,
value: value,
ctx: Context{
Coordinate: &Coordinate{0, 0},
Dimensions: &Dimensions{width, height},
Margins: &Margins{left, right, top, bottom},
},
}
}

func (d *document) Render() {
fmt.Println(d.value)
for _, component := range d.components {
component.Render()
}
}

func (d *document) IsDrawable() bool {
return false
}

func (d *document) GetType() string {
return d._type.String()
}

func (d *document) Add(components ...Component) {
for _, component := range components {
if d._type.Accept(component.GetType()) {
d.components = append(d.components, component)
}
}
}

func (d *document) Generate(file string) error {
d.ctx.Print(d._type)
for _, component := range d.components {
component.Render(d.fpdf, d.ctx)
}

return d.fpdf.OutputFileAndClose(file)
}
16 changes: 8 additions & 8 deletions pkg/v2/image/image.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package image

import (
"fmt"
"github.com/johnfercher/maroto/internal/fpdf"
"github.com/johnfercher/maroto/pkg/v2"

Check failure on line 5 in pkg/v2/image/image.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
)

Expand All @@ -18,17 +18,17 @@ func New(path string) *image {
}
}

func (d *image) Render() {
fmt.Println(d.path)
for _, component := range d.components {
component.Render()
func (i *image) Render(fpdf fpdf.Fpdf, ctx v2.Context) {
ctx.Print(i.path)
for _, component := range i.components {
component.Render(fpdf, ctx)
}
}

func (d *image) GetType() string {
return d._type.String()
func (i *image) GetType() string {
return i._type.String()
}

func (d *image) Add(_ ...v2.Component) {
func (i *image) Add(_ ...v2.Component) {
return
}
37 changes: 22 additions & 15 deletions pkg/v2/row/row.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
package row

import (
"fmt"
"github.com/johnfercher/maroto/internal/fpdf"
"github.com/johnfercher/maroto/pkg/v2"

Check failure on line 5 in pkg/v2/row/row.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
)

type row struct {
height int
height float64
_type v2.DocumentType
components []v2.Component
}

func New(height int) *row {
func New(height float64) *row {
return &row{
_type: v2.Row,
height: height,
}
}

func (d *row) Render() {
fmt.Println(d.height)
for _, component := range d.components {
component.Render()
}
}

func (d *row) GetType() string {
return d._type.String()
func (r *row) GetType() string {
return r._type.String()
}

func (d *row) Add(components ...v2.Component) {
func (r *row) Add(components ...v2.Component) {
for _, component := range components {
if d._type.Accept(component.GetType()) {
d.components = append(d.components, component)
if r._type.Accept(component.GetType()) {
r.components = append(r.components, component)
}
}
}

func (r *row) Render(fpdf fpdf.Fpdf, ctx v2.Context) {
ctx.Print(r.height)
r.render(fpdf, ctx)
for _, component := range r.components {
component.Render(fpdf, ctx)
}
}

func (r *row) render(fpdf fpdf.Fpdf, ctx v2.Context) {
fpdf.AddPage()
fpdf.SetFont("Arial", "B", 16)
fpdf.CellFormat(ctx.Dimensions.Width-ctx.Margins.Left-ctx.Margins.Right, r.height, "", "1", 0, "C", false, 0, "")
}
16 changes: 8 additions & 8 deletions pkg/v2/text/text.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package text

import (
"fmt"
"github.com/johnfercher/maroto/internal/fpdf"
v2 "github.com/johnfercher/maroto/pkg/v2"
)

Expand All @@ -18,17 +18,17 @@ func New(value string) *text {
}
}

func (d *text) Render() {
fmt.Println(d.value)
for _, component := range d.components {
component.Render()
func (t *text) Render(fpdf fpdf.Fpdf, ctx v2.Context) {
ctx.Print(t.value)
for _, component := range t.components {
component.Render(fpdf, ctx)
}
}

func (d *text) GetType() string {
return d._type.String()
func (t *text) GetType() string {
return t._type.String()
}

func (d *text) Add(_ ...v2.Component) {
func (t *text) Add(_ ...v2.Component) {
return
}
Binary file added v2.pdf
Binary file not shown.

0 comments on commit 5cc7521

Please sign in to comment.