Document to PDF

This guide will teach you how to convert all types of Word documents to PDF.

Before you begin

You should get your API key from your UniCloud account.

If this is your first time using UniOffice SDK, follow this guide to set up a local development environment.

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 https://github.com/unidoc/unioffice-examples

Navigate to the path document/doc-to-pdf folder in the unioffice-examples directory.

cd unioffice-examples/document/doc-to-pdf/

You can also find many types of Word documents in this directory, which will be used in the code of the guide.

How it works

Lines 7-17 import the UniOffice packages and other required dependencies.

The init function in lines 19-28 authenticates your request with your UNIDOC_LICENSE_API_KEY.

In line 40, the variable “filenames” is defined, which contains the names of all the files that need to be converted to PDF.

In the main function, which runs from line 51 to 66, you can see how the list of names is iterated through. For each name, an output path for the result is generated in line 53. In the next line, the document is opened, and then in line 59, it is converted to PDF using the ConvertToPdf method. Finally, in line 61, the file is saved to the previously generated output path.

Run the code

Execute the following command to convert all the files to PDF.

go run main.go

Sample output

These are some of the files.

Table:

Table PDF File

Text Only Landscape:

Text Only Landscape PDF File

Headers and Footers:

Headers and Footers PDF File

Got any Questions?

We're here to help you.