How to Linearize a PDF Document in Ruby

Did you know that linearizing the contents of a PDF will optimize it for streaming download? Enabling streaming download will allow you to view your files while you are waiting; this can be particularly helpful if you download a large amount of files on the web. In this quick tutorial, we will demonstrate how you can use an API in Ruby to automatically linearize a PDF.

To start, we will add the Ruby client to our Gemfile:

gem 'cloudmersive-convert-api-client', '~> 2.1.6'

Next, we can call the function with the following code:

# load the gem
require 'cloudmersive-convert-api-client'
# setup authorization
CloudmersiveConvertApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
api_instance = CloudmersiveConvertApiClient::EditPdfApi.newinput_file ='/path/to/inputfile') # File | Input file to perform the operation on.begin
#Linearize and optimize a PDF for streaming download
result = api_instance.edit_pdf_linearize(input_file)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling EditPdfApi->edit_pdf_linearize: #{e}"

Integrating this simple process will provide you with an opportunity to begin working with your PDF files prior to download completion.

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.