How to Convert an RTF File to JPG in Ruby

Cloudmersive
2 min readJun 29, 2021

--

Rich Text Format (RTF) documents don’t have the greatest track record for ease of use I can’t count the times that RTF has caused me problems with compatibility, nor the times that I have accidentally corrupted an RTF file by saving it in the wrong app. I say, never again! With this simplified means of converting our documents into JPG format, we will be sitting pretty in just a few minutes.

We shall begin by installing the Ruby client:

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

Once the installation is complete, we can call the convert_document_rtf_to_jpg 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.opts = {
quality: 56 # Integer | Optional; Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75. Default value is 75.
}
begin
#Convert Rich Text Format RTF to JPG/JPEG image array
result = api_instance.convert_document_rtf_to_jpg(input_file, opts)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling ConvertDocumentApi->convert_document_rtf_to_jpg: #{e}"
end

That’s it! To retrieve your API key, head over to the Cloudmersive website to register for a free account; this provides 800 monthly calls across all of our APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet