Multipage PDF From HTML and Assets Directory

This guide will demonstrate how to process an HTML document with organized directories of resource and render a multipage PDF document 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-17 imports the necessary dependencies. The init function initializes the package by setting the metered License API key.

In lines 29-32 the number of command line arguments is checked to make sure the server address is provided as the second argument when running the code. Then a connection is established to the server using unihtml.Connect(os.Args[1]). Line 44 loads the HTML content from the data directory to create nihtml.Document object. In line 51 the size of the PDF is set to A5 to make the content span multiple pages. Line 57 sets the page orientation to a landscape. The rendered PDF pages are obtained using htmlDocument.GetPdfPages(context.Background()) in line 60. These pages are then added to a creator object in the for loop defined in lines 67-72. Finally, the document is written to a file in line 75.

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_pages.go <server address>

Sample output

output page1 output page2

Got any Questions?

We're here to help you.