Security Report
This guide will show how to create a security-report document using creator templates.
Before you begin
Before you begin, you should first 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/security-report
folder in the unipdf-examples directory.
cd unipdf-examples/templates/security-report
How it works
The following code shows how a security report document document is created using the template.
The import
section ind the init
function in lines 10-36
imports necessary libraries and loads license key respectively.
The main
function is defined in lines 38-139
. In this function 39-41
the creator object is instantiated and the margins of the page is set. Also the table of contents generation is set to true using c.AddTOC = true
in line 40. In line 44-47
the template file is read using the readTemplate
function.
In lines 51-72
the template options object is defined. In lines 74-76
the template is drawn using c.DrawTemplate
. In lines 79-95
the header section of the document is drawn. And the footer is drawn using the code in lines 98-119
. The front page of the document is drawn using the following code in lines 122-133
.
c.CreateFrontPage(func(args creator.FrontpageFunctionArgs) {
// Read front page template.
frontPageTpl, err := readTemplate("templates/front-page.tpl")
if err != nil {
log.Fatal(err)
}
// Draw front page template.
if err := c.DrawTemplate(frontPageTpl, nil, nil); err != nil {
log.Fatal(err)
}
})
The document is written to file in lines 136-138
using c.WriteToFile("unipdf-security-report.pdf")
.
The readTemplate
function in lines 143-156
reads a template file and returns an io.Reader
buffer.
In lines 159-180
the function createPieChart
is defined. This function, as its name indicates, is used to create a pie chart. It returns render.ChartRenderable
object using unichart.PieChart
as can be seen from the code in lines 177-180
.
The function createLineChart
in lines 183-199
is used to create a line chart.
The function createBarChart
which is defined in lines 201-222
is used to create a bar chart. In this function the unichart.BarChart
object is instantiated in lines 202-206
using the following code.
chart := &unichart.BarChart{
BarWidth: 20,
BarSpacing: 20,
Bars: parseChartValMap(valMap),
}
This object is returned by the function after setting the values and the axis values in lines 209-221
.
In lines 225-234
the function createStackedBarChart
is defined to create stacked bar chart as its name indicates.
The function createStackedBar
in lines 239-245
is used to create stacked bar.
The function parseChartValMap
in lines 248-266
is used to parse a value map and returns it as a dataset slice.
Run the code
run the code by using the following command.
go run pdf_security_report.go
Sample Output
preview of the generated document.