pdf

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Maroto

type Maroto interface {
	// Grid System
	Row(height float64, closure func())
	Col(width uint, closure func())
	ColSpace(gridSize uint)

	// Registers
	RegisterHeader(closure func())
	RegisterFooter(closure func())

	// Outside Col/Row Components
	TableList(header []string, contents [][]string, prop ...props.TableList)
	Line(spaceHeight float64, prop ...props.Line)

	// Inside Col/Row Components
	Text(text string, prop ...props.Text)
	FileImage(filePathName string, prop ...props.Rect) (err error)
	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
	OutputFileAndClose(filePathName string) error
	Output() (bytes.Buffer, error)

	// Helpers
	AddPage()
	SetBorder(on bool)
	SetBackgroundColor(color color.Color)
	SetAliasNbPages(alias string)
	SetFirstPageNb(number int)
	GetBorder() bool
	GetPageSize() (width float64, height float64)
	GetCurrentPage() int
	GetCurrentOffset() float64
	SetPageMargins(left, top, right float64)
	GetPageMargins() (left float64, top float64, right float64, bottom float64)
	SetCompression(compress bool)

	// Fonts
	AddUTF8Font(familyStr string, styleStr consts.Style, fileStr string)
	SetFontLocation(fontDirStr string)
	SetProtection(actionFlag byte, userPassStr, ownerPassStr string)
	SetDefaultFontFamily(fontFamily string)
	GetDefaultFontFamily() string
}

Maroto is the principal abstraction to create a PDF document.

func NewMaroto

func NewMaroto(orientation consts.Orientation, pageSize consts.PageSize) Maroto

NewMaroto create a Maroto instance returning a pointer to PdfMaroto Receive an Orientation and a PageSize. Shorthand when using a preset page size from consts.PageSize.

Example

ExampleNewMaroto demonstrates how to create maroto.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Do things
	m.GetPageMargins()

	// Do more things and save...
}
Output:

func NewMarotoCustomSize

func NewMarotoCustomSize(orientation consts.Orientation, pageSize consts.PageSize, unitStr string, width, height float64) Maroto

NewMarotoCustomSize creates a Maroto instance returning a pointer to PdfMaroto Receive an Orientation and a PageSize. Use if custom page size is needed. Otherwise use NewMaroto() shorthand if using page sizes from consts.Pagesize. If using custom width and height, pageSize is just a string value for the format and takes no effect. Width and height inputs are measurements of the page in Portrait orientation.

Example

ExampleNewMaroto demonstrates how to create maroto with custom page size.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMarotoCustomSize(consts.Landscape, "C6", "mm", 114.0, 162.0)

	// Do things
	m.GetPageMargins()

	// Do more things and save...
}
Output:

type PdfMaroto

type PdfMaroto struct {
	// Gofpdf wrapper.
	Pdf fpdf.Fpdf

	// Components.
	Math            internal.Math
	Font            internal.Font
	TextHelper      internal.Text
	SignHelper      internal.Signature
	Image           internal.Image
	Code            internal.Code
	TableListHelper internal.TableList
	LineHelper      internal.Line
	// contains filtered or unexported fields
}

PdfMaroto is the principal structure which implements Maroto abstraction.

func (*PdfMaroto) AddPage

func (s *PdfMaroto) AddPage()

AddPage adds a new page in the PDF.

Example

ExamplePdfMaroto_AddPage how to force add a new page.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Add rows, cols and components
	m.AddPage()

	// Add rows, col and components in a new page
	// Do more things and save...
}
Output:

func (*PdfMaroto) AddUTF8Font

func (s *PdfMaroto) AddUTF8Font(familyStr string, styleStr consts.Style, fileStr string)

AddUTF8Font add a custom utf8 font. familyStr is the name of the custom font registered in maroto. styleStr is the style of the font and fileStr is the path to the .ttf file.

Example

ExamplePdfMaroto_AddUTF8Font demonstrates how to add a custom utf8 font.

