PDF From a Simple HTML
This guide will show the process of generating a PDF document from a simple HTML file using UniHTML
.
Sample input
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 above example code starts by importing the necessary libraries in the import
section.
Then it initializes the package by setting the metered license key using license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
The next we find is the main
function which is defined in lines 26-59
. In this function the command line arguments is checked for the server address. Then a connection is established to UniHTML
server in line 33
uisng unihtml.Connect(os.Args[1])
.
In line 39
a new Creator
is instantiated. A unihtml.Document
is then created from the simple.html
file in line 42
. This object implements the creator.Drawable
interface and is used in the Draw
method of the creator
object.
In line 49
the HTML document is processed and the pages are rendered into a PDF page. Finally, the document is written to a file in line 55
.
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.go <server address>