Skip to content

rongfengliang/maroto

Repository files navigation

Maroto

GoDoc Travis Codecov Go Report Card Mentioned in Awesome Go

A Maroto way to create PDFs. Maroto is inspired in Bootstrap and uses Gofpdf. Fast and simple.

Maroto definition: Brazilian expression, means an astute/clever/intelligent person.

You can write your PDFs like you are creating a site using Bootstrap. A Row may have many Cols, and a Col may have many components. Besides that, pages will be added when content may extrapolate the useful area. You can define a header which will be added always when a new page appear, in this case, a header may have many rows, lines or tablelist.

Installation

  • With go get:
go get -u github.com/rongfengliang/maroto

Features

result

Grid System

Components To Use Inside a Col

Components To Use Outside a Row

Components To Wrap Row, TableList and Line

Others

  • Properties: most of the components has properties which you can use to customize appearance and behavior.
  • SetBorder: Used to draw rectangles in every row and column
  • SetBackgroundColor: Used to change the background color of one cell.
  • SetPageMargins: Customize the page margins
  • NewMarotoCustomSize(): Create Maroto with custom page dimensions
  • SetAliasNbPages: Set placeholder to use in texts for total count of pages
  • SetFirstPageNb: Set first number for page numbering
  • Automatic New Page: New pages are generated automatically when needed.
  • 100% Unicode
  • Save: You can save on disk or export to a base64 string

Roadmap

Examples

In the PDFs folder there are the PDFs generated using Maroto, and in the examples folder there are subfolders with the code to generate the PDFs.

result

Code

// Billing example
package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/gobuffalo/packr/v2"
	"github.com/rongfengliang/maroto/pkg/consts"
	"github.com/rongfengliang/maroto/pkg/pdf"
	"github.com/rongfengliang/maroto/pkg/props"
)

func main() {
	box := packr.New("pdf", "../font")
	begin := time.Now()
	m := pdf.NewMarotoCustomSize(consts.Landscape, "C6", "mm", 114.0, 162.0)
	m.SetPageMargins(5, 5, 5)
	notoSansBytes, err := box.Find("NotoSansSC-Regular.ttf")
	if err != nil {
		log.Fatal(err)
	}
	m.AddUTF8FontFromBytes("NotoSansSC", "", notoSansBytes)
	// m.SetBorder(true)
	m.Row(40, func() {
		m.Col(4, func() {
			_ = m.FileImage("biplane.jpg", props.Rect{
				Center:  true,
				Percent: 50,
			})
		})
		m.Col(4, func() {
			m.Text("Gopher International Shipping, Inc.", props.Text{
				Top:         12,
				Size:        12,
				Extrapolate: true,
			})
		})
		m.ColSpace(4)
	})

	m.Line(10)

	m.Row(30, func() {
		m.Col(12, func() {
			m.Text("北京市海淀区", props.Text{
				Size:   10,
				Align:  consts.Right,
				Family: consts.NotoSansSC,
			})
			m.Text("荣锋亮 TN 39021", props.Text{
				Size:   10,
				Align:  consts.Right,
				Family: consts.NotoSansSC,
				Top:    10,
			})
			m.Text("United States (USA)", props.Text{
				Size:  10,
				Align: consts.Right,
				Top:   20,
			})
		})
	})

	m.Row(30, func() {
		m.Col(12, func() {
			m.QrCode("https://cnblogs.com/rongfengliang")
		})
	})

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

	end := time.Now()
	fmt.Println(end.Sub(begin))
}

Others

Stargazers over time

Stargazers over time