Golang Pdf Creation with Maroto and Gofpdf

Author

Reads 734

From above crop faceless male developer in black hoodie writing software code on netbook while working in light studio
Credit: pexels.com, From above crop faceless male developer in black hoodie writing software code on netbook while working in light studio

Creating PDFs with Go is a breeze, thanks to libraries like Maroto and GoFPDF. Maroto is a lightweight, easy-to-use library that allows you to create PDFs with a simple and intuitive API.

GoFPDF, on the other hand, provides a more extensive set of features, including support for tables, images, and fonts. With GoFPDF, you can create complex PDF documents with ease.

One of the key benefits of using Maroto or GoFPDF is that they are both highly customizable. You can adjust font sizes, colors, and styles to match your specific needs.

To get started with PDF creation in Go, you'll need to install one of these libraries and import it into your project.

Curious to learn more? Check out: Golang Create

Choosing a Library

Golang PDF libraries have evolved significantly over time, offering a wide range of features.

Integrating PDF handling capabilities into Golang applications opens doors to various use cases, such as generating reports, processing invoices, creating documents, and much more.

Credit: youtube.com, How To Add Links In PDF Files using UniPDF | Golang

These libraries offer a wide array of features, including PDF generation and modification, text and image manipulation, and parsing and extraction.

To get started with creating PDFs, you'll first need to install a library, such as gofpdf, which provides a simple interface for creating PDF documents.

The gofpdf library can be installed using the go get command.

Modern Golang PDF libraries offer a wide array of features, including PDF generation and modification, text and image manipulation, and parsing and extraction.

These features make it easier to create and manipulate PDF documents in Golang applications.

Some libraries may have better performance, community support, documentation, and licensing than others, so it's essential to research and compare them before making a decision.

A comparative analysis of prominent Golang PDF libraries can help you make an informed choice.

Maroto Library

Maroto is built upon GoFPDF, a low-level builder library for generating PDFs in Go. GoFPDF offers flexibility and control to define every element in your PDF, but its complexity makes it time-consuming to use.

Credit: youtube.com, #59 Golang - Create Professional PDFs: A Complete Guide with Maroto

Maroto takes a different approach, providing a simple interface for generating PDFs based on the Bootstrap framework. It compensates for Gofpdf's complexity with predefined layouts, automatic line breaks, and a simplified API for adding images, headers, and footers.

Maroto is still in active development, despite GoFPDF's archived status. This means you can rely on Maroto for your PDF generation needs, and it will continue to improve over time.

Maroto's simplified API makes it easier to create PDF documents compared to GoFPDF. You'll spend less time developing and more time focusing on your project's core features.

Curious to learn more? Check out: Golang Restful

Gofpdf Library

The Gofpdf Library is a popular choice for creating PDF documents in Golang. It provides a simple interface for creating PDFs.

To install Gofpdf, you can use the go get command. This library is a great starting point for developers looking to create PDFs in Golang.

Here are the basic steps to create a PDF with Gofpdf:

A fresh viewpoint: Create a Package in Golang

Credit: youtube.com, How To Split PDF Files In Go - Using UniPDF

1. Import the gofpdf package

2. Initialize a new PDF document with gofpdf.New

3. Add a new page to the document

4. Set the font and add a cell with the desired text

5. Save the PDF to a file

This is a simple example of creating a PDF with Gofpdf, and it's a great way to get started with PDF creation in Golang.

Advanced Techniques

With the basics covered, let's move on to some advanced techniques for creating more complex PDFs.

You can use Gofpdf to create PDFs with multiple pages by calling the AddPage method multiple times.

One of the most powerful features of Gofpdf is its ability to merge multiple PDFs into one. This can be achieved by using the MergePDF method.

By combining these advanced techniques, you can create PDFs that are tailored to your specific needs.

Adding Images and Fonts

Adding images to your PDF can make it much more visually appealing. This can be done by specifying the file path, x and y coordinates, width, and height of the image, as seen in the example where the line of code adds an image located at image.jpg to the PDF.

