Cloud Render using Ruby

This example shows how to generate a PDF from a DOCX template using Ruby.  It calls the Docmosis REST API to merge the data with the template and stream the result back.

The sample code includes the instructions to get started. You will need a Free Trial and then plug your Docmosis Cloud access key into the code below, then run.

Note: This code sample is written to specifically work with DWS4.

# This Ruby example calls the Docmosis Cloud service to create a PDF document
# from the default template (WelcomeTemplate.doc).
#
# Copyright Docmosis 2022
#
require 'uri'
require 'net/http'
require 'openssl'

# RESTful service host. Set your region here.
# End-point in the USA     
url = URI("https://us1.dws4.docmosis.com/api/render")
# End-point in the EU
#url = URI("https://eu1.dws4.docmosis.com/api/render")
# End-point in AUS
#url = URI("https://au1.dws4.docmosis.com/api/render")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

# Set your access key here. This is visible in your cloud account in the Docmosis Cloud Console.
# It is your key to accessing your service - keep it private and safe.   
ACCESS_KEY='XXXX';
TEMPLATE_NAME='samples/WelcomeTemplate.docx';
OUTPUT_NAME='result.pdf';
DATA='{"title":"My Example", "messages":[' +
   '{"msg":"the sun is shining"},' + 
   '{"msg":"Great"},' + 
   '{"msg":"back to work"},' + 
   ']}';

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{" +
  "\"accessKey\": \"#{ACCESS_KEY}\", " +
  "\"templateName\": \"#{TEMPLATE_NAME}\",\n  " +
  "\"outputName\": \"#{OUTPUT_NAME}\",\n " +
  "\"data\":#{DATA} " +
  "\n}"

response = http.request(request)

case response.code
when '200'
  File.open(OUTPUT_NAME,"wb"){|f|f.write(response.body)}
  puts "\"#{OUTPUT_NAME}\" created"
else
  puts "Failed:" + response.body
end

Feedback

Invalid Input

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam. Please also consider contacting support@docmosis.com if you need help with this article.

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Invalid Input

Applies To

Docmosis-Java Tornado Cloud
Version(s) - - DWS4