How to Create a PDF in Go: A Step-by-Step Tutorial

UnidocLib
3 min readJul 12, 2023

--

How to create pdf in go by unidoc

In today’s digital age, generating PDF files programmatically has become an essential skill for developers and professionals alike. PDFs ensure document integrity, consistent formatting, and easy sharing across different platforms.

If you’re a Go developer looking to create PDFs using the power of Go, you’re in luck! This step-by-step tutorial will explore how to create PDFs using the UniPDF library, equipping you with the knowledge to get started.

Step 1: Setting Up the Environment

Before we dive into PDF creation, make sure you have Go installed on your system. Visit the official Go website (golang.org) and download the latest stable version suitable for your operating system.

Once installed, verify that Go works correctly by opening your terminal or command prompt and typing “go version.”

Step 2: Installing the UniPDF Library

We will use the UniPDF library to create PDFs in Go. Start by installing the library by executing the following command in your terminal:

```
go get -u github.com/unidoc/unipdf/v3/…

```

This command will download and install the UniPDF library along with its dependencies.

Step 3: Creating the PDF Document

Begin by creating a new Go file and importing the required packages. Let’s create a simple PDF document with a title and some text.

Follow these steps:

```go
package main
import (
"fmt"
"github.com/unidoc/unipdf/v3/common"
"github.com/unidoc/unipdf/v3/model"
)
func main() {
// Create a new PDF document
pdf := model.NewPdf()
// Create a new page
page := pdf.NewPage()
// Set the font and size for the title
titleFont, err := pdf.FindFont("Helvetica-Bold")
if err != nil {
common.Log.Error("Font not found: %v", err)
return
}
titleFontSize := 16.0
// Write the title
title := "My First PDF in Go"
titleWidth := titleFont.PtToPdfUnit(titleFontSize) * float64(len(title))
titleX := (page.Width() - titleWidth) / 2.0
titleY := page.Height() - 50.0
page.AddTextAnnot(titleX, titleY, titleFont, titleFontSize, title)
// Set the font and size for regular text
textFont, err := pdf.FindFont("Helvetica")
if err != nil {
common.Log.Error("Font not found: %v", err)
return
}
textFontSize := 12.0
// Write some text
text := "This is the content of my PDF."
textWidth := textFont.PtToPdfUnit(textFontSize) * float64(len(text))
textX := (page.Width() - textWidth) / 2.0
textY := titleY - 50.0
page.AddTextAnnot(textX, textY, textFont, textFontSize, text)
// Add the page to the PDF document
err = pdf.AddPage(page)
if err != nil {
common.Log.Error("Failed to add page: %v", err)
return
}
// Output the PDF file
err = pdf.WriteToFile("my_pdf.pdf")
if err != nil {
common.Log.Error("Failed to write PDF file: %v", err)
return
}
fmt.Println("PDF created successfully!")
}

```

Step 4: Customizing the PDF

Creating a basic PDF is just the beginning. The UniPDF library provides extensive functionality to customize your PDFs further. You can add images, tables, annotations, bookmarks, and more. Refer to the UniPDF documentation for comprehensive details on all available features.

Step 5: Compiling and Running the Program

To compile and run your Go program, open your terminal or command prompt, navigate to the directory containing your Go file, and execute the following command:

```
go run main.go

```

If everything goes well, you should see a new PDF file named “my_pdf.pdf” in the same directory.

Conclusion:

Congratulations! You have successfully learned how to create a PDF in Go using the powerful UniPDF library.

This tutorial covered the basics of generating a simple PDF document, but the library offers many features for advanced PDF manipulation. Explore the documentation, experiment with different options, and unleash the full potential of PDF creation with Go.

Happy PDF generation!

#GoProgramming #PDFGeneration #UniPDFLibrary #ProgrammingTutorial #PDFCreation #Golang #CodeSnippet #DeveloperTips

--

--

UnidocLib

PDF and Office (docx, xlsx, pptx) libraries for Golang. UniDoc is the most comprehensive solution in golang for managing PDF and office files. https://unidoc.io