Create PDF Report

This guide will walk you through creating a pdf creating a PDF report using the UniPDF creator package.

Before you begin

Before starting to follow along with this guide you should get your API key from your UniCloud account.

If this is your first time using UniPDF SDK, follow this guide to set up a local development environment.

Project setup

Clone the project repository

In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.

git clone https://github.com/unidoc/unipdf-examples.git

Navigate to the report folder in the unipdf-examples directory.

cd unipdf-examples/report

Configure environment variables

Replace the UNIDOC_LICENSE_API_KEY with your API credentials from your UniCloud account.

Linux/Mac

export UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE

Windows

set UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE

How it works

The import section in lines 14-31 imports UniPDF packages and other necessary libraries.
The init function defined in lines 33-40 loads your metered license key from the system environment and authenticates your library request.

The main function in lines 42-47 calls the RunPdfReport function to generate the by providing the path the report document to be generated.

The RunPdfReport function defined in lines 49-122 generates the whole document. In this function in lines 50-58 two fonts types are loaded from files using model.NewPdfFontFromTTFFile method. In lines 60-61 a new creator is instantiated and the margins of the page are set using c.SetPageMargins(50, 50, 100, 70). In lines 64-72 the table of contents is generated. In lines 74-80 a new image is loaded and its scale and position are set using ScaleToHeight and SetPos methods of the creator.Image object.

In line 82 the document control page is created using DoDocumentControl function and in line 84 the document overview page is created by calling the DoFeatureOverview function. The front page is created using CreateFrontPage method of the creator object in lines 87-89. In lines 92-95 a header is added to each page using DrawHeader method. The footer is drawn in lines 98-114 using DrawFooter method. The document is written to file using c.WriteToFile(outputPath) method in lines 116-122.

The DoFirstPage functions in lines 125-152 generates the front page.

The DoDocumentControl function in lines 160-310 creates the document control page. In this function the code in lines 161-263 creates a new chapter and adds different paragraphs using c.NewParagraph and cell.SetContent(p) methods. The for loop in lines 267-297 creates a document history table. In line 299-304 the table is added into the subchapter and the chapter that holds all the contents is drawn using the following piece of code.

sc.Add(histTable)

err := c.Draw(ch)
if err != nil {
  panic(err)
}

The DoFeatureOverview function 309-518 creates the feature overview page. In lines 311-313 a new page and a new chapter are created using


c.NewPage()
ch := c.NewChapter("Feature overview")

In lines 329-369 the Paragraphs section of the page is drawn. In lines 371-404 the Tables section of the page is created. The Images section is drawn in lines 406-425 and the QR Codes or Barcodes section is drawn in lines 427-447. The graphing /charts section is drawn in lines 447-482. Finally the Headers and Footers content of the document is created in lines 447-515.

The function makeQrCodeImage in lines 522-535 creates a QR code image using the given text and image dimension and returns image.Image object.

Run the code

go run pdf_report.go

Sample output

The following image shows the preview of the output document.

PDF Report

Got any Questions?

We're here to help you.