Getting Started

This getting started to UniHTML guide will show how to get started working with unihtml. UniHTML is a UniPDF plugin that enables converting and injecting HTML to PDF files.

By the end of this introduction tutorial, you will be familiar with the process of converting HTML to PDF using UniHTML.

Setting Up The UniHTML Server

The UniHTML Plugin works with a working UniHTML server. This server is distributed using a docker image. To run the UniHTML server:

  1. Pull UniHTML Server Docker image using:

    docker pull unidoccloud/unihtml:latest
    
  2. Start the server using the following command.

    docker run -p 8080:8080 -e UNIHTML_LICENSE_PATH=path/to/license -e UNIHTML_CUSTOMER_NAME=customer_name unidoccloud/unihtml
    

    To use the metered license key set the UNIDOC_METERED_API_KEY environment variable as follows.

    docker run -p 8080:8080 -e UNIDOC_METERED_API_KEY=$UNIDOC_LICENSE_API_KEY unidoccloud/unihtml
    

For more details on How to prepare and run the server check the full guide here

If everything goes right you should see the server running as follows.

[INFO]  server.go:177 Listening private API on: :8081
[INFO]  server.go:168 Listening public API on: :8080

Once the server is up and running you are ready to generate PDF documents from HTML using the UniHTML client.

Usage

Save the following code into a file and run it to render an inline HTML string to a PDF document.

package main

import (
	"fmt"
	"os"

	"github.com/unidoc/unihtml"
	"github.com/unidoc/unipdf/v3/common/license"
	"github.com/unidoc/unipdf/v3/creator"
)

func init() {
	// Make sure to load your metered License API key prior to using the library.
	// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
	err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
	if err != nil {
		panic(err)
	}
}

func main() {
	if len(os.Args) != 2 {
		fmt.Println("Err: provided invalid arguments. No UniHTML server path provided")
		os.Exit(1)
	}

	// Establish connection with the UniHTML Server.
	if err := unihtml.Connect(os.Args[1]); err != nil {
		fmt.Printf("Err:  Connect failed: %v\n", err)
		os.Exit(1)
	}

	htmlContent := `
		 <!DOCTYPE html>
		 <html>
		 <head>
			 <style>
				 body {
					 background-color: #6E85F7;
					 font-size-adjust: initial;
				 }
			 </style>
		 </head>
		 <body>
		 
		 <h1>Simple Document</h1>
		 <p>Example contnet</p>
		 
		 </body>
		 </html>	
	 `

	// Get new PDF Creator.
	c := creator.New()

	// Convert the HTML content to UniHTML document.
	htmlDocument, err := unihtml.NewDocumentFromString(htmlContent)
	if err != nil {
		fmt.Printf("Err: NewDocument failed: %v\n", err)
		os.Exit(1)
	}

	// Draw the html document file in the context of the creator.
	if err = c.Draw(htmlDocument); err != nil {
		fmt.Printf("Err: Draw failed: %v\n", err)
		os.Exit(1)
	}

	// Write the result file to PDF.
	if err = c.WriteToFile("simple-from-text.pdf"); err != nil {
		fmt.Printf("Err: %v\n", err)
		os.Exit(1)
	}
}

Run the code using the following command.

go run file_name.go <server address>

Finally, you should find a PDF document that looks like the screenshot below. Output

Got any Questions?

We're here to help you.