Solutions for a Ruby on Rails .ppt or .pptx export
For Ruby on Rails apps that deal a lot with reporting to external clients, it often adds a lot of value if you can export reports that are a little more user friendly. A client that receives a report that is more visually appealing and has data organized in a way that tells a story will likely feel that they got more from your service.
The downside to exporting pdf’s is that it is not editable. What if your app spits out a lot of great looking information but your client account manager wants to make some changes before sending it over. Exporting a powerpoint is one way to get an editable and visually appealing report format that is also easy to open. As an added bonus, you can of course create nice presentations that you can give to clients the way powerpoint was intended.
At our company we decided that the end goal would be to export a file with a .ppt extension (rather than .key or something else) because virtually all slide presentation software can open the .ppt format. After much research we narrowed it down to two options for a Ruby on Rails platform, creating Google Slides, through the Google Slides API, or using the Powerpoint gem in Rails.
Google Slides API Setup
It wasn’t until I became a developer that I realized how truly massive and nerdy Google is. I have seen a lot of API’s doing both basic and complicated things but entering the Google API docs feels like entering a foreign country. The Google ecosystem is huge and they handle things very differently than a regular API call or adding a ruby gem.
Google has a great quickstart walkthrough that you can find here. I won’t re-create that walk through but I think there are a few things to add if you are newer to programming and working in the Rails environment.
After the first step where you add your project to Google and download the configuration file, place the file credentials.json
in your rails app. I placed it in the root directory and it is probably a safe idea to add credentials.json
to your .gitignore
file so that your Github wont add the file.
On step 3, Google gives you the quickstart.rb file. I placed the first 30 lines of code (through the end of the “authorize” method) into an initializer file config/initializers/google-slides-service.rb
require 'google/apis/slides_v1'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'fileutils'
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
APPLICATION_NAME = 'Google Slides API Ruby Quickstart'.freeze
CREDENTIALS_PATH = 'credentials.json'.freeze
TOKEN_PATH = 'token.yaml'.freeze
SCOPE = Google::Apis::SlidesV1::AUTH_PRESENTATIONS_READONLY
##
# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.
#
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
def authorize
client_id = Google::Auth::ClientId.from_file(CREDENTIALS_PATH)
token_store = Google::Auth::Stores::FileTokenStore.new(file: TOKEN_PATH)
authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
user_id = 'default'
credentials = authorizer.get_credentials(user_id)
if credentials.nil?
url = authorizer.get_authorization_url(base_url: OOB_URI)
puts 'Open the following URL in the browser and enter the ' \
"resulting code after authorization:\n" + url
code = gets
credentials = authorizer.get_and_store_credentials_from_code(
user_id: user_id, code: code, base_url: OOB_URI
)
end
credentials
end
and then created a service file in the lib folder lib/services/google-slides.rb
where I put the rest of the code.
service = Google::Apis::SlidesV1::SlidesService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
# Prints the number of slides and elements in a sample presentation:
# https://docs.google.com/presentation/d/1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc/edit
presentation_id = '1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc'
presentation = service.get_presentation(presentation_id)
puts "The presentation contains #{presentation.slides.count} slides:"
presentation.slides.each_with_index do |slide, i|
puts "- Slide \##{i + 1} contains #{slide.page_elements.count} elements."
end
# [END slides_quickstart]
Eventually I will want to create a class or module around creating and editing slides, but for now I just wanted to get things working.
To test the code, I copied and pasted the lines from my service file (google-slides.rb) into my rails console. The console then gave me instructions from Google to visit a particular URL. After visiting and logging in I copied the code it gave me back into my rails console and pressed enter. After that the connection to Google’s API was made and I was able to run commands normally.
What Does Google Have to Offer?
From my quick study it looks like everything you can do in Google slides on their web app you can also do through the API. It looks easy enough to create a new presentation, create slides and even export files from Google Drive. The API even will let you add transitions, speaker notes, and work with themes. The API can get as complicated as you would like it to and it looks possible to make the finest detail changes all through the API.
Because you have the ability to edit slides from the API, you have another advantage using Google Slides. Using the Google web app, you can create any kind of theme or template on the Google app and then make any kinds of edits you want through your app. This could be really helpful when getting started. You can even create an MVP offering where you do most of the work in the Google app and then add your data through the Google API.
On the other hand, if you use the Google API, you are bound to the Google world. This means that you will be storing all of your slides and presentations on Google Drive. Because Google is a dynamic and growing company, you are also more likely to encounter changes to the API than you would using a gem which is version controlled. Overall, you relinquish some control, but what you gain in adaptability is huge.
Powerpoint Gem
Definitely the biggest advantage to the Powerpoint app is the ease of set up. You can find the set up here, but it is literally just installing the gem and then you are good to go.
However, what you can do with the gem is kind of limited. It is really easy to add slides and create presentations. All you need to do is create a new object
@deck = Powerpoint::Presentation.new
and add slides (your options are textual slide, pictorial slide, or a mix of the two)
@deck.add_pictorial_slide title, image_path
“title” and “image_path” are variables you set ahead of time. Finally you save the new presentation
@deck.save(‘test.pptx’)
and it exports the file as a .pptx at whatever file path you specify. You can adjust picture position relatively easily but that appears to be the extent of built in customization.
Setting up and using the Powerpoint gem could not be easier, however, I have not found anything that will enable you to modify slides beyond adding text and pictures. The gem is cleanly built, so I would expect that it would be relatively straightforward to build out that stuff yourself.
In the end it really depends on your end goal. If you want to start exporting your data into nicely designed slides, it may be worth figuring out the Google API. If you are more concerned about owning your content and building a code infrastructure that you control, than the ruby gem might be best.
Top comments (0)