Create PDF Tables
This guide shows you to create a PDF Table with UniPDF. With this knowledge, You can include tables in your PDFs and perform other PDF functionalities.
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 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
In this example, you will see the different ways you can use UniPDF packages to create and customize tables.
Lines 9-18
import the UniPDF packages and other required dependencies.
Lines 20-27
authenticate your request with your UNIDOC_LICENSE_API_KEY
with the init function.
In the main function lines 29-72
, we create a new instance of the PDF creator using the .New()
method. In this guide, we will be saving the output to unidoc-tables.pdf
.
Lines 74-94
generates the front page for the PDF. We will also draw a footer for each of the pages using the .drawFooter
from lines 96-108
and generate a table of contents with the .TOC
method from lines 110-129
.
From here, you can choose to experiment with creating basic or advanced tables in your PDF.
For a basic PDF with tables, Lines 131-150
creates a new chapter with the NewChapter
function and creates three subchapters in the new chapter showing how tables work in UniPDF.
In the first subchapter, we create a table and align its content horizontally similar to working with text in an editor.
In the second subchapter, we create a table and align its content vertically similar to working with text in an editor.
In the third subchapter, we create a table and illustrate wrapping and aligning text to fit a cell.
Lines 152-428
creates a new chapter with three subschapters showing how to style the content in your table.
From Lines 430-711
, we have the advanced table in detail. We created a new chapter with three subchapters, each subchapter with a table showing how to span a table content across columns, set rows as headers and create subtables in a table.
Run the code
Run this command to create the PDF. This will also get all the required dependencies to run the program automatically.
go run pdf_tables.go
Sample output
Open the created PDF in any PDF viewer, and you should get a 7-paged PDF like this.