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 to install and run Tornado.
# This Ruby example calls the local Docmosis Tornado service to create a PDF document # from the default template (WelcomeTemplate.docx). # To run: # 1) install the rest-client gem if you haven't already (see below) # 2) run the code # # Note, this example uses the rest-client Ruby component. # To install: gem install rest-client # Home page https://github.com/rest-client/rest-client # # Copyright Docmosis 2015 # require 'rest-client' require 'date' require 'json' # RESTFul service URL $DWS_RENDER_URL = "http://localhost:8080/api/render" # Your account key $ACCESS_KEY = "" # The template to use # NOTE that it has to be defined in your account with the same name specified here $TEMPLATE = "WelcomeTemplate.docx" # Change with your actual template name # The output file name $OUTPUT='myWelcome.pdf'; def msg (message) {'msg'=>"#{message}"} end # Set the web proxy to use from the environment variable. # The settings for proxy are site-dependent so you may need to # adjust this RestClient.proxy = ENV['http_proxy']; RestClient.post($DWS_RENDER_URL, { 'accessKey' => $ACCESS_KEY, 'templateName' => $TEMPLATE, 'outputName' => $OUTPUT, 'data' => { 'date' => Date.today, 'message' => 'This Tornado Document Engine is working great!', } }.to_json, :content_type => :json) {|response, request, result, &block| case response.code when 200 File.open($OUTPUT,"wb"){|f|f.syswrite(response.body)} puts "\"#{$OUTPUT}\" created" else # 4XX errors - client errors - something needs to be corrected in the request # 5XX errors - server side errors - possibly worth a retry # show error response (details) puts "Error response:\n\n#{response.code} #{response}\n\n" response.return!(request, result, &block) end }