Generate invoice PDF in Go

Wilson Tan
3 min readMay 26, 2021

While there are various Go libraries that can create PDF such as Maroto, UniPDF, wkhtmltopdf etc, I have stumbled upon GoFPDF which offers the capability to create complicated PDF file and in this article, we will be looking at how we can generate invoice PDF using GoFPDF.

Note that GoFPDF is no longer maintained, but it continues to function and serve our purpose well in this article.

Sample of invoice PDF generated

Let’s get started

First, we will create a page of A4 size in portrait mode and we also set the margins for the page. As we run pdf.AddPage(), the current position will move to the top left position in the page according to the margin set for the page. It is important to set the margin for the PDF document first before we add a new page.

marginX := 10.0
marginY := 20.0
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.SetMargins(marginX, marginY, marginX)
pdf.AddPage()

Once a page is added, we will start by adding a logo to the top left of the document.

pdf.ImageOptions("assets/logo.png", 0, 0, 65, 25, false, gofpdf.ImageOptions{ImageType: "PNG", ReadDpi: true}, 0, "")

Next, we will start adding the company name and other details. We can set the font style and size first via pdf.SetFont("Arial", “B", 16). Since we have added a logo, we need to move the cursor position downwards and to achieve that, we can get the Y position via pdf.GetY(). pdf.GetFontSize() returns the size of the current font in points according to the unit of measurement we set in gofpdf.New() which is in mm. A line gap (equivalent to the font size) is added to the current Y position and is set via pdf.SetY(currentY). After setting the position and the font, we can print out the text via pdf.Cell(40, 10, "Company Name") with the width and height specified. This is the simplest form of printing out a text with no fills, borders, links etc.

pdf.SetFont("Arial", "B", 16)
_, lineHeight := pdf.GetFontSize()
currentY := pdf.GetY() + lineHeight
pdf.SetY(currentY)
pdf.Cell(40, 10, "Company Name")

To draw a line, we can use pdf.Line(x1, y1, x2, y2) . Another note is that we can easily move down the position by a specified height via pdf.Ln(h) and the current position will start from the starting X position again with a new Y position.

Now, it’s time to draw the table. We will use pdf.CellFormat() to draw the border of the cell. The function also offers the flexibility to set the alignment, fill color, link etc. Note that once this function is called, the current position will move by the width amount specified, thus allowing us to create another cell directly next to it. To move the current position down with no gap, we can call pdf.Ln(-1) which will move the position down by the height of previous printed cell. The following is the sample code for printing the table.

Sample code for printing table

We are now done for generating an invoice PDF in Go. If you are interested to find out more, you can check out the source code at https://github.com/wilsontwm/invoice-generator

Cheers!

--

--