DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 108 posts at DZone. View Full User Profile

Ruby Web Application for Converting Word Excel PowerPoint & PDF Documents in Cloud

  • submit to reddit
require "sinatra"
require "asposecloudsdk"

get "/" do
  erb :index

post "/convert" do
  unless params[:input_file] && (tmpfile = params[:input_file][:tempfile]) && (name = params[:input_file][:filename])
    return "No file selected"

  app_sid = "67xxxxxd-xxx2-7xx3-1xx7-2xxxxxxxxxxd"
  app_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxx", app_key)

  request_url = ""
  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"
    return "Error: wrong file selected"
  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


//HTML Code Page for selecting file to upload

  <form action="convert" method="post" enctype="multipart/form-data">
    <label>Input file
      <input type="file" name="input_file"/>
    <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>
    <button type=submit>Upload</button>

This tutorial explains the steps for 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. Aspose for Cloud is a document creation, conversion and automation platform. Its cloud-based nature and REST APIs make it even powerful to plug into any application within minutes. The SDKs make the integration process much easier. We will be using the Ruby and Bundle, Sinatra, Aspose for Cloud and Heroku in this project. Document conversion is the most basic feature provided by Aspose for Cloud APIs. You might be interested to learn about other features now. For step by step explanation please visit the tutorial page.