How to Convert DOCX to JPG in Ruby
Manually converting Word documents to another file format can be a complex task that requires hundreds of lines of code and a whole lot of patience. If this is a struggle you’re encountering, this brief tutorial will provide with an easy way to disentangle the conversion process for DOCX to JPG by using an API in Ruby.
Let’s begin by adding the Ruby client to our Gemfile:
gem 'cloudmersive-convert-api-client', '~> 2.1.6'
Once this is complete, we can simply input our DOCX file and API key into the following example 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'
endapi_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 Word DOCX Document to JPG/JPEG image array
result = api_instance.convert_document_docx_to_jpg(input_file, opts)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling ConvertDocumentApi->convert_document_docx_to_jpg: #{e}"
end
The operation will be completed in seconds and will return an array of JPG/JPEG images — one for each page of the original Word document. If you need to obtain an API key, you can do so by visiting the Cloudmersive website and registering for a free account; this will provide 800 calls/month across any of our APIs.