DOCX, XSLX to PDF Conversion - OfficeToPDF - Ruby 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-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#------------------------------------------------------------------------------
13# The following sample illustrates how to use the PDF.Convert utility class
14# to convert MS Office files to PDF
15#
16# This conversion is performed entirely within the PDFNet and has *no*
17# external or system dependencies dependencies -- Conversion results will be
18# the same whether on Windows, Linux or Android.
19#
20# Please contact us if you have any questions.
21#------------------------------------------------------------------------------
22
23# Relative path to the folder containing the test files.
24$inputPath = "../../TestFiles/"
25$outputPath = "../../TestFiles/Output/"
26
27def SimpleDocxConvert(input_filename, output_filename)
28 # Start with a PDFDoc (the conversion destination)
29 pdfdoc = PDFDoc.new()
30
31 # perform the conversion with no optional parameters
32 inputFile = $inputPath + input_filename
33 Convert.OfficeToPDF(pdfdoc, inputFile, nil)
34
35 # save the result
36 outputFile = $outputPath + output_filename
37 pdfdoc.Save(outputFile, SDFDoc::E_linearized)
38
39 # And we're done!
40 puts "Saved " + output_filename
41end
42
43def FlexibleDocxConvert(input_filename, output_filename)
44 # Start with a PDFDoc (the conversion destination)
45 pdfdoc = PDFDoc.new()
46
47 options = OfficeToPDFOptions.new()
48
49 # set up smart font substitutions to improve conversion results
50 # in situations where the original fonts are not available
51 inputFile = $inputPath
52 options.SetSmartSubstitutionPluginPath(inputFile)
53
54 # create a conversion object -- this sets things up but does not yet
55 # perform any conversion logic.
56 # in a multithreaded environment, this object can be used to monitor
57 # the conversion progress and potentially cancel it as well
58 inputFile = $inputPath + input_filename
59 conversion = Convert.StreamingPDFConversion(pdfdoc, inputFile, options)
60
61 # Print the progress of the conversion.
62 # puts "Status " + (conversion.GetProgress()*100).to_s + "%, " +
63 # conversion.GetProgressLabel()
64
65 # actually perform the conversion
66 # this particular method will not throw on conversion failure, but will
67 # return an error status instead
68 while (conversion.GetConversionStatus() == DocumentConversion::EIncomplete)
69 conversion.ConvertNextPage()
70 # print out the progress status as we go
71 # puts "Status " + (conversion.GetProgress()*100).to_s + "%, " +
72 # conversion.GetProgressLabel()
73 end
74
75 if(conversion.GetConversionStatus() == DocumentConversion::ESuccess)
76 num_warnings = conversion.GetNumWarnings()
77 # print information about the conversion
78 for i in 0..num_warnings-1
79 puts "Conversion Warning " + conversion.GetWarningString(i)
80 end
81
82 # save the result
83 outputFile = $outputPath + output_filename
84 pdfdoc.Save(outputFile, SDFDoc::E_linearized)
85 # done
86 puts "Saved " + output_filename
87 else
88 puts "Encountered an error during conversion " + conversion.GetErrorString()
89 end
90
91end
92
93
94def main()
95 # The first step in every application using PDFNet is to initialize the
96 # library. The library is usually initialized only once, but calling
97 # Initialize() multiple times is also fine.
98 PDFNet.Initialize(PDFTronLicense.Key)
99 PDFNet.SetResourcesPath("../../../Resources")
100
101 # first the one-line conversion function
102 SimpleDocxConvert("simple-word_2007.docx", "simple-word_2007.pdf")
103
104 # then the more flexible line-by-line conversion API
105 FlexibleDocxConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf")
106 PDFNet.Terminate
107 puts "Done."
108end
109
110main()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales