Generate PDF from an Inline HTML Content

In this guide the process of using an HTML string to generate a PDF document using UniHTML will be explained.

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 simple folder in the unihtml-examples directory.

cd unihtml-examples/simple

How it works

The import section in lines 8-15 imports the necessary libraries. Then the init function in lines 7-14 sets the metered license API key to authenticate the library request.

The main function starts by checking the command line arguments for the server address. Line 33 in this function tries to establish a connection to the sever using the provided server address. The HTML content is defined as a string in lines 38-55. A new creator.Creator is instantiated in line 59. Then a new unihtml.Document is created from the string in line 62. Line 69 draws the content on a PDF page using c.Draw(htmlDocument). Finally, the document is written to file using c.WriteToFile("simple-from-text.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 simple-html-content.go <server address>

Sample output

converted PDF

Got any Questions?

We're here to help you.