Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. Since its public release in 1995, Ruby has drawn devoted coders worldwide. In 2006, Ruby achieved mass acceptance. Ruby is also completely free. Not only free of charge, but also free to use, copy, modify, and distribute.
Because of its popularity, we are sure that a lot of you write your code in Ruby, so SelectPdf team decided to put together a few samples for our Html To Pdf API to make your life a little easier if you need to convert from web pages to pdf in your Ruby applications.
Later Edit: The recommended approach to access SelectPdf Online REST API from Ruby is to use the dedicated Ruby Client Library for SelectPdf API. Alternatively, you can write your own code to access the API. Below are some examples for this approach.
Example #1 – How to use Ruby to convert a webpage to PDF and save it on the disk
This code converts an url to pdf in Ruby using SelectPdf HTML To PDF API through a GET request. The content is saved into a file on the disk.
# This code converts an url to pdf in Ruby using SelectPdf REST API through a GET request.
# The content is saved into a file on the disk.
require 'net/http'
api_endpoint = 'https://selectpdf.com/api2/convert/'
key = 'your license key here'
test_url = 'https://selectpdf.com'
local_file = 'test.pdf'
parameters = {
"key" => key,
"url" => test_url
}
encoded_parameters = URI.encode(parameters.map{|k,v| "#{k}=#{v}"}.join("&"))
uri = URI("#{api_endpoint}?#{encoded_parameters}")
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new uri.request_uri
http.request request do |response|
if (response.code == '200') then
# on success - save generated pdf document into a file
open local_file, 'wb' do |io|
response.read_body do |chunk|
io.write chunk
end
end
print "Test pdf document generated successfully!\n"
else
# error - show response code and message
print "HTTP Response code: " + response.code + "\n"
print "HTTP Response message: " + response.message + "\n"
end
end
end
Example #2 – How to use Ruby to convert a webpage to PDF with a POST API request and save it on the disk
This code converts an url to pdf in Ruby using SelectPdf HTML To PDF REST API through a POST request. The parameters are url encoded. The content is saved into a file on the disk.
# This code converts an url to pdf in Ruby using SelectPdf REST API through a POST request.
# The content is saved into a file on the disk.
require 'net/http'
api_endpoint = 'https://selectpdf.com/api2/convert/'
key = 'your license key here'
test_url = 'https://selectpdf.com'
local_file = 'test.pdf'
parameters = {
"key" => key,
"url" => test_url
}
uri = URI(api_endpoint)
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Post.new uri.request_uri
request.set_form_data(parameters)
http.request request do |response|
if (response.code == '200') then
# on success - save generated pdf document into a file
open local_file, 'wb' do |io|
response.read_body do |chunk|
io.write chunk
end
end
print "Test pdf document generated successfully!\n"
else
# error - show response code and message
print "HTTP Response code: " + response.code + "\n"
print "HTTP Response message: " + response.message + "\n"
end
end
end
Example #3 – How to use Ruby to convert a web page to PDF with a POST API request and save it on the disk
This code converts an url to pdf in Ruby using SelectPdf HTML To PDF REST API through a POST request. The parameters are JSON encoded. The content is saved into a file on the disk.
# This code converts an url to pdf in Ruby using SelectPdf REST API through a POST request.
# The parameters are JSON encoded.
# The content is saved into a file on the disk.
require 'net/http'
require 'json'
api_endpoint = 'https://selectpdf.com/api2/convert/'
key = 'your license key here'
test_url = 'https://selectpdf.com'
local_file = 'test.pdf'
parameters = {
"key" => key,
"url" => test_url
}
uri = URI(api_endpoint)
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Post.new uri.request_uri
request.body = parameters.to_json
request["Content-Type"] = "application/json"
http.request request do |response|
if (response.code == '200') then
# on success - save generated pdf document into a file
open local_file, 'wb' do |io|
response.read_body do |chunk|
io.write chunk
end
end
print "Test pdf document generated successfully!\n"
else
# error - show response code and message
print "HTTP Response code: " + response.code + "\n"
print "HTTP Response message: " + response.message + "\n"
end
end
end
The above samples use only the 2 mandatory parameters (key and url or html) for the html to pdf conversion. For the full list of optional parameters, take a look at SelectPdf HTML To PDF REST API page. Using the API optional parameters, you can control the page size, orientation and margins in the pdf document, the document information and security, the headers and footers and a lot more.
Hopefully these examples will help you to be on your way using the API for URL to PDF conversion in Ruby.
The above Ruby samples can also be found in the following GitHub repository:
https://github.com/selectpdf/selectpdf-api-ruby-samples