A Photographer Looking at Images on Film Negatives
Credit: pexels.com, A Photographer Looking at Images on Film Negatives

To add an image, you'll need to specify the image type, such as JPG, and DPI setting, like ReadDpi: true. This will ensure that the image is displayed correctly in the PDF.

You can also add custom fonts to your PDF to give it a unique look. This is done by adding the font using a JSON file that contains the necessary font metrics, like the Roboto font.

Setting the font for subsequent text is also important, so make sure to do this after adding the custom font.

Multi-Page

Creating multi-page PDFs is as simple as calling pdf.AddPage() multiple times. You can add multiple pages to a PDF by repeating this process.

To add content to each page, you can use the same methods as before. For example, you can add a cell with text to each page.

Adding a cell with the text "Page 1" to the first page and "Page 2" to the second page is a great way to label each page.

Generating from Scratch

People Working in front of the Computer
Credit: pexels.com, People Working in front of the Computer

Generating from Scratch is a powerful technique that allows you to create PDF files directly from code. You can start by following along with an example that generates a PDF invoice from scratch.

The example uses a main.go file and a GitHub repository to store the code base. Note that the main.go file will be added to later. The NewInvoiceData allows any data type, and a switch statement filters the allowed data type.

To generate a PDF invoice, you'll need an API key, which can be obtained by gathering new invoice data objects on lines 10 and 14. This data is used to create the PDF invoice file.

Creating PDFs programmatically is useful for generating reports, invoices, or other documents that require structured and visually appealing data presentation. This can be achieved by adding specific lines of code to the unipdf-getting-started file after the import block.

Libraries and Performance

As you explore the world of Golang PDF libraries, it's essential to consider their performance. Unidoc may have a higher resource overhead due to its wide range of features.

However, it remains performant for most applications. This means you can rely on Unidoc for your PDF needs without worrying about significant slowdowns.

The performance of Golang PDF libraries is crucial for applications that handle large volumes of PDFs. You can expect Unidoc to handle these tasks efficiently.

Comparative Analysis of Libraries

Credit: youtube.com, How Do You Do Comparative Analysis? - The Language Library

In terms of features, modern Golang PDF libraries offer a wide array of capabilities, including PDF generation and modification, text and image manipulation, and parsing and extraction.

These libraries can create new PDFs from scratch or modify existing ones, making them highly versatile.

One notable example is Unidoc, which has a dedicated customer support team providing timely assistance for any issues or inquiries.

Unidoc is a commercial library, but its customer support team is a significant advantage for developers who need help.

Some prominent Golang PDF libraries also focus on performance, ensuring they can handle complex tasks efficiently.

However, community support is also a crucial aspect to consider, as it can impact the overall development experience.

Related reading: Golang Support

Performance

Performance is a crucial aspect to consider when choosing a library. Unidoc, for instance, has a wide range of features, which can lead to a higher resource overhead.

Libraries with a lot of features can indeed be more resource-intensive. However, this doesn't necessarily mean they're slow.

Close-up view of a computer screen displaying code in a software development environment.
Credit: pexels.com, Close-up view of a computer screen displaying code in a software development environment.

Unidoc remains performant for most applications, despite its extensive feature set. This is likely due to its efficient design and optimization.

In general, a library's performance is determined by its ability to balance features and resource usage. Some libraries prioritize speed, while others focus on offering a wide range of features.

Introduction and Basics

Portable Document Format (PDF) has become a staple in the digital world due to its ability to preserve the layout of any source document.

PDF is a file format that captures the digital world by preserving fonts, images, graphics, and layout. This makes it an excellent choice for creating reports, invoices, or other documents where data needs to be presented in a structured and visually appealing format.

Creating PDFs programmatically can be particularly useful for generating reports or invoices directly from code.

The Portable Document Format is a file format that has been around for a while, and Adobe has an excellent guide that covers everything you need to know about it.

Creating PDFs

Person Signing in Documentation Paper
Credit: pexels.com, Person Signing in Documentation Paper