package main

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

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Define font to all styles.
	m.AddUTF8Font("CustomArial", consts.Normal, "internal/assets/fonts/arial-unicode-ms.ttf")
	m.AddUTF8Font("CustomArial", consts.Italic, "internal/assets/fonts/arial-unicode-ms.ttf")
	m.AddUTF8Font("CustomArial", consts.Bold, "internal/assets/fonts/arial-unicode-ms.ttf")
	m.AddUTF8Font("CustomArial", consts.BoldItalic, "internal/assets/fonts/arial-unicode-ms.ttf")

	m.Row(10, func() {
		m.Col(12, func() {
			// Use style.
			m.Text("CustomUtf8Font", props.Text{
				Family: "CustomArial",
			})
		})
	})

	// Do more things and save...
}
Output:

func (*PdfMaroto) Barcode

func (s *PdfMaroto) Barcode(code string, prop ...props.Barcode) (err error)

Barcode create an barcode inside a cell.

Example

ExamplePdfMaroto_Barcode demonstrates how to place a barcode inside a Col.

package main

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

func main() {
	// Passing nil on barcode props parameter implies the Barcode fills it's
	// context cell depending on it's size.
	// It's possible to define the barcode positioning through
	// the top and left parameters unless center parameter is true.
	// In brief, when center parameter equals true, left and top parameters has no effect.
	// Percent parameter represents the Barcode's width/height inside the cell.
	// i.e. Percent: 75 means that the Barcode will take up 75% of Col's width
	// There is a constraint in the proportion defined, height cannot be greater than 20% of
	// the width, and height cannot be smaller than 10% of the width.

	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Do a lot of things on rows and columns...

	m.Col(12, func() {
		_ = m.Barcode("https://github.com/johnfercher/maroto", props.Barcode{
			Percent:    75,
			Proportion: props.Proportion{Width: 50, Height: 10},
			Center:     true,
		})
	})

	// do more things...
}
Output:

func (*PdfMaroto) Base64Image

func (s *PdfMaroto) Base64Image(base64 string, extension consts.Extension, prop ...props.Rect) error

Base64Image add an Image reading byte slices inside a cell. Defining Image properties.

Example

ExamplePdfMaroto_Base64Image demonstrates how to add an Image from a base64 string.

package main

import (
	"encoding/base64"

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

func main() {
	// When props.Rect is nil, method make Image fulfill the context cell.
	// When center is true, left and top has no effect.
	// Percent represents the width/height of the Image inside the cell,
	// Ex: 85, means that Image will have width of 85% of column width.
	// When center is false, is possible to manually positioning the Image.

	m := pdf.NewMaroto(consts.Portrait, consts.A4)
	rowHeight := 5.0

	// Bytes of the image loaded
	bytes := []byte{1, 2, 3}
	base64String := base64.StdEncoding.EncodeToString(bytes)

	m.Row(rowHeight, func() {
		m.Col(12, func() {
			_ = m.Base64Image(base64String, consts.Png, props.Rect{
				Left:    5,
				Top:     5,
				Center:  true,
				Percent: 85,
			})
		})
	})

	// Do more things and save...
}
Output:

func (*PdfMaroto) Col

func (s *PdfMaroto) Col(width uint, closure func())

Col create a column inside a row and enable to add components inside. Maroto do not support recursive columns or rows inside columns.

Example

ExamplePdfMaroto_Col demonstrates how to add an useful column.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)
	rowHeight := 5.0

	// Warning: The sum of all column gridSize cannot extrapolate 12
	// Warning: There is no way to use a row inside a row or a row inside a col.
	m.Row(rowHeight, func() {
		m.Col(12, func() {
			// Add Image, Text, Signature, QrCode or Barcode...
		})
	})

	// Warning: The sum of all column gridSize cannot extrapolate 12
	// Warning: There is no way to use a row inside a row or a row inside a col.
	m.Row(rowHeight, func() {
		m.Col(6, func() {
			// Add Image, Text, Signature, QrCode or Barcode...
		})
		m.Col(6, func() {
			// Add Image, Text, Signature, QrCode or Barcode...
		})
	})

	// Do more things and save...
}
Output:

func (*PdfMaroto) ColSpace

func (s *PdfMaroto) ColSpace(gridSize uint)

ColSpace create an empty column inside a row.

Example

