Create PDF Report Landscape

This guide will show how to create PDF reports in landscape mode.

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 15-32 imports UniPDF packages and other necessary libraries.
The init function defined in lines 34-41 loads your metered license key from the system environment and authenticates your library request. The main function in lines 43-48 creates the report by calling RunPdfReport function.

The RunPdfReport function generates the report document and writes it to file. In lines 51-59 the necessary fonts are loaded from files using model.NewPdfFontFromTTFFile. In lines 61-66 a new creator is instantiated and the document margin is set. The landscape page is defined here in line 64 where the sizes of the A4 are reversed during initialization of the page size using .SetPageSize(creator.PageSize{creator.PageSizeA4[1], creator.PageSizeA4[0]}). The second dimension is set to be the first and vise versa.

The table of contents is generated in lines 69-77. In lines 87 and 89 the document control and feature overview pages are created using DoDocumentControl and DoFeatureOverview functions. In lines 92-94 the front page is created using

c.CreateFrontPage(func(args creator.FrontpageFunctionArgs) {
  DoFirstPage(c, robotoFontRegular, robotoFontPro)
})

Lines 97-100 draw the page header. The footer is drawn in lines 103-119. In lines 121-126 the document is written to file using c.WriteToFile.

The DoFirstPage function defined in lines 130-157 creates the first page of the document. In this function, in lines 131 and 132 the necessary fonts are loaded from files. In lines 134-156 three new paragraphs are created and added to the page.

The DoDocumentControl function in lines 314-522 creates the document control page. In this function new paragraphs are created using c.NewParagraph. All the paragraphs are added using Add method of the creator.Chapter type.

The DoFeatureOverview function defined in lines 314-323 creates the feature overview page of the report document. The makeQrCodeImage function creates a QR code image based on the given text data and image dimensions.

Run the code

go run pdf_report_landscape.go

Sample output

The following image shows the preview of the generated document.

PDF Landscape Report

Got any Questions?

We're here to help you.