Create PDF Reports
This guide shows you to create a PDF document with UniPDF. With this knowledge, You can perform further PDF functionalities in your application.
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 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
Change into the repository directory and navigate to the report
folder.
cd unipdf-examples/report
Configure Environment Variables
Replace the UNIDOC_LICENSE_API_KEY
with your API credentials from your UniCloudaccount.
Linux/Mac
export UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE
Windows
set UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE
Code Review
Lines 14-31
import the UniPDF packages and other required dependencies.
Lines 33-40
authenticate your request with your UNIDOC_LICENSE_API_KEY
with the init function.
Lines 42-47
call the RunPDFReport
function with an input, which is the path to the PDF you want to create. In this guide, we will save the output to unidoc-report.pdf
.
From line 49
, we will create the RunPDFreport
function in detail.
Lines 50-58
loads the .tff
font file into a PDFFont type that can be used in text styling functions. For this example, we will be using the Roboto font (bold and regular).
Lines 60-62
create a new instance of the PDF creator using the .New()
method, set the page margins in 4D (left, right, top, bottom) with the setPDFMargin
function.
Line 63-122
generates a table of contents for the created PDF with the .AddTOC
method. After which, we set a particular page as the front page of the PDF, drawing a header and footer on each created output page using AddHeader and AddFooter functions, respectively. Finally, we save the output to a specific file in the path.
Lines 125-152
style the front page with font, colour and margins. The front page can include paragraphs and headers.
Run the code
Run this command to create the PDF. This will also get all the required dependencies to run the program.
go run pdf_report.go
Sample Output
Open the created PDF in any PDF viewer, and you should get a 5-paged PDF like this.