Skip to content

Commit

Permalink
feat: datamatrix code
Browse files Browse the repository at this point in the history
  • Loading branch information
kmpm authored Jun 23, 2021
1 parent 94f9fb3 commit 032684b
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type Code interface {
AddQr(code string, cell Cell, prop props.Rect)
AddBar(code string, cell Cell, prop props.Barcode) (err error)
AddDataMatrix(code string, cell Cell, prop props.Rect)
}

type code struct {
Expand All @@ -27,6 +28,18 @@ func NewCode(pdf fpdf.Fpdf, math Math) *code {
}
}

func (s *code) AddDataMatrix(code string, cell Cell, prop props.Rect) {
key := barcode.RegisterDataMatrix(s.pdf, code)

var x, y, w, h float64
if prop.Center {
x, y, w, h = s.math.GetRectCenterColProperties(cell.Width, cell.Width, cell.Width, cell.Height, cell.X, prop.Percent)
} else {
x, y, w, h = s.math.GetRectNonCenterColProperties(cell.Width, cell.Width, cell.Width, cell.Height, cell.X, prop)
}
barcode.Barcode(s.pdf, key, x, y+cell.Y, w, h, false)
}

// AddQr create a QrCode inside a cell
func (s *code) AddQr(code string, cell Cell, prop props.Rect) {
key := barcode.RegisterQR(s.pdf, code, qr.H, qr.Unicode)
Expand Down
104 changes: 104 additions & 0 deletions internal/examples/dmgrid/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"fmt"
"os"
"time"

"github.com/johnfercher/maroto/pkg/consts"
"github.com/johnfercher/maroto/pkg/pdf"
"github.com/johnfercher/maroto/pkg/props"
)

func main() {
begin := time.Now()
m := pdf.NewMaroto(consts.Portrait, consts.Letter)
m.SetBorder(true)

m.Row(40, func() {
m.Col(2, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Percent: 50,
})
})
m.Col(4, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Percent: 75,
})
})
m.Col(6, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Percent: 100,
})
})
})

m.Row(40, func() {
m.Col(2, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 50,
})
})
m.Col(4, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 75,
})
})
m.Col(6, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 100,
})
})
})

m.Row(40, func() {
m.Col(6, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Percent: 50,
})
})
m.Col(4, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Percent: 75,
})
})
m.Col(2, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Percent: 100,
})
})
})

m.Row(40, func() {
m.Col(6, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 50,
})
})
m.Col(4, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 75,
})
})
m.Col(2, func() {
m.DataMatrixCode("https://github.com/johnfercher/maroto", props.Rect{
Center: true,
Percent: 100,
})
})
})

err := m.OutputFileAndClose("internal/examples/pdfs/dmgrid.pdf")
if err != nil {
fmt.Println("Could not save PDF:", err)
os.Exit(1)
}

end := time.Now()
fmt.Println(end.Sub(begin))
}
Binary file added internal/examples/pdfs/dmgrid.pdf
Binary file not shown.
17 changes: 17 additions & 0 deletions pkg/pdf/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Maroto interface {
Base64Image(base64 string, extension consts.Extension, prop ...props.Rect) (err error)
Barcode(code string, prop ...props.Barcode) error
QrCode(code string, prop ...props.Rect)
DataMatrixCode(code string, prop ...props.Rect)
Signature(label string, prop ...props.Font)

// File System
Expand Down Expand Up @@ -480,6 +481,22 @@ func (s *PdfMaroto) Barcode(code string, prop ...props.Barcode) (err error) {

return
}
func (s *PdfMaroto) DataMatrixCode(code string, prop ...props.Rect) {
rectProp := props.Rect{}
if len(prop) > 0 {
rectProp = prop[0]
}
rectProp.MakeValid()

cell := internal.Cell{
X: s.xColOffset,
Y: s.offsetY + rectProp.Top,
Width: s.colWidth,
Height: s.rowHeight,
}

s.Code.AddDataMatrix(code, cell, rectProp)
}

// QrCode create a qrcode inside a cell.
func (s *PdfMaroto) QrCode(code string, prop ...props.Rect) {
Expand Down

0 comments on commit 032684b

Please sign in to comment.