Creating PDFs in Golang is a straightforward process. You can use the gofpdf library to create a PDF with a single line of text, as shown in Example 3.

To start, you'll need to import the gofpdf package and initialize a new PDF document. This is done by calling gofpdf.New() with the desired page orientation, unit of measurement, size, and font directory.

Once you have your PDF document set up, you can add a new page to the document using pdf.AddPage(). You can then set the font to Arial, bold, with a size of 16 points using pdf.SetFont().

To add text to the page, you can use the pdf.Cell() method, which takes the width, height, and text to be added as arguments. For example, you can add a cell with the text "Hello, World!" using pdf.Cell(40, 10, "Hello, World!").

Finally, you can save the PDF to a file using pdf.OutputFileAndClose(). This method takes the file path as an argument and saves the PDF to that file.

Suggestion: Golang Use Cases

Close-up of hands typing on a laptop with code on screen, perfect for work from home and tech themes.
Credit: pexels.com, Close-up of hands typing on a laptop with code on screen, perfect for work from home and tech themes.

Here's a summary of the steps to create a PDF with gofpdf:

You can also use the UniDoc Go package to create PDFs, as shown in Example 1. This package allows you to create a PDF with a nice invoice format. You can create a Client struct with a property creator, which holds your PDF creator instance, and use the creator instance to manipulate the PDF document.

To create a PDF with UniDoc, you'll need to load the API key from the environment variable, and then use the creator instance to create the PDF. You can add tables, blocks, and other elements to the PDF using the creator instance.

For example, you can create a table block using the creator instance, as shown in Example 1. You can also use the creator instance to add blocks to the output document, which will create the PDF at the specified path.

Credit: youtube.com, Golang PDF tool with Go, Maroto & GoFakeIt | Tutorial

UniDoc also provides a SDK hosted on GitHub, which you can use to create PDF reports, as shown in Example 2. This SDK allows you to create a page with styled text and save it to a file.

Overall, creating PDFs in Golang is a simple process that can be achieved using libraries like gofpdf and UniDoc. With these libraries, you can create PDFs with various formats and layouts, and save them to files for later use.

Installation and Setup

To start working with Go and PDFs, you need to install the gofpdf library using the go get command.

The gofpdf library provides a simple interface for creating PDF documents in Golang, making it a great choice for beginners.

First, initialize a new Go project by opening a terminal window and running a command that will create a go.mod file in the folder. This file is used to manage the dependencies of a Go project.

If this caught your attention, see: Golang Go

Credit: youtube.com, Visual Studio Code and Golang "Go" installation on Windows 11

You can customize the command to suit your needs by replacing github.com with the domain where you store your files, such as Bitbucket or Gitlab, and substituting USERNAME with your username and PROJECT_NAME with the desired project name.

After the command creates a go.mod file successfully, you are ready to install the necessary packages. The only package required to follow along with this article is Maroto.

Maroto is in its v2 stage, which provides significant performance improvements compared to v1.

A Beginner's Guide

As a beginner, you're in luck because you can start creating PDFs with Golang using the gofpdf library as a starting point. This library is perfect for those just starting out.

You've probably been drawn to Golang's simplicity, speed, and efficiency, but you might be wondering how to use it to write PDFs. Golang is a great language for developers who want to create efficient and effective solutions.

With the gofpdf library, you'll gain the skills to create PDFs confidently, even if you're new to Golang. This library is designed to help you navigate the landscape of PDF generation with Golang on the UniDoc platform.

You can use this library to create PDFs for various needs, from simple reports to more advanced reporting needs. The UniPDF library by UniDoc is also a great option for more advanced reporting needs, but gofpdf is a good starting point for beginners.

Mona Renner

Senior Copy Editor

Mona Renner is a meticulous and detail-driven Copy Editor with a passion for refining complex concepts into clear and concise language. With a keen eye for grammar and syntax, she has honed her skills in editing articles across a range of technical topics, including Google Drive APIs. Her expertise lies in distilling technical jargon into accessible and engaging content that resonates with diverse audiences.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.