Convert an Office Word File to PDF in Ruby

Cloudmersive
2 min readMay 17, 2021

--

Have a contract or manual you created in Word that needs to be easily accessible/shareable to partners and clients? If so, providing the actual DOCX file may not be the best idea since the contents may be altered or edited and loss of the original concept could occur. To seamlessly and safely share these documents electronically, you can automatically convert them to the share-friendly PDF format by using the following API in Ruby.

We will begin by adding the Ruby client to the Gemfile:

gem 'cloudmersive-validate-api-client', '~> 2.1.6'

Then, we are ready to input the target DOCX file and call the function with the following code:

# load the gem
require 'cloudmersive-convert-api-client'
# setup authorization
CloudmersiveConvertApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end
api_instance = CloudmersiveConvertApiClient::ConvertDocumentApi.newinput_file = File.new('/path/to/inputfile') # File | Input file to perform the operation on.begin
#Convert Word DOCX Document to PDF
result = api_instance.convert_document_docx_to_pdf(input_file)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling ConvertDocumentApi->convert_document_docx_to_pdf: #{e}"
end

By the end of this quick operation, you will have a new PDF file! If you need an API key, you can retrieve one by registering for a free account on the Cloudmersive website; this provides access to 800 calls/month across any of our APIs.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.