Notice: This implementation is from Maroto V1. The project now has a newer version, simpler and faster.
Nowadays, we have some libraries to create PDFs using the Go programming language. But the currently options are hard to maintain or have a slow execution time. Therefore, I was wondering, if we have a library which executes fast, using few resources and still easy to modify the code. Something like this.
And not like this.
The main difference between the codes is that maroto example focus more in create the PDF and avoid the programmer to deal with low level details of how the PDF is built. To create the project, I started to wrap to the gofpdf lib. I choose gofpdf because it's one of the fastest libraries to create PDFs, and I know that is used in production environments.
Although, the maroto still doesn't have the tons of features which gofpdf provides, but nothing prevents us to wrap all the features in the future. We just create a MVP which will be improved with feedbacks over time.
I choose to follow the way of Bootstrap to build HTML pages, because it would be easier to anyone learn how to use the project. Here is the page on Github, here are some samples of code, here are the results from the samples, and here is the documentation.
Grid System
The grid system is inspired on Bootstrap, we do have rows and columns. A row may have multiple columns inside it, and a column may have multiple components (text, barcode and etc) inside it. We also have some components like the RegisterHeader, which you can put multiple rows inside it.
There are some components which doesn't have to be inside a row, like the Line and TableList, which basically fulfill an entire row or many rows. The grid basically works like this:
m := pdf.NewMaroto(consts.Portrait, consts.Letter)
m.RegisterHeader(func() {
m.Row(rowSize, func() {
m.Col(12, func() {
m.Component(..., props.Prop{})
})
})
})
m.Line()
m.Row(rowSize, func() {
m.Col(12, func() {
m.Component(..., props.Prop{})
})
})
m.TableList()
Components
The project provide some components to make the development easier, and every component may receive properties, which are useful to change behavior of the component in a controlled way. We will not enter in details of how every component and your properties works, for this purpose we have the documentation.
License
The project is licensed under the MIT license.
Conclusion
This is my very first Golang opensource project, I focused in make the lib easier and extensible as possible. The project still in development, and we have a little roadmap to follow, improving testability and wrapping more features from gofpdf. If you have any question or advice, please, let me known.