The PDFTron SDK now supports Go Language/GoLang. As a Go developer, you can use PDFTron’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 PDFTron SDK and GoLang

To start, first download and install the PDFTron 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

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

  1. First, navigate into the PDFTron 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.

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

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

package main
package main
import (
	"fmt"
	. "pdftron"
)
  1. 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 )
}
  1. 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
    options.SetSmartSubstitutionPluginPath(inputPath)

    // 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){
			break
		}
		conversion.ConvertNextPage()
		// 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 )
	}else{
        fmt.Println("Encountered an error during conversion: " + conversion.GetErrorString() )
	}
}
  1. 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
    PDFNetInitialize("demo:1630436733976:78fdd6c00300000000222fa9b8dab35afb9cc6a740157513bd83ca26dd")
    
    // Please set below to src/pdftron/PDFNetC/Resources
    PDFNetSetResourcesPath("../../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")
    PDFNetTerminate()
}
  1. 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

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

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

To check out Go/GoLang samples projects, visit our Github page.

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 PDFTron SDK Go Language package.