Log Book Report
This guide will show you how to create a sample logbook document.
Before you begin
Before you begin, you should get your API key from your UniCloud account.
If this is your first time using UniPDF SDK, follow this guide to setup a local development environment.
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 templates/log-book-report
folder in the unipdf-examples directory.
cd unipdf-examples/templates/log-book-report
How it works
The import
section in lines 9-22
imports the necessary UniPDF packages and other libraries.
The init
function in lines gets your license key and authenticates your request.
The Item
object in lines 35-48
represents logbook item.
The LogBookData
in lines 51-55
which contains the log book Item
represents the data used in the template.
In lines 57-169
the main
function is defined. In this function in lines 58-60
the Creator
object is instantiated and the page margins are set. In lines 63-66
the template is read using readTemplate("templates/main.tpl")
.
In the lines 68-81
some necessary fonts are loaded from file. In line 84-87
the json data is read.
In line 88
the data is split using splitData(data.Items)
. The TemplateOptions
is created in 91-111
. The logbook data map is created in lines 114-119
. The main template is drawn in lines 121-123
using c.DrawTemplate
. The front page is drawn in lines 126-137
. The header and the footer are drawn in lines 157-163
using the following code.
c.DrawHeader(func(block *creator.Block, args creator.HeaderFunctionArgs) {
draw("templates/header.tpl", block, args.PageNum)
})
c.DrawFooter(func(block *creator.Block, args creator.FooterFunctionArgs) {
draw("templates/footer.tpl", block, args.PageNum)
})
Finally the document is written c.WriteToFile("unipdf-log-book.pdf")
in lines 166-168
.
The function readTemplate
in lines 172-184
is to read the template file.
The readData
function in lines 187-200
reads the json data and returns a decoded LogBookData
object.
The function splitData
defined in lines 203-225
splits the whole data to fractions which are rendered in each page.
Run the code
Run the code using the following command.
go run pdf_log_book.go
Sample Output
The following image show the preview of the logbook document.