How to convert Word DOCX to PDF in Ruby

Cloudmersive
1 min readAug 9, 2019

Here is a very simple use of one of our APIs to convert any document file (including DOCX) to a PDF. Let’s dive in.

Start with installing the client to your Gemfile, as follows:

gem 'cloudmersive-convert-api-client', '~> 1.3.3'

Then we call the convert_document_autodetect_to_pdf function:

# 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/file") # File | Input file to perform the operation on.begin
#Convert Document to PDF
result = api_instance.convert_document_autodetect_to_pdf(input_file)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling ConvertDocumentApi->convert_document_autodetect_to_pdf: #{e}"
end

And it’s done! This function will automatically detect whatever type of document it is given and then convert it into the desired PDF format. Supported formats include PPT(X), XLS(X), and many others.

--

--

Cloudmersive

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