ExamplePdfMaroto_ColSpace demonstrates how to add an empty column inside a row.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)
	rowHeight := 5.0

	// Warning: The sum of all column gridSize cannot extrapolate 12
	m.Row(rowHeight, func() {
		m.ColSpace(12)
	})

	// Warning: The sum of all column gridSize cannot extrapolate 12
	m.Row(rowHeight, func() {
		m.ColSpace(6)
		m.ColSpace(6)
	})

	// Do more things and save...
}
Output:

func (*PdfMaroto) DataMatrixCode

func (s *PdfMaroto) DataMatrixCode(code string, prop ...props.Rect)

DataMatrixCode creates an datamatrix code inside a cell.

Example

ExamplePdfMaroto_DataMatrixCode demonstrates how to add a DataMatrixCode inside a Col.

package main

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

func main() {
	// Passing nil on rectProps makes
	// the DataMatrixCode fill the context cell.
	// When center is true, left and top has no effect.
	// Percent represents the width/height of the DataMatrixCode inside the cell.
	// i.e. 80 means that the DataMatrixCode will take up 80% of Col's width
	// When center is false, positioning of the DataMatrixCode can be done through
	// left and top.

	m := pdf.NewMaroto(consts.Portrait, consts.A4)
	rowHeight := 5.0

	m.Row(rowHeight, func() {
		m.Col(12, func() {
			m.DataMatrixCode("https://godoc.org/github.com/johnfercher/maroto", props.Rect{
				Left:    5,
				Top:     5,
				Center:  false,
				Percent: 80,
			})
		})
	})
}
Output:

func (*PdfMaroto) FileImage

func (s *PdfMaroto) FileImage(filePathName string, prop ...props.Rect) error

FileImage add an Image reading from disk inside a cell. Defining Image properties.

Example

ExamplePdfMaroto_FileImage demonstrates how add an Image reading from disk.

package main

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

func main() {
	// When props.Rect is nil, method make Image fulfill the context cell.
	// When center is true, left and top has no effect.
	// Percent represents the width/height of the Image inside the cell,
	// Ex: 85, means that Image will have width of 85% of column width.
	// When center is false, is possible to manually positioning the Image.

	m := pdf.NewMaroto(consts.Portrait, consts.A4)
	rowHeight := 5.0

	m.Row(rowHeight, func() {
		m.Col(12, func() {
			_ = m.FileImage("path/Image.jpg", props.Rect{
				Left:    5,
				Top:     5,
				Center:  true,
				Percent: 85,
			})
		})
	})

	// Do more things and save...
}
Output:

func (*PdfMaroto) GetBorder

func (s *PdfMaroto) GetBorder() bool

GetBorder return the actual border value.

Example

ExamplePdfMaroto_GetBorder demonstrates how to obtain the actual borders status.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// false.
	_ = m.GetBorder()

	m.SetBorder(true)

	// true.
	_ = m.GetBorder()

	// Do more things and save...
}
Output:

func (*PdfMaroto) GetCurrentOffset

func (s *PdfMaroto) GetCurrentOffset() float64

GetCurrentOffset obtain the current offset in y axis.

Example

ExamplePdfMaroto_GetCurrentOffset demonstrates how to obtain the current write offset i.e the height of cursor adding content in the pdf.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Offset here will be 0.
	_ = m.GetCurrentOffset()

	// Add Rows, Cols and Components until maroto add a new page.

	// Offset here will not be 0.
	_ = m.GetCurrentOffset()

	// Add Rows, Cols and Components to maroto add a new page.

	// Offset here will be 0.
	_ = m.GetCurrentOffset()

	// Do more things and save...
}
Output:

func (*PdfMaroto) GetCurrentPage

func (s *PdfMaroto) GetCurrentPage() int

GetCurrentPage obtain the current page index this can be used inside a RegisterFooter/RegisterHeader to draw the current page, or to another purposes.

Example

ExamplePdfMaroto_GetCurrentPage demonstrates how to obtain the current page index.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Index here will be 0.
	_ = m.GetCurrentPage()

	// Add Rows, Cols and Components.

	// Index here will not be 0.
	_ = m.GetCurrentPage()

	// Do more things and save...
}
Output:

func (*PdfMaroto) GetDefaultFontFamily

func (s *PdfMaroto) GetDefaultFontFamily() string

GetDefaultFontFamily allows you to get the current default font family.

Example

