Document conversion has never been easier in a ruby application. It is just a matter of few lines now. Here an tutorial explaining the steps to creating a complete working web application that can
convert Microsoft Word, Excel, PowerPoint and PDF documents online to a bunch of supported file formats in a Ruby web application. We will be using the following:
- Ruby and Bundler
- Sinatra
- Aspose for Cloud
- Heroku
Let’s assume that you already have installed and configured ruby and bundler on your system. Check http://rvm.io or http://rubyinstaller.org/ for a quick setup. Let’s start. First we need a directory to keep all of our good stuff together. Let’s call it doconv (short of document converter). All files that we create and all commands that we run are inside doconv directory.
Create a file named Gemfile. This is where we tell our application what else we need to run our application. It is basically a list of third-party tools and libraries that we need. We add sinatra (a web application framework) and thin (a web application server) gems. Put the following stuff in Gemfile.
Gemfile
source "https://rubygems.org"
gem "sinatra"
gem "thin"
Create another file called app.rb. This is where our actual application source code lives inside. Since it is too small, we don't have to write multi-file modular application. Create a directory called views and a file index.erb inside views directory. We want to load the file index.erb inside app.rb. Here come the contents of both files:
app.rb
require "sinatra"
get "/" do
erb :index
end
views/index.erb
<h3>Hello World</h3>
You have created your first web application using Sinatra. Let’s run it. Open your console, terminal, command prompt or whatever you call it. Make sure you are in doconv directory. Run the following command:
bundle install
The command may take a while as it will be downloading stuff for your tiny application and will make your application ready. Now use the following command from the same directory to run your application.
bundle exec ruby app.rb
You will see Listening on localhost:4567, CTRL+C to stop. This is where you can launch your web browser and see "Hello World" at http://localhost:4567/ URL.
So simple! That was a hello world. Let us convert documents now. Add some HTML to our application first. For that we edit our views/index.erb. It should be a simple HTML form that allows us to upload HTML file along with a drop-down menu to select between various output formats for conversion.
views/index.erb
<html>
<head><title>doconv</title></head>
<body>
<form action="convert" method="post" enctype="multipart/form-data">
<label>Input file
<input type="file" name="input_file"/>
</label>
<br/>
<label>Output format
<select name="format">
<option value="pdf">PDF</option><option value="tiff">TIFF</option>
<option value="xps">XPS</option><option value="svg">SVG</option>
<option value="docx">DOCX</option><option value="doc">DOC</option>
<option value="xlsx">XLSX</option><option value="xls">XLS</option>
<option value="text">Text</option>
</select>
</label>
<br/>
<button type=submit>Upload</button>
</form>
</body>
</html>
Now we have a file upload option in our application along with a submit button to start the upload. It’s time to add some code for document conversion. We will need Aspose for Cloud ruby SDK. Add the SDK to our Gemfile.
Gemfile
gem "rest-client"
gem "asposecloudsdk", :github => "asposeforcloud/Aspose_Cloud_SDK_For_Ruby"
Run bundle again in your doconv directory to let it download and install the SDK for you.
bundle install
Now comes the coding part. Add the following line to the top of app.rb to load Aspose for Cloud ruby SDK:
app.rb
require "asposecloudsdk"
We create another endpoint /covert to handle uploaded documents. We shall use the file extension to detect the type of file, and call the appropriate API for conversion to the requested format.
app.rb
post "/convert" do
unless params[:input_file] && (tmpfile = params[:input_file][:tempfile]) && (name = params[:input_file][:filename])
return "No file selected"
end
app_sid = "67xxxxxd-xxx2-7xx3-1xx7-2xxxxxxxxxxd"
app_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
Aspose::Cloud::Common::AsposeApp.new(app_sid, app_key)
request_url = "http://api.aspose.com/v1.1/"
if /^.+\.(docx|doc|rtf)$/ =~ params[:input_file][:filename]
request_url += "words/convert"
elsif /^.+\.(xlsx|xls)$/ =~ params[:input_file][:filename]
request_url += "cells/convert"
elsif /^.+\.(pptx|ppt)$/ =~ params[:input_file][:filename]
request_url += "slides/convert"
elsif /^.+\.(pdf)$/ =~ params[:input_file][:filename]
request_url += "pdf/convert"
else
return "Error: wrong file selected"
end
request_url += "?format=" + params[:format]
signed_request_url = Aspose::Cloud::Common::Utils.sign(request_url)
converted_file_stream = RestClient.put(signed_request_url, params[:input_file][:tempfile])
response.headers["Content-Type"] = "application/octect-stream"
response.headers["Content-Disposition"] = "attachment; filename=" + params[:input_file][:filename] + "." + params[:format]
return converted_file_stream
end
We actually have made our document converter application. Lets explain the above rocket science.
app.rb
app_sid = "67xxxxxd-xxx2-7xx3-1xx7-2xxxxxxxxxxd"
app_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
Aspose::Cloud::Common::AsposeApp.new(app_sid, app_key)
You will need to use App SID and App Key. Just visit http://cloud.aspose.com and signup for free. All API request URLs must be signed to protect your account. The SDK already provide this feature. All you have to do is to sign each URL before sending the request. You do not need to sign it again if same request is sent multiple times.
app.rb
signed_request_url = Aspose::Cloud::Common::Utils.sign(request_url)
After we have a signed URL it’s just a simple HTTP request. We have used RestClient here, which makes it a one-line statement for us to send input document as a request body and retrieve converted document as response stream. The response stream returned from Aspose for Cloud API request is send back to web browser. Thanks to Aspose for Cloud, it supports many document formats.
Run your document converter and enjoy. You might want to do some crazy things with it. Yes, we know that a PDF cannot be converted into an Excel sheet. Don't try that - your application might crash or throw strange exception. Be smart and don't allow user errors in your application. Why not run our document converter on Heroku. Heroku is a platform that makes it possible for you to deploy your application in just a few minutes. You just need to create Procfile in your doconv directory with following contents in it:
Procfile
web: bundle exec ruby app.rb -p $PORT
Assuming that you already have Heroku Toolbelt installed and configured. Save all files in a Git repository and push to Heroku.
git init .
git add .
git commit -m "Initial import"
heroku create
git push heroku master
Your document converter is available on the internet and ready to use. Document conversion is the most basic feature provided by Aspose for Cloud APIs. You might be interested to learn about other features now. If you are using a paid plan, the documents processed by users of your applications will be counted against your account and billed accordingly.
Overview: Aspose for Cloud
Aspose for Cloud is a cloud-based document generation, conversion and automation platform for developers that offer a unique suite of APIs to work with Word documents, Excel spreadsheets, PowerPoint presentations, PDFs, and email formats and protocols. It supports all features for file processing, document scanning, barcodes creation and recognition, and allows extracting text or images too. You can also work with SaaSpose APIs using REST SDKs that can be called from .NET, Java, PHP and Ruby etc.