Module: SelectPdf

Defined in:
lib/selectpdf.rb

Overview

SelectPdf Online REST API Ruby client library. Contains HTML to PDF converter, PDF merge, PDF to text extractor, search PDF.

Convert HTML to PDF

require 'selectpdf'
print "This is SelectPdf-#{SelectPdf::CLIENT_VERSION}\n"

url = 'https://selectpdf.com'
local_file = 'Test.pdf'
api_key = 'Your API key here'

begin
  api = SelectPdf::HtmlToPdfClient.new(api_key)

  api.page_size = SelectPdf::PageSize::A4
  api.margins = 0
  api.page_numbers = FALSE
  api.page_breaks_enhanced_algorithm = TRUE

  api.convert_url_to_file(url, local_file)
rescue SelectPdf::ApiException => e
  print("An error occurred: #{e}")
end

Merge PDFs from local disk or public url and save result into a file on disk.

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

Extract text from PDF

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 Pdf

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

Defined Under Namespace

Classes: ApiClient, ApiException, AsyncJobClient, HtmlToPdfClient, OutputFormat, PageLayout, PageMode, PageNumbersAlignment, PageOrientation, PageSize, PdfMergeClient, PdfToTextClient, RenderingEngine, SecureProtocol, StartupMode, TextLayout, UsageClient, WebElementsClient

Constant Summary collapse

MULTIPART_FORM_DATA_BOUNDARY =

Multipart/form-data boundary

'------------SelectPdf_Api_Boundry_$'
NEW_LINE =

New line

"\r\n"
CLIENT_VERSION =

Library version

'1.4.0'

Instance Attribute Summary collapse

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.


175
176
177
# File 'lib/selectpdf.rb', line 175

def code
  @code
end

#messageObject (readonly)

Returns the value of attribute message.


175
176
177
# File 'lib/selectpdf.rb', line 175

def message
  @message
end