DOCX, XSLX to PDF Conversion - OfficeToPDF - Go Sample Code

Sample code for using Apryse Server SDK to convert Office documents to PDF (including Word, Excel, PowerPoint and Publisher) without needing any external dependencies or MS Office licenses. Office to PDF conversion can be performed on a Linux or Windows server to automate Office-centric workflows, or entirely in the user's client (web browser, mobile device). The conversion functionality can be combined with our Viewer to display or annotate Office files (docx, xlsx, pptx) on all major platforms, including Web, Android, iOS, Xamarin, UWP, and Windows. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB.

Learn more about our Server SDK and Office Document Conversion Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 . "pdftron"
10)
11
12import "pdftron/Samples/LicenseKey/GO"
13
14//------------------------------------------------------------------------------
15// The following sample illustrates how to use the PDF.Convert utility class
16// to convert MS Office files to PDF
17//
18// This conversion is performed entirely within the PDFNet and has *no*
19// external or system dependencies dependencies -- Conversion results will be
20// the same whether on Windows, Linux or Android.
21//
22// Please contact us if you have any questions.
23//------------------------------------------------------------------------------
24
25// Relative path to the folder containing the test files.
26var inputPath = "../../TestFiles/"
27var outputPath = "../../TestFiles/Output/"
28
29func SimpleDocxConvert(inputFileName string, outputFileName string){
30 // Start with a PDFDoc (the conversion destination)
31 pdfdoc := NewPDFDoc()
32
33 // perform the conversion with no optional parameters
34 ConvertOfficeToPDF(pdfdoc, inputPath + inputFileName, NewConversionOptions())
35
36 // save the result
37 pdfdoc.Save(outputPath + outputFileName, uint(SDFDocE_linearized))
38
39 // And we're done!
40 fmt.Println("Saved " + outputFileName )
41}
42
43func FlexibleDocxConvert(inputFileName string , outputFileName string){
44 // Start with a PDFDoc (the conversion destination)
45 pdfdoc := NewPDFDoc()
46
47 options := NewOfficeToPDFOptions()
48
49 // set up smart font substitutions to improve conversion results
50 // in situations where the original fonts are not available
51 options.SetSmartSubstitutionPluginPath(inputPath)
52
53 // create a conversion object -- this sets things up but does not yet
54 // perform any conversion logic.
55 // in a multithreaded environment, this object can be used to monitor
56 // the conversion progress and potentially cancel it as well
57 conversion := ConvertStreamingPDFConversion(pdfdoc, inputPath + inputFileName, options)
58
59 // Print the progress of the conversion.
60 // print( "Status: " + str(conversion.GetProgress()*100) +"%, " +
61 // conversion.GetProgressLabel())
62
63 // actually perform the conversion
64 // this particular method will not throw on conversion failure, but will
65 // return an error status instead
66 for {
67 if (conversion.GetConversionStatus() != DocumentConversionEIncomplete){
68 break
69 }
70 conversion.ConvertNextPage()
71 // print out the progress status as we go
72 // print("Status: " + str(conversion.GetProgress()*100) + "%, " +
73 // conversion.GetProgressLabel() )
74 }
75
76 if(conversion.GetConversionStatus() == DocumentConversionESuccess){
77 numWarnings := conversion.GetNumWarnings()
78 // print information about the conversion
79 for i := uint(0); i < numWarnings; i++ {
80 fmt.Println("Conversion Warning: " + conversion.GetWarningString(i) )
81 i = i + 1
82 }
83 // save the result
84 pdfdoc.Save(outputPath + outputFileName, uint(SDFDocE_linearized))
85 // done
86 fmt.Println("Saved " + outputFileName )
87 }else{
88 fmt.Println("Encountered an error during conversion: " + conversion.GetErrorString() )
89 }
90}
91
92func main(){
93 // The first step in every application using PDFNet is to initialize the
94 // library. The library is usually initialized only once, but calling
95 // Initialize() multiple times is also fine.
96 PDFNetInitialize(PDFTronLicense.Key)
97 PDFNetSetResourcesPath("../../Resources")
98
99 // first the one-line conversion function
100 SimpleDocxConvert("simple-word_2007.docx", "simple-word_2007.pdf")
101
102 // then the more flexible line-by-line conversion API
103 FlexibleDocxConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf")
104 PDFNetTerminate()
105
106}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales