maroto
A maroto way to create PDFs. Maroto is inspired in Bootstrap and uses gofpdf. Fast and simple.
Top Related Projects
Quick Overview
Maroto is a Go library for creating PDF documents with a simple and intuitive API. It provides a high-level abstraction layer over the gofpdf library, making it easier to create complex PDF layouts with tables, images, and various text styles.
Pros
- Easy to use API for creating PDF documents
- Supports complex layouts with tables, images, and text styling
- Built-in support for common elements like headers, footers, and page numbers
- Active development and community support
Cons
- Limited customization options compared to lower-level PDF libraries
- Dependency on the gofpdf library
- May have performance limitations for very large or complex documents
- Limited support for advanced PDF features like forms or interactive elements
Code Examples
Creating a simple PDF with a title and paragraph:
m := maroto.New()
m.AddPage()
m.Row(40, func() {
m.Col(12, func() {
m.Text("Welcome to Maroto", props.Text{
Top: 12,
Style: consts.Bold,
Align: consts.Center,
Size: 20,
})
m.Text("This is a simple example", props.Text{
Top: 22,
Align: consts.Center,
Size: 15,
})
})
})
err := m.OutputFileAndClose("output.pdf")
if err != nil {
fmt.Println("Could not save PDF:", err)
os.Exit(1)
}
Adding a table to the PDF:
m := maroto.New()
m.AddPage()
headers := []string{"Name", "Age", "City"}
contents := [][]string{
{"John Doe", "30", "New York"},
{"Jane Smith", "25", "London"},
{"Bob Johnson", "35", "Paris"},
}
m.TableList(headers, contents, props.TableList{
HeaderProp: props.TableListContent{
Family: consts.Arial,
Style: consts.Bold,
Size: 10,
},
ContentProp: props.TableListContent{
Family: consts.Courier,
Style: consts.Normal,
Size: 8,
},
Align: consts.Center,
AlternatedBackground: &color.Color{
Red: 200,
Green: 200,
Blue: 200,
},
HeaderContentSpace: 4,
Line: true,
})
err := m.OutputFileAndClose("table_example.pdf")
if err != nil {
fmt.Println("Could not save PDF:", err)
os.Exit(1)
}
Getting Started
To use Maroto in your Go project, follow these steps:
-
Install the library:
go get -u github.com/johnfercher/maroto
-
Import the library in your Go code:
import ( "github.com/johnfercher/maroto" "github.com/johnfercher/maroto/pkg/consts" "github.com/johnfercher/maroto/pkg/props" )
-
Create a new Maroto instance and start building your PDF:
m := maroto.New() m.AddPage() // Add content to your PDF err := m.OutputFileAndClose("output.pdf")
Competitor Comparisons
A simple library for generating PDF written in Go lang
Pros of gopdf
- More lightweight and focused solely on PDF generation
- Offers lower-level control over PDF elements
- Better performance for large-scale PDF generation
Cons of gopdf
- Less user-friendly for creating complex layouts
- Requires more manual coding for advanced features
- Limited built-in support for tables and charts
Code Comparison
maroto example:
m := pdf.NewMaroto(consts.Portrait, consts.A4)
m.Row(40, func() {
m.Col(12, func() {
m.Text("Hello World")
})
})
m.OutputFileAndClose("output.pdf")
gopdf example:
pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
pdf.AddPage()
pdf.SetFont("Arial", "", 14)
pdf.Cell(nil, "Hello World")
pdf.WritePdf("output.pdf")
Both libraries allow for PDF generation in Go, but maroto provides a higher-level abstraction with a more intuitive API for creating complex layouts. gopdf offers more granular control over PDF elements but requires more manual coding for advanced features. maroto is better suited for quickly creating structured documents, while gopdf may be preferred for performance-critical applications or when fine-grained control is necessary.
Golang PDF library for creating and processing PDF files (pure go)
Pros of unipdf
- More comprehensive PDF manipulation capabilities, including reading, editing, and form filling
- Supports both creation and modification of existing PDFs
- Offers advanced features like digital signatures and encryption
Cons of unipdf
- Commercial license required for most use cases
- Steeper learning curve due to more complex API
- Less focused on simplifying PDF generation compared to Maroto
Code Comparison
Maroto (creating a simple PDF):
m := maroto.New()
m.Row(40, func() {
m.Col(12, func() {
m.Text("Hello, World!")
})
})
m.OutputFileAndClose("output.pdf")
unipdf (creating a simple PDF):
pdf := creator.New()
page := pdf.NewPage()
p := creator.NewParagraph("Hello, World!")
p.SetPos(100, 100)
page.Draw(p)
pdf.WriteToFile("output.pdf")
Both libraries offer PDF creation capabilities, but unipdf provides a more extensive set of features for PDF manipulation. Maroto focuses on simplifying PDF generation with a user-friendly API, while unipdf offers a broader range of PDF-related functionalities at the cost of increased complexity and licensing requirements.
A PDF processor written in Go.
Pros of pdfcpu
- More comprehensive PDF manipulation capabilities, including encryption, optimization, and validation
- Command-line interface for easy integration into scripts and workflows
- Supports both reading and writing PDF files
Cons of pdfcpu
- Steeper learning curve due to its extensive feature set
- Less focused on PDF generation from scratch compared to Maroto
- May be overkill for simple PDF creation tasks
Code Comparison
Maroto (PDF generation):
m := maroto.New()
m.Row(40, func() {
m.Col(12, func() {
m.Text("Hello, World!")
})
})
m.OutputFileAndClose("output.pdf")
pdfcpu (PDF manipulation):
api.AddWatermarks(inputFile, outputFile, nil, watermarkContent, nil)
While Maroto focuses on creating PDFs from scratch with a fluent API, pdfcpu provides a wide range of PDF manipulation functions. Maroto is more suitable for generating simple reports or documents, whereas pdfcpu excels in tasks like adding watermarks, merging PDFs, or optimizing existing files. The choice between the two depends on the specific PDF-related tasks required in your project.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Maroto V2
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. Art by @marinabankr
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.
Maroto v2.1.2
is here! Try out:
- Installation with
go get
:
go get github.com/johnfercher/maroto/v2@v2.1.2
- You can see the full
v2
documentation here. - The
v1
still exists in this branch, and you can see the doc here.
Contributing
Command | Description | Dependencies |
---|---|---|
make build | Build project | go |
make test | Run unit tests | go |
make fmt | Format files | gofmt , gofumpt and goimports |
make lint | Check files | golangci-lint |
make dod | (Definition of Done) Format files and check files | Same as make build , make test , make fmt and make lint |
make install | Install all dependencies | go , curl and git |
make examples | Run all examples | go |
make mocks | Generate mocks | go and mockery |
make docs | Run docsify docs server local | docsify |
make godoc | Run godoc server local | godoc |
Stargazers over time
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot