SelectPdf cloud REST API is a platform independent PDF manipulation API. As a true REST API, it can be used with any language: .NET, Java, PHP, Python, Go, Node.js and many more. We are presenting today the dedicated Ruby client library for SelectPdf API.
Using the SelectPdf Online REST API Ruby client library you can easily take advance of the API features offered by SelectPdf:
HTML to PDF REST API – SelectPdf HTML To PDF Online REST API is a professional solution that lets you create PDF from web pages and raw HTML code in your applications.
PDF to TEXT REST API – SelectPdf Pdf To Text REST API is an online solution that lets you extract text from your PDF documents or search your PDF document for certain words.
PDF Merge REST API – SelectPdf Pdf Merge REST API is an online solution that lets you merge local or remote PDFs into a final PDF document.
All these APIs can be easily integrated with Ruby using the dedicated client library.
Installation
Install SelectPdf Ruby Client for Online API via RubyGems.
OR
Download selectpdf-api-ruby-client-1.4.0.zip, unzip it and run:
gem build selectpdf.gemspec
gem install selectpdf-1.4.0.gem
OR
Clone selectpdf-api-ruby-client from Github and install the library.
cd selectpdf-api-ruby-client
gem build selectpdf.gemspec
gem install selectpdf-1.4.0.gem
Get a trial key for SelectPdf online REST API
Once the library is installed, you need a key to be able to access the API.
GET A DEMO LICENSE KEY NOW
The free trial key for the online API is valid for 7 days and it includes 200 conversions.
Sample Code
The Ruby client library makes accessing SelectPdf online REST API very easy. Here are a few samples that present the main features of the API. For details and full list of parameters access the SelectPdf API Ruby documentation on RubyDoc or the individual pages of the APIs: HTML to PDF API or PDF to TEXT API or PDF Merge API.
Convert HTML to PDF in Ruby
The following sample shows the main features of the HTML To PDF API. Comment/uncomment code to convert an url to file or memory or also convert raw HTML to file or memory.
require 'selectpdf'
$stdout.sync = true
print "This is SelectPdf-#{SelectPdf::CLIENT_VERSION}\n"
url = 'https://selectpdf.com'
local_file = 'Test.pdf'
api_key = 'Your API key here'
begin
client = SelectPdf::HtmlToPdfClient.new(api_key)
# set parameters - see full list at https://selectpdf.com/html-to-pdf-api/
client.page_size = SelectPdf::PageSize::A4 # PDF page size
client.page_orientation = SelectPdf::PageOrientation::PORTRAIT # PDF page orientation
client.margins = 0 # PDF page margins
client.rendering_engine = SelectPdf::RenderingEngine::WEBKIT # rendering engine
client.conversion_delay = 1 # conversion delay
client.navigation_timeout = 30 # navigation timeout
client.page_numbers = FALSE # page numbers
client.page_breaks_enhanced_algorithm = TRUE # enhanced page break algorithm
# additional properties
# client.use_css_print = TRUE # enable CSS media print
# client.disable_javascript = TRUE # disable javascript
# client.disable_internal_links = TRUE # disable internal links
# client.disable_external_links = TRUE # disable external links
# client.keep_images_together = TRUE # keep images together
# client.scale_images = TRUE # scale images to create smaller pdfs
# client.single_page_pdf = TRUE # generate a single page PDF
# client.user_password = 'password' # secure the PDF with a password
# generate automatic bookmarks
# client.pdf_bookmarks_selectors = 'H1, H2' # create outlines (bookmarks) for the specified elements
# client.viewer_page_mode = SelectPdf::PageMode::USE_OUTLINES # display outlines (bookmarks) in viewer
print "Starting conversion ...\n"
# convert url to file
client.convert_url_to_file(url, local_file)
# convert url to memory
# pdf = client.convert_url(url)
# convert html string to file
# client.convert_html_string_to_file('This is some html.', local_file)
# convert html string to memory
# pdf = client.convert_html_string('This is some html.')
print "Finished! Number of pages: #{client.number_of_pages}.\n"
# get API usage
usage_client = SelectPdf::UsageClient.new(api_key)
usage = usage_client.get_usage(FALSE)
print("Usage: #{usage}\n")
print('Conversions remained this month: ', usage['available'], "\n")
rescue SelectPdf::ApiException => e
print("An error occurred: #{e}")
end
Convert HTML to PDF with custom header/footer in Ruby
The following sample shows how to convert a web page to PDF and also setting a custom header or footer.
require 'selectpdf'
$stdout.sync = true
print "This is SelectPdf-#{SelectPdf::CLIENT_VERSION}\n"
url = 'https://selectpdf.com'
local_file = 'Test.pdf'
api_key = 'Your API key here'
begin
client = SelectPdf::HtmlToPdfClient.new(api_key)
# set parameters - see full list at https://selectpdf.com/html-to-pdf-api/
client.margins = 0 # PDF page margins
client.page_breaks_enhanced_algorithm = TRUE # enhanced page break algorithm
# header properties
client.show_header = TRUE # display header
# client.header_height = 50 # header height
# client.header_url = url # header url
client.header_html = 'This is the HEADER!!!!' # header html
# footer properties
client.show_footer = TRUE # display footer
# client.footer_height = 60 # footer height
# client.footer_url = url # footer url
client.footer_html = 'This is the FOOTER!!!!' # footer html
# footer page numbers
client.page_numbers = TRUE # show page numbers in footer
client.page_numbers_template = '{page_number} / {total_pages}' # page numbers template
client.page_numbers_font_name = 'Verdanda' # page numbers font name
client.page_numbers_font_size = 12 # page numbers font size
client.page_numbers_alignment = SelectPdf::PageNumbersAlignment::CENTER # page numbers alignment
print "Starting conversion ...\n"
# convert url to file
client.convert_url_to_file(url, local_file)
# convert url to memory
# pdf = client.convert_url(url)
# convert html string to file
# client.convert_html_string_to_file('This is some html.', local_file)
# convert html string to memory
# pdf = client.convert_html_string('This is some html.')
print "Finished! Number of pages: #{client.number_of_pages}.\n"
# get API usage
usage_client = SelectPdf::UsageClient.new(api_key)
usage = usage_client.get_usage(FALSE)
print("Usage: #{usage}\n")
print('Conversions remained this month: ', usage['available'], "\n")
rescue SelectPdf::ApiException => e
print("An error occurred: #{e}")
end
Extract text from PDF in Ruby
The following sample shows how to extract the text from a PDF document using SelectPdf API. Comment/uncomment code to convert a local PDF or a PDF from a remote url to file or memory.
require 'selectpdf'
$stdout.sync = true
print "This is SelectPdf-#{SelectPdf::CLIENT_VERSION}\n"
test_url = 'https://selectpdf.com/demo/files/selectpdf.pdf'
test_pdf = 'Input.pdf'
local_file = 'Result.txt'
api_key = 'Your API key here'
begin
client = SelectPdf::PdfToTextClient.new(api_key)
# set parameters - see full list at https://selectpdf.com/pdf-to-text-api/
client.start_page = 1 # start page (processing starts from here)
client.end_page = 0 # end page (set 0 to process file til the end)
client.output_format = SelectPdf::OutputFormat::TEXT # set output format (Text or HTML)
print "Starting pdf to text ...\n"
# convert local pdf to local text file
client.text_from_file_to_file(test_pdf, local_file)
# extract text from local pdf to memory
# text = client.text_from_file(test_pdf)
# print text
# convert pdf from public url to local text file
# client.text_from_url_to_file(test_url, local_file)
# extract text from pdf from public url to memory
# text = client.text_from_url(test_url)
# print text
print "Finished! Number of pages processed: #{client.number_of_pages}.\n"
# get API usage
usage_client = SelectPdf::UsageClient.new(api_key)
usage = usage_client.get_usage(FALSE)
print("Usage: #{usage}\n")
print('Conversions remained this month: ', usage['available'], "\n")
rescue SelectPdf::ApiException => e
print("An error occurred: #{e}")
end
Search for text in PDF using Ruby
The following sample shows how to search a PDF document for a specific text.
require 'selectpdf'
$stdout.sync = true
print "This is SelectPdf-#{SelectPdf::CLIENT_VERSION}\n"
test_url = 'https://selectpdf.com/demo/files/selectpdf.pdf'
test_pdf = 'Input.pdf'
api_key = 'Your API key here'
begin
client = SelectPdf::PdfToTextClient.new(api_key)
# set parameters - see full list at https://selectpdf.com/pdf-to-text-api/
client.start_page = 1 # start page (processing starts from here)
client.end_page = 0 # end page (set 0 to process file til the end)
client.output_format = SelectPdf::OutputFormat::TEXT # set output format (Text or HTML)
print "Starting search pdf ...\n"
# search local pdf
results = client.search_file(test_pdf, 'pdf')
# search pdf from public url
# results = client.search_url(test_url, 'pdf')
print "Search results: #{results}.\nSearch results count: #{results.length}\n"
print "Finished! Number of pages processed: #{client.number_of_pages}.\n"
# get API usage
usage_client = SelectPdf::UsageClient.new(api_key)
usage = usage_client.get_usage(FALSE)
print("Usage: #{usage}\n")
print('Conversions remained this month: ', usage['available'], "\n")
rescue SelectPdf::ApiException => e
print("An error occurred: #{e}")
end
Merge PDFs using Ruby
The following sample shows how merge several PDF documents into a final file. The source PDFs can be local files or PDFs from remote urls. The final PDF can be retrieved in memory or saved to a local file.
require 'selectpdf'
$stdout.sync = true
print "This is SelectPdf-#{SelectPdf::CLIENT_VERSION}\n"
test_url = 'https://selectpdf.com/demo/files/selectpdf.pdf'
test_pdf = 'Input.pdf'
local_file = 'Result.pdf'
api_key = 'Your API key here'
begin
client = SelectPdf::PdfMergeClient.new(api_key)
# set parameters - see full list at https://selectpdf.com/pdf-merge-api/
# specify the pdf files that will be merged (order will be preserved in the final pdf)
client.add_file(test_pdf) # add PDF from local file
client.add_url_file(test_url) # add PDF from public url
# client.add_file(test_pdf, 'pdf_password') # add PDF (that requires a password) from local file
# client.add_url_file(test_url, 'pdf_password') # add PDF (that requires a password) from public url
print "Starting pdf merge ...\n"
# merge pdfs to local file
client.save_to_file(local_file)
# merge pdfs to memory
# pdf = client.save
print "Finished! Number of pages: #{client.number_of_pages}.\n"
# get API usage
usage_client = SelectPdf::UsageClient.new(api_key)
usage = usage_client.get_usage(FALSE)
print("Usage: #{usage}\n")
print('Conversions remained this month: ', usage['available'], "\n")
rescue SelectPdf::ApiException => e
print("An error occurred: #{e}")
end
The above Ruby samples can also be found in the following GitHub repository:
https://github.com/selectpdf/selectpdf-api-ruby-client/tree/main/samples.
