Convert to PDF
This guide will demonstrate the process converting PowerPoint file 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
To get the example navigate to the path presentation/convert_to_pdf
folder in the unioffice-examples
directory.
cd unioffice-examples/presentation/convert_to_pdf/
How it works
The example code starts by importing the unioffice
packages and other necessary Go libraries. This is done in lines 8-18
, in the import
section.
The init
function in lines 19-38
, sets the metered key needed for this code to run. Since this example requires both UniOffice
and UniPDF
licenses, both license are set in this function.
The presentation files to be converted to PDF are listed using a slice in lines 40-51
and assigned to the filenames
variable.
The main
function is defined in lines 53-68
. Here the for
loop in lines 50-62
, iterates through each file name, constructs the full path of the input file, and converts them to PDF. The conversion is done using the following code:
outputPath := fmt.Sprintf("output/%s.pdf", filename)
doc, err := document.Open(filename + ".docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
defer doc.Close()
c := convert.ConvertToPdf(doc)
err = c.WriteToFile(outputPath)
Run the code
Run this command to run the example code and convert the PPTX to pdf file.
go run main.go
Sample output