Unlock the Power of Direct PDF Editing with WebViewer 10.7

How to Create PDFs from Microsoft Office Documents using Go Language

By Rachmat Hartano | 2021 Nov 23

Sanity Image
Read time

3 min

The Apryse SDK now supports Go Language/GoLang. As a Go developer, you can use Apryse’s GoLang PDF library and document processing functionalities to create, edit, print, convert, and more in your applications on Windows, Linux, and Mac.

This blog post explains how to run a GoLang DOCX to PDF conversion–without requiring a Microsoft Office license or any other plugins!

Download Apryse SDK and GoLang

Copied to clipboard

To start, first download and install the Apryse SDK and GoLang.

  1. Go to the Go PDF Library and select your platform (Windows, Linux, or Mac).
  2. Follow the instructions to download and run the script under the Initial Setup section.

Convert a Microsoft Office document to PDF in Go

Copied to clipboard

After installing the Apryse SDK GoLang package, you can start converting Office documents.

1. First, navigate into the Apryse Go SDK/Samples directory and create a new directory named myApp (if it does not exist already).

Note: This blog assumes your application is called myApp.

2. Next, create a new directory inside myApp called GO (Samples/myApp/GO).

3. Then, create a Go file named officetopdf.go and define the Go package and imports using the following example:

package main
package main
import (
	. "pdftron"

4. Now, create subfolders called input and output and write a simple function to convert DOCX to PDF:

// Define relative path to the folder containing the test files.
var inputPath = "input/"
var outputPath = "output/"

func SimpleDocxConvert(inputFileName string, outputFileName string){
    // Define pdfdoc
    pdfdoc := NewPDFDoc()

    // Perform the conversion with no optional parameters
    ConvertOfficeToPDF(pdfdoc, inputPath + inputFileName, NewConversionOptions())

    // Save the result with an option to linearize the PDF
    pdfdoc.Save(outputPath + outputFileName, uint(SDFDocE_linearized))

    // And we're done!
    fmt.Println("Saved " + outputFileName )

5. For a more flexible Office to PDF conversion, you can also create the function below. This provides status updates on conversion progress and return warnings/errors.

func FlexibleDocxConvert(inputFileName string , outputFileName string){
    // Start with a PDFDoc (the conversion destination)
    pdfdoc :=  NewPDFDoc()

    options :=  NewOfficeToPDFOptions() 

    // Set up smart font substitutions to improve conversion results
    // in situations where the original fonts are not available

    // Create a conversion object -- this sets things up but does not yet
    // perform any conversion logic.
    // In a multithreaded environment, this object can be used to monitor
    // the conversion progress and potentially cancel it as well
    conversion := ConvertStreamingPDFConversion(pdfdoc, inputPath + inputFileName, options)

    // Print the progress of the conversion.
    // print( "Status: " + str(conversion.GetProgress()*100) +"%, " +
    //        conversion.GetProgressLabel())

    // Actually perform the conversion
    // This particular method will not throw on conversion failure, but will
    // return an error status instead
	for {
		if (conversion.GetConversionStatus() != DocumentConversionEIncomplete){
		// print out the progress status as we go
		// print("Status: " + str(conversion.GetProgress()*100) + "%, " +
		//     conversion.GetProgressLabel() )

    if(conversion.GetConversionStatus() == DocumentConversionESuccess){
        numWarnings := conversion.GetNumWarnings()
        // print information about the conversion
        for i := uint(0); i < numWarnings; i++ {
            fmt.Println("Conversion Warning: " + conversion.GetWarningString(i) )
            i = i + 1
        // Save the result with linearized option
        pdfdoc.Save(outputPath + outputFileName, uint(SDFDocE_linearized))
        // done
        fmt.Println("Saved " + outputFileName )
        fmt.Println("Encountered an error during conversion: " + conversion.GetErrorString() )

6. Finally, declare the main function in Go:

func main(){
    // Initialize PDFNet with demo license key
    // License key can be obtained from https://www.pdftron.com/pws/get-key
    // Please set below to src/pdftron/PDFNetC/Resources

    // Execute the simple conversion function. Make sure to supply input1.docx in the input folder
    SimpleDocxConvert("input1.docx", "output1.pdf")

    // Execute the more flexible line-by-line conversion API. 
   //  Make sure to supply input2.docx in the input folder
    FlexibleDocxConvert("input2.docx", "output2.pdf")

7. Make sure your interpreter finds the PDFNetC library, then run your application by executing:

go run officetopdf.go

And that’s it! You’ve created a PDF by converting an Office document using the GoLang PDF library.

Next Steps

Copied to clipboard

In this blog, we showed just how easy it is to generate PDFs from Office documents using the new Apryse SDK for Go PDF Library.

For Apryse SDK for Go documentation, sample codes, and other resources, visit the Go PDF library.

And as always, we are here to help; if you have trouble with any of the steps here, or with getting started, feel free to contact us. We're happy to answer any questions about the Apryse SDK Go Language package.

Sanity Image

Rachmat Hartano

Share this post