Skip to content

Commit

Permalink
Update docs (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher authored Dec 18, 2023
1 parent 36939c4 commit 720515c
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 0 deletions.
30 changes: 30 additions & 0 deletions maroto.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type maroto struct {
pool async.Processor[[]core.Page, []byte]
}

// New is responsible for create a new instance of core.Maroto.
// It's optional to provide an *entity.Config with customizations
// those customization are created by using the config.Builder.
func New(cfgs ...*entity.Config) core.Maroto {
cache := cache.New()
cfg := getConfig(cfgs...)
Expand All @@ -68,6 +71,11 @@ func New(cfgs ...*entity.Config) core.Maroto {
return m
}

// AddPages is responsible for add pages directly in the document.
// By adding a page directly, the current cursor will reset and the
// new page will appear as the next. If the page provided have
// more rows than the maximum useful area of a page, maroto will split
// that page in more than one.
func (m *maroto) AddPages(pages ...core.Page) {
for _, page := range pages {
if m.currentHeight != m.headerHeight {
Expand All @@ -78,16 +86,30 @@ func (m *maroto) AddPages(pages ...core.Page) {
}
}

// AddRows is responsible for add rows in the current document.
// By adding a row, if the row will extrapolate the useful area of a page,
// maroto will automatically add a new page. Maroto use the information of
// PageSize, PageMargin, FooterSize and HeaderSize to calculate the useful
// area of a page.
func (m *maroto) AddRows(rows ...core.Row) {
m.addRows(rows...)
}

// AddRow is responsible for add one row in the current document.
// By adding a row, if the row will extrapolate the useful area of a page,
// maroto will automatically add a new page. Maroto use the information of
// PageSize, PageMargin, FooterSize and HeaderSize to calculate the useful
// area of a page.
func (m *maroto) AddRow(rowHeight float64, cols ...core.Col) core.Row {
r := row.New(rowHeight).Add(cols...)
m.addRow(r)
return r
}

// RegisterHeader is responsible to define a set of rows as a header
// of the document. The header will appear in every new page of the document.
// The header cannot occupy an area greater than the useful area of the page,
// it this case the method will return an error.
func (m *maroto) RegisterHeader(rows ...core.Row) error {
height := m.getRowsHeight(rows...)
if height+m.footerHeight > m.config.Dimensions.Height {
Expand All @@ -104,6 +126,10 @@ func (m *maroto) RegisterHeader(rows ...core.Row) error {
return nil
}

// RegisterFooter is responsible to define a set of rows as a footer
// of the document. The footer will appear in every new page of the document.
// The footer cannot occupy an area greater than the useful area of the page,
// it this case the method will return an error.
func (m *maroto) RegisterFooter(rows ...core.Row) error {
height := m.getRowsHeight(rows...)
if height > m.config.Dimensions.Height {
Expand All @@ -115,6 +141,8 @@ func (m *maroto) RegisterFooter(rows ...core.Row) error {
return nil
}

// Generate is responsible to compute the component tree created by
// the usage of all other Maroto methods, and generate the PDF document.
func (m *maroto) Generate() (core.Document, error) {
m.provider.SetProtection(m.config.Protection)
m.provider.SetCompression(m.config.Compression)
Expand All @@ -130,6 +158,8 @@ func (m *maroto) Generate() (core.Document, error) {
return m.generate()
}

// GetStructure is responsible for return the component tree, this is useful
// on unit tests cases.
func (m *maroto) GetStructure() *node.Node[core.Structure] {
m.fillPageToAddNew()

Expand Down
2 changes: 2 additions & 0 deletions metricsdecorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type metricsDecorator struct {
inner core.Maroto
}

// NewMetricsDecorator is responsible to create the metrics decorator
// for the maroto instance.
func NewMetricsDecorator(inner core.Maroto) core.Maroto {
return &metricsDecorator{
inner: inner,
Expand Down
3 changes: 3 additions & 0 deletions pkg/components/code/barcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type barcode struct {
config *entity.Config
}

// NewBar is responsible to create an instance of a Barcode.
func NewBar(code string, ps ...props.Barcode) core.Component {
prop := props.Barcode{}
if len(ps) > 0 {
Expand All @@ -29,11 +30,13 @@ func NewBar(code string, ps ...props.Barcode) core.Component {
}
}

// NewBarCol is responsible to create an instance of a Barcode wrapped in a Col.
func NewBarCol(size int, code string, ps ...props.Barcode) core.Col {
bar := NewBar(code, ps...)
return col.New(size).Add(bar)
}

// NewBarRow is responsible to create an instance of a Barcode wrapped in a Row.
func NewBarRow(height float64, code string, ps ...props.Barcode) core.Row {
bar := NewBar(code, ps...)
c := col.New().Add(bar)
Expand Down
3 changes: 3 additions & 0 deletions pkg/components/code/matrixcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type matrixCode struct {
config *entity.Config
}

// NewMatrix is responsible to create an instance of a MatrixCode.
func NewMatrix(code string, barcodeProps ...props.Rect) core.Component {
prop := props.Rect{}
if len(barcodeProps) > 0 {
Expand All @@ -29,11 +30,13 @@ func NewMatrix(code string, barcodeProps ...props.Rect) core.Component {
}
}

// NewMatrixCol is responsible to create an instance of a MatrixCode wrapped in a Col.
func NewMatrixCol(size int, code string, ps ...props.Rect) core.Col {
matrixCode := NewMatrix(code, ps...)
return col.New(size).Add(matrixCode)
}

// NewMatrixRow is responsible to create an instance of a MatrixCode wrapped in a Row.
func NewMatrixRow(height float64, code string, ps ...props.Rect) core.Row {
matrixCode := NewMatrix(code, ps...)
c := col.New().Add(matrixCode)
Expand Down
3 changes: 3 additions & 0 deletions pkg/components/code/qrcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type qrCode struct {
config *entity.Config
}

// NewQr is responsible to create an instance of a QrCode.
func NewQr(code string, barcodeProps ...props.Rect) core.Component {
prop := props.Rect{}
if len(barcodeProps) > 0 {
Expand All @@ -29,11 +30,13 @@ func NewQr(code string, barcodeProps ...props.Rect) core.Component {
}
}

// NewQrCol is responsible to create an instance of a QrCode wrapped in a Col.
func NewQrCol(size int, code string, ps ...props.Rect) core.Col {
qrCode := NewQr(code, ps...)
return col.New(size).Add(qrCode)
}

// NewQrRow is responsible to create an instance of a QrCode wrapped in a Row.
func NewQrRow(height float64, code string, ps ...props.Rect) core.Row {
qrCode := NewQr(code, ps...)
c := col.New().Add(qrCode)
Expand Down
1 change: 1 addition & 0 deletions pkg/components/col/col.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type col struct {
style *props.Cell
}

// New is responsible to create an instance of core.Col.
func New(size ...int) core.Col {
if len(size) == 0 {
return &col{isMax: true}
Expand Down
3 changes: 3 additions & 0 deletions pkg/components/image/bytesimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type bytesImage struct {
config *entity.Config
}

// NewFromBytes is responsible to create an instance of an Image.
func NewFromBytes(bytes []byte, extension extension.Type, ps ...props.Rect) core.Component {
prop := props.Rect{}
if len(ps) > 0 {
Expand All @@ -32,11 +33,13 @@ func NewFromBytes(bytes []byte, extension extension.Type, ps ...props.Rect) core
}
}

// NewFromBytesCol is responsible to create an instance of an Image wrapped in a Col.
func NewFromBytesCol(size int, bytes []byte, extension extension.Type, ps ...props.Rect) core.Col {
image := NewFromBytes(bytes, extension, ps...)
return col.New(size).Add(image)
}

// NewFromBytesRow is responsible to create an instance of an Image wrapped in a Row.
func NewFromBytesRow(height float64, bytes []byte, extension extension.Type, ps ...props.Rect) core.Row {
image := NewFromBytes(bytes, extension, ps...)
c := col.New().Add(image)
Expand Down
3 changes: 3 additions & 0 deletions pkg/components/image/fileimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type fileImage struct {
config *entity.Config
}

// NewFromFile is responsible to create an instance of an Image.
func NewFromFile(path string, ps ...props.Rect) core.Component {
prop := props.Rect{}
if len(ps) > 0 {
Expand All @@ -29,11 +30,13 @@ func NewFromFile(path string, ps ...props.Rect) core.Component {
}
}

// NewFromFileCol is responsible to create an instance of an Image wrapped in a Col.
func NewFromFileCol(size int, path string, ps ...props.Rect) core.Col {
image := NewFromFile(path, ps...)
return col.New(size).Add(image)
}

// NewFromFileRow is responsible to create an instance of an Image wrapped in a Row.
func NewFromFileRow(height float64, path string, ps ...props.Rect) core.Row {
image := NewFromFile(path, ps...)
c := col.New().Add(image)
Expand Down
3 changes: 3 additions & 0 deletions pkg/components/line/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type line struct {
prop props.Line
}

// New is responsible to create an instance of a Line.
func New(ps ...props.Line) core.Component {
lineProp := props.Line{}
if len(ps) > 0 {
Expand All @@ -26,11 +27,13 @@ func New(ps ...props.Line) core.Component {
}
}

// NewCol is responsible to create an instance of a Line wrapped in a Col.
func NewCol(size int, ps ...props.Line) core.Col {
r := New(ps...)
return col.New(size).Add(r)
}

// NewRow is responsible to create an instance of a Line wrapped in a Row.
func NewRow(height float64, ps ...props.Line) core.Row {
r := New(ps...)
c := col.New().Add(r)
Expand Down
9 changes: 9 additions & 0 deletions pkg/components/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import (
"github.com/johnfercher/maroto/v2/pkg/core"
)

// Listable is the main abstraction of a listable item in a TableList.
// A collection of objects that implements this interface may be added
// in a list.
type Listable interface {
GetHeader() core.Row
GetContent(i int) core.Row
}

// BuildFromPointer is responsible to receive a collection of objects that implements
// Listable and build the rows of TableList. This method should be used in case of a collection
// of pointers.
func BuildFromPointer[T Listable](arr []*T) ([]core.Row, error) {
if len(arr) == 0 {
return nil, errors.New("empty array")
Expand All @@ -27,6 +33,9 @@ func BuildFromPointer[T Listable](arr []*T) ([]core.Row, error) {
return Build(list)
}

// Build is responsible to receive a collection of objects that implements
// Listable and build the rows of TableList. This method should be used in case of a collection
// of values.
func Build[T Listable](arr []T) ([]core.Row, error) {
if len(arr) == 0 {
return nil, errors.New("empty array")
Expand Down
1 change: 1 addition & 0 deletions pkg/components/page/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type page struct {
prop props.Page
}

// New is responsible to create a core.Page.
func New(ps ...props.Page) core.Page {
prop := props.Page{}
if len(ps) > 0 {
Expand Down
1 change: 1 addition & 0 deletions pkg/components/row/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type row struct {
config *entity.Config
}

// New is responsible to create a core.Row.
func New(height float64) core.Row {
return &row{
height: height,
Expand Down
3 changes: 3 additions & 0 deletions pkg/components/signature/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type signature struct {
config *entity.Config
}

// New is responsible to create an instance of a Signature.
func New(value string, ps ...props.Signature) core.Component {
prop := props.Signature{}
if len(ps) > 0 {
Expand All @@ -30,11 +31,13 @@ func New(value string, ps ...props.Signature) core.Component {
}
}

// NewCol is responsible to create an instance of a Signature wrapped in a Col.
func NewCol(size int, value string, ps ...props.Signature) core.Col {
signature := New(value, ps...)
return col.New(size).Add(signature)
}

// NewRow is responsible to create an instance of a Signature wrapped in a Row.
func NewRow(height float64, value string, ps ...props.Signature) core.Row {
signature := New(value, ps...)
c := col.New().Add(signature)
Expand Down
3 changes: 3 additions & 0 deletions pkg/components/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type text struct {
config *entity.Config
}

// New is responsible to create an instance of a Text.
func New(value string, ps ...props.Text) core.Component {
textProp := props.Text{}
if len(ps) > 0 {
Expand All @@ -27,11 +28,13 @@ func New(value string, ps ...props.Text) core.Component {
}
}

// NewCol is responsible to create an instance of a Text wrapped in a Col.
func NewCol(size int, value string, ps ...props.Text) core.Col {
text := New(value, ps...)
return col.New(size).Add(text)
}

// NewRow is responsible to create an instance of a Text wrapped in a Row.
func NewRow(height float64, value string, ps ...props.Text) core.Row {
r := New(value, ps...)
c := col.New().Add(r)
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/johnfercher/maroto/v2/pkg/props"
)

// Builder is the abstraction responsible for global customizations on the document.
type Builder interface {
WithPageSize(size pagesize.Type) Builder
WithDimensions(width float64, height float64) Builder
Expand Down Expand Up @@ -59,6 +60,7 @@ type builder struct {
backgroundImage *entity.Image
}

// NewBuilder is responsible to create an instance of Builder.
func NewBuilder() Builder {
return &builder{
providerType: provider.Gofpdf,
Expand Down

0 comments on commit 720515c

Please sign in to comment.