How to convert HTML to PDF in Ruby
1 min readAug 9, 2019
Let’s look at how to convert HTML files to PDFs. This is going to be much easier than you might think. Start by adding the following to your Gemfile to install the Document Conversion API that we need.
gem 'cloudmersive-convert-api-client', '~> 1.3.3'
Now, just call the convert_document_autodetect_to_pdf function as we see here:
# 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/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
You’re done! Now you just need to figure out what to do with all that time you just saved. But seriously, this function on its own can convert any commonly used document format into a PDF. This includes DOCX, PPTX, XLSX, and many others.