PDF from HTML and Assets Directory

This guide will show the process of creating a simple PDF document from an HTML content organized in a directory using UniHTML.

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. To set up a UniHTML server follow this getting started guide.

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 [email protected]:unidoc/unihtml-examples.git

Navigate to the directory folder in the unihtml-examples directory.

cd unihtml-examples/directory

How it works

The import section in lines 8-15 imports the necessary libraries. The init function defined in lines 17-24 sets the metered License API key to authenticate the library usage.

The main function is defined in lines 25-75. Line 32 in this function tries to create a connection with server by calling the Connect function from the unihtml package. The server address from the command line argument is provided to this function call. In line 38 a creator.Creator object is created. Line 41-42 creates a paragraph and draws it using Draw method the creator.Creator object. The HTML content is loaded from the data directory and rendered to PDF in lines 48-58. Line 64-65 creates additional final paragraph and draws it on the PDF document. Finally, the PDF document is written to a file in line 71 using c.WriteToFile("directory.pdf").

Run the code

Run the code using the following command to generate the PDF file. Replace the server address with the actual address. E.g. localhost:8080 for a server running locally on port 8080.

go run directory.go <server address>

Sample output

Output Output

Got any Questions?

We're here to help you.