ExamplePdfMaroto_AddUTF8Font demonstrates how to obtain the current default font family.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Will return Arial.
	_ = m.GetDefaultFontFamily()

	// Change the default to Courier.
	m.SetDefaultFontFamily(consts.Courier)

	// Will return Courier.
	_ = m.GetDefaultFontFamily()

	// Do more things and save...
}
Output:

func (*PdfMaroto) GetPageMargins

func (s *PdfMaroto) GetPageMargins() (left float64, top float64, right float64, bottom float64)

GetPageMargins returns the set page margins. Comes in order of Left, Top, Right, Bottom Default page margins is left: 10, top: 10, right: 10.

Example

ExamplePdfMaroto_GetPageMargins demonstrates how to obtain the current page margins.

package main

import (
	"fmt"

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

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Get
	left, top, right, bottom := m.GetPageMargins()
	fmt.Println(left)
	fmt.Println(top)
	fmt.Println(right)
	fmt.Println(bottom)

	// Do more things and save...
}
Output:

func (*PdfMaroto) GetPageSize

func (s *PdfMaroto) GetPageSize() (width float64, height float64)

GetPageSize return the actual page size.

Example

ExamplePdfMaroto_GetPageSize demonstrates how to obtain the current page size (width and height).

package main

import (
	"fmt"

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

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Get
	width, height := m.GetPageSize()
	fmt.Println(width)
	fmt.Println(height)

	// Do more things and save...
}
Output:

func (*PdfMaroto) Line

func (s *PdfMaroto) Line(spaceHeight float64, prop ...props.Line)

Line draw a line from margin left to margin right in the current row.

Example

ExamplePdfMaroto_Line demonstrates how to draw a line separator.

package main

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

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Not passing Prop.Color make line as black.
	// Not passing width make width as 0.1.
	// Not passing style make style as solid.
	m.Line(1.0, props.Line{
		Color: color.Color{
			Red:   255,
			Green: 100,
			Blue:  50,
		},
		Style: consts.Dotted,
		Width: 1.0,
	})

	// Do more things and save...
}
Output:

func (*PdfMaroto) Output

func (s *PdfMaroto) Output() (bytes.Buffer, error)

Output extract PDF in byte slices.

Example

ExamplePdfMaroto_Output demonstrates how to get a base64 string from PDF.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Do a lot of things on rows and columns...

	_, err := m.Output()
	if err != nil {
		return
	}
}
Output:

func (*PdfMaroto) OutputFileAndClose

func (s *PdfMaroto) OutputFileAndClose(filePathName string) (err error)

OutputFileAndClose save pdf in disk.

Example

ExamplePdfMaroto_OutputFileAndClose demonstrates how to save a PDF object into disk.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Do a lot of things on rows and columns...

	err := m.OutputFileAndClose("path/file.pdf")
	if err != nil {
		return
	}
}
Output:

func (*PdfMaroto) QrCode

func (s *PdfMaroto) QrCode(code string, prop ...props.Rect)

QrCode create a qrcode inside a cell.

Example

ExamplePdfMaroto_QrCode demonstrates how to add a QR Code inside a Col.

package main

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

func main() {
	// Passing nil on rectProps makes
	// the QR Code fills the context cell.
	// When center is true, left and top has no effect.
	// Percent represents the width/height of the QR Code inside the cell.
	// i.e. 80 means that the QR Code will take up 80% of Col's width
	// When center is false, positioning of the QR Code can be done through
	// left and top.

	m := pdf.NewMaroto(consts.Portrait, consts.A4)
	rowHeight := 5.0

	m.Row(rowHeight, func() {
		m.Col(12, func() {
			m.QrCode("https://godoc.org/github.com/johnfercher/maroto", props.Rect{
				Left:    5,
				Top:     5,
				Center:  false,
				Percent: 80,
			})
		})
	})
}
Output:

func (*PdfMaroto) RegisterFooter

func (s *PdfMaroto) RegisterFooter(closure func())

RegisterFooter define a sequence of Rows, Lines ou TableLists which will be added in every new page.

Example

ExamplePdfMaroto_RegisterFooter demonstrates how to register footer.

package main

