How to convert HTML to a Word DOCX Document in Ruby

Today’s topic: converting HTML to DOCX made easy. Here’s how:

  1. Add our Convert API Client to your Gemfile with this snippet here.
gem 'cloudmersive-convert-api-client', '~> 1.3.3'

2. Call convert_web_html_to_docx as seen below:

# 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::ConvertWebApi.newinput_request = CloudmersiveConvertApiClient::HtmlToOfficeRequest.new # HtmlToOfficeRequest |begin
#HTML to DOCX
result = api_instance.convert_web_html_to_docx(input_request)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling ConvertWebApi->convert_web_html_to_docx: #{e}"
end

And that’s all she wrote! Images may be included if they have qualified URLs. Rich text formatting also carries over.

Image for post
Image for post

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store