Convert to PDF
The process of converting a spreadsheet file to PDF using UniOffice
library will be demonstrated in this guide.
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 spreadsheet/convert_to_pdf
folder in the unioffice-examples
directory.
cd unioffice-examples/spreadsheet/convert_to_pdf/
How it works
The import
section and the init
function are defined in lines 6-15
and 17-28
accordingly.
The main
function is defined in lines 33-54
. The for
loop iterates through each file name and converts the spreadsheet to PDF. First a workbook is object is created from a given file using spreadsheet.Open(filename + ".xlsx")
.
Then the for
loop iterates through each sheet of the workbook file and converts it to PDF using:
c := convert.ConvertToPdf(&s)
err = c.WriteToFile(fmt.Sprintf("%s/sheet_%d.pdf", filename, si))
if err != nil {
log.Fatalf("error saving PDF: %s", err)
}
Run the code
To convert the sample spreadsheet to PDF run the following command.
go run main.go
Sample input
Sample Output
Here is a preview first page the converted PDF.