import (
	"time"

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

func main() {
	// For register footer you need to call method RegisterFooter
	// that receives a closure.
	// In this closure you are free to set any components you want to compose
	// your footer.
	// In this example there is a signature and a text with right align.
	// It is important to remember that it is recommended to create Row's and
	// Col's if necessary.
	// You have to register the footer immediately after the Maroto
	// All footers will be rendered at the bottom of all pages

	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	m.RegisterFooter(func() {
		m.Row(10, func() {
			m.Col(6, func() {
				m.Signature("lorem ipsum dolor")
			})
			m.Col(6, func() {
				m.Text(time.Now().Format("02-January-2006"), props.Text{Align: consts.Right})
			})
		})
	})

	// Do more things or not and save...
}
Output:

func (*PdfMaroto) RegisterHeader

func (s *PdfMaroto) RegisterHeader(closure func())

RegisterHeader define a sequence of Rows, Lines ou TableLists which will be added in every new page.

Example

ExamplePdfMaroto_RegisterHeader demonstrates how to register header.

package main

import (
	"time"

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

func main() {
	// For register header in Maroto you need to call method RegisterHeader
	// that receives a closure.
	// In this closure you are free to set any components you want to compose
	// your header.
	// In this example there is a two texts with different props and one image.
	// It is important to remember that it is recommended to create Row's and
	// Col's if necessary.
	// You have to register the header immediately after the Maroto

	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	m.RegisterHeader(func() {
		m.Row(10, func() {
			m.Col(3, func() {
				m.Text("lorem ipsum dolor", props.Text{Align: consts.Left})
			})
			m.Col(3, func() {
				_ = m.FileImage("internal/assets/images/frontpage.png")
			})
			m.Col(3, func() {
				m.Text(time.Now().Format("02-January-2006"),
					props.Text{Align: consts.Right})
			})
		})
	})

	// Do more things or not and save...
}
Output:

func (*PdfMaroto) Row

func (s *PdfMaroto) Row(height float64, closure func())

Row define a row and enable add columns inside the row. Maroto do not support recursive rows or rows inside columns.

Example

ExamplePdfMaroto_Row demonstrates how to define a row.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)
	rowHeight := 5.0

	// Warning: There is no way to use a row inside a row or a row inside a col.
	m.Row(rowHeight, func() {
		m.Col(12, func() {
			// Add a component
		})
	})

	// Warning: There is no way to use a row inside a row or a row inside a col.
	m.Row(rowHeight, func() {
		m.Col(12, func() {
			// Add another component
		})
	})

	// Do more things and save...
}
Output:

func (*PdfMaroto) SetAliasNbPages

func (s *PdfMaroto) SetAliasNbPages(alias string)

SetAliasNbPages Defines an alias for the total number of pages. It will be substituted as the document is closed.

Example

ExamplePdfMaroto_SetAliasNbPages demonstrates how to use SetAliasNbPages method.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Ths will create placeholder "{nbs}" to use in texts for total count of pages.
	m.SetAliasNbPages("{nbs}")

	// This will create a row with full width column and inside it a text that will display the total number of pages.
	m.Row(10, func() {
		m.Col(12, func() {
			m.Text("Total number of pages: {nbs}")
		})
	})
}
Output:

func (*PdfMaroto) SetBackgroundColor

func (s *PdfMaroto) SetBackgroundColor(color color.Color)

SetBackgroundColor define the background color of the PDF. This method can be used to toggle background from rows.

Example

ExamplePdfMaroto_SetBackgroundColor demonstrates how to use the SetBackgroundColor method.

package main

import (
	"github.com/johnfercher/maroto/pkg/color"
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	m.SetBackgroundColor(color.Color{
		Red:   100,
		Green: 20,
		Blue:  30,
	})

	// This Row will be filled with the color.
	m.Row(20, func() {
		m.Col(12, func() {
			// Add components.
		})
	})

	m.SetBackgroundColor(color.NewWhite())
	// Note: The default value is White (255, 255, 255), if maroto see this value it will ignore not will the cell with any color.

	// This Row will not be filled with the color.
	m.Row(20, func() {
		m.Col(12, func() {
			// Add components.
		})
	})

	// Do more things and save...
}
Output:

func (*PdfMaroto) SetBorder

func (s *PdfMaroto) SetBorder(on bool)

SetBorder enable the draw of lines in every cell. Draw borders in all columns created.

Example

ExamplePdfMaroto_SetBorder demonstrates how to enable the line drawing in every cell.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)
	m.SetBorder(true)

	// Add some Rows, Cols, Lines and etc...
	// Here will be drawn borders in every cell.

	m.SetBorder(false)

	// Add some Rows, Cols, Lines and etc...
	// Here will not be drawn borders.

	// Do more things and save...
}
Output:

func (*PdfMaroto) SetCompression

func (s *PdfMaroto) SetCompression(compress bool)

SetCompression allows to set/unset compression for a page Compression is on by default.

Example

ExamplePdfMaroto_SetCompression demonstrates how to disable compression By default compression is enabled.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	m.SetCompression(false)

	// Do more things and save...
}
Output:

func (*PdfMaroto) SetDefaultFontFamily

func (s *PdfMaroto) SetDefaultFontFamily(fontFamily string)

SetDefaultFontFamily allows you to customize the default font. By default Arial is the original value.

func (*PdfMaroto) SetFirstPageNb

func (s *PdfMaroto) SetFirstPageNb(number int)

SetFirstPageNb define first page number Default: 0.

Example

ExamplePdfMaroto_SetFirstPageNb demonstrates how to use SetFirstPageNb method.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Ths will set first page index to 1.
	m.SetFirstPageNb(1)
}
Output:

func (*PdfMaroto) SetFontLocation

func (s *PdfMaroto) SetFontLocation(fontDirStr string)

SetFontLocation allows you to change the fonts lookup location. fontDirStr is an absolute path where the fonts should be located.

Example

ExamplePdfMaroto_SetFontLocation demonstrates how to add a custom utf8 font from custom location.

package main

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

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	// Define custom location. It might be an absolute path as well as a relative path.
	m.SetFontLocation("internal/assets/fonts/")

	// Define font to all styles.
	m.AddUTF8Font("CustomArial", consts.Normal, "arial-unicode-ms.ttf")
	m.AddUTF8Font("CustomArial", consts.Italic, "arial-unicode-ms.ttf")
	m.AddUTF8Font("CustomArial", consts.Bold, "arial-unicode-ms.ttf")
	m.AddUTF8Font("CustomArial", consts.BoldItalic, "arial-unicode-ms.ttf")

	m.Row(10, func() {
		m.Col(12, func() {
			// Use style.
			m.Text("CustomUtf8Font", props.Text{
				Family: "CustomArial",
			})
		})
	})

	// Do more things and save...
}
Output:

func (*PdfMaroto) SetPageMargins

func (s *PdfMaroto) SetPageMargins(left, top, right float64)

SetPageMargins overrides default margins (10,10,10) the new page margin will affect all PDF pages.

Example

ExamplePdfMaroto_SetPageMargins demonstrates how to set custom page margins.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	m.SetPageMargins(10, 60, 10)

	// Do more things or not and save...
}
Output:

func (*PdfMaroto) SetProtection

func (s *PdfMaroto) SetProtection(actionFlag byte, userPassStr, ownerPassStr string)

SetProtection define a password to open the pdf.

Example

ExamplePdfMaroto_SetProtection demonstrates how to define a protection to pdf.

package main

import (
	"github.com/johnfercher/maroto/pkg/consts"
	"github.com/johnfercher/maroto/pkg/pdf"
)

func main() {
	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	m.SetProtection(1, "userPassword", "ownerPassword")

	// Do more things and save...
}
Output:

func (*PdfMaroto) SetTopPageMargin

func (s *PdfMaroto) SetTopPageMargin(top float64)

func (*PdfMaroto) Signature

func (s *PdfMaroto) Signature(label string, prop ...props.Font)

Signature add a space for a signature inside a cell, the space will have a line and a text below.

Example

ExamplePdfMaroto_Signature demonstrates how to add a Signature space inside a col.

package main

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

func main() {
	// Not passing this font prop will lead the method to use all the follow values.
	// Not passing family, make method use arial.
	// Not passing style, make method use normal.
	// Not passing size, make method use 10.0.
	// Not passing color, make method use the current color.

	m := pdf.NewMaroto(consts.Portrait, consts.A4)
	rowHeight := 5.0

	m.Row(rowHeight, func() {
		m.Col(12, func() {
			m.Signature("LabelForSignature", props.Font{
				Size:   12.0,
				Style:  consts.BoldItalic,
				Family: consts.Courier,
				Color: color.Color{
					Red:   10,
					Green: 20,
					Blue:  30,
				},
			})
		})
	})

	// Do more things and save...
}
Output:

func (*PdfMaroto) TableList

func (s *PdfMaroto) TableList(header []string, contents [][]string, prop ...props.TableList)

TableList create a table with multiple rows and columns, so is not possible use this component inside a row or inside a column. Headers define the amount of columns from each row. Headers have bold style, and localized at the top of table. Contents are array of arrays. Each array is one line.

Example

ExamplePdfMaroto_TableList demonstrates how to add a table with multiple rows and columns.

package main

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

func main() {
	// Not passing this table list prop will lead the method to use all the follow values.
	// Not passing HeaderProp.Size, make the method use 10.
	// Not passing HeaderProp.Family, make the method use arial.
	// Not passing HeaderProp.Style, make the method use bold.
	// Not passing HeaderProp.GridSizes, make the method use an array with same length
	// Not passing HeaderProp.Color, make the method use a black font
	// of header array, the values will be perfectly divided to make all columns with the same size.
	// Not passing Align, make the method to use left.
	// Not passing ContentProp.Size, make the method use 10.
	// Not passing ContentProp.Family, make the method use arial.
	// Not passing ContentProp.Style, make the method use normal.
	// Not passing ContentProp.Color, make the method use a black font
	// Not passing Content.GridSizes, make the method use an array with same length
	// of content array in the first line, the values will be perfectly divided to make all columns with the same size.
	// Not passing HeaderContentSpace, will make the method use 4.
	// Not passing VerticalContentPadding, will make the method use 0. This prop cannot be less than 0.

	m := pdf.NewMaroto(consts.Portrait, consts.A4)

	headers := []string{"Header1", "Header2"}
	contents := [][]string{
		{"Content1", "Content2"},
		{"Content3", "Content3"},
	}

	m.TableList(headers, contents, props.TableList{
		HeaderProp: props.TableListContent{
			Family:    consts.Arial,
			Style:     consts.Bold,
			Size:      11.0,
			GridSizes: []uint{3, 9},
		},
		ContentProp: props.TableListContent{
			Family:    consts.Courier,
			Style:     consts.Normal,
			Size:      10.0,
			GridSizes: []uint{3, 9},
		},
		Align: consts.Center,
		AlternatedBackground: &color.Color{
			Red:   100,
			Green: 20,
			Blue:  255,
		},
		HeaderContentSpace:     10.0,
		Line:                   false,
		VerticalContentPadding: 4.0,
	})

	// TableList have to be used at same level as row
	m.Row(10, func() {
		m.Col(12, func() {
			// Add a component
		})
	})

	// Do more things and save...
}
Output:

func (*PdfMaroto) Text

func (s *PdfMaroto) Text(text string, prop ...props.Text)

Text create a text inside a cell.

Example

ExamplePdfMaroto_Text demonstrates how to add a Text inside a col.

package main

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

func main() {
	// Not passing the text prop will lead to use all the follow default values.
	// Not passing family, makes the method use arial.
	// Not passing style, makes the method use normal.
	// Not passing size, makes the method use 10.0.
	// Not passing align, makes the method use left.
	// Not passing extrapolate, makes the method use false.
	// Not passing color, makes the method use the current color.
	// Top cannot be less than 0.
	// VerticalPadding cannot be less than 0.

	m := pdf.NewMaroto(consts.Portrait, consts.A4)
	rowHeight := 5.0

	m.Row(rowHeight, func() {
		m.Col(12, func() {
			m.Text("TextContent", props.Text{
				Size:            12.0,
				Style:           consts.BoldItalic,
				Family:          consts.Courier,
				Align:           consts.Center,
				Top:             1.0,
				Extrapolate:     false,
				VerticalPadding: 1.0,
				Color: color.Color{
					Red:   10,
					Green: 20,
					Blue:  30,
				},
			})
		})
	})

	// Do more things and save...
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL