class HyPDF

Constants

HOST
VERSION

Public Class Methods

decryptpdf(file, options = {}) click to toggle source
# File lib/hypdf.rb, line 124
def decryptpdf(file, options = {})
  options.merge!(file: File.new(file))
  response = request('decryptpdf', options).body

  if options[:bucket].nil?
    {pdf: response}
  else
    JSON.parse(response, symbolize_names: true)
  end
end
editmeta(file, options = {}) click to toggle source
# File lib/hypdf.rb, line 56
def editmeta(file, options = {})
  options.merge!(file: File.new(file))
  response = request('editmeta', options).body

  if options[:bucket].nil?
    {pdf: response}
  else
    JSON.parse(response, symbolize_names: true)
  end
end
encryptpdf(file, options = {}) click to toggle source
# File lib/hypdf.rb, line 113
def encryptpdf(file, options = {})
  options.merge!(file: File.new(file))
  response = request('encryptpdf', options).body

  if options[:bucket].nil?
    {pdf: response}
  else
    JSON.parse(response, symbolize_names: true)
  end
end
fillform(file, options = {}) click to toggle source
# File lib/hypdf.rb, line 102
def fillform(file, options = {})
  options.merge!(file: File.new(file))
  response = request('fillform', options).body

  if options[:bucket].nil?
    {pdf: response}
  else
    JSON.parse(response, symbolize_names: true)
  end
end
htmltopdf(content, options = {}) click to toggle source
# File lib/hypdf.rb, line 12
def htmltopdf(content, options = {})
  raise HyPDF::ContentRequired if content.nil? || content.empty?
  options[:content] = content
  response = request('htmltopdf', options)

  result = {
    pages: response.headers['hypdf-pages'].to_i,
    page_size: response.headers['hypdf-page-size'],
    pdf_version: response.headers['hypdf-pdf-version'].to_f
  }

  if options[:callback].nil? && options[:bucket].nil?
    result.merge(pdf: response.body)
  else
    result.merge(JSON.parse(response.body, symbolize_names: true))
  end
end
jobstatus(job_id, options = {}) click to toggle source
# File lib/hypdf.rb, line 30
def jobstatus(job_id, options = {})
  options[:job_id] = job_id
  options[:user] ||= ENV["HYPDF_USER"]
  options[:password] ||= ENV["HYPDF_PASSWORD"]

  response = HTTParty.get(
    "#{HyPDF::HOST}/jobstatus",
    query: options
  )
  case response.code
  when 200 then JSON.parse(response.body)
  when 400 then raise HyPDF::ContentRequired
  when 401 then raise HyPDF::AuthorizationRequired
  when 402 then raise HyPDF::PaymentRequired
  when 403 then raise HyPDF::S3AccessDenied
  when 404 then raise HyPDF::NoSuchBucket
  when 408 then raise HyPDF::RequestTimeout
  when 500 then raise HyPDF::InternalServerError
  end
end
pdfextract(file, options = {}) click to toggle source
# File lib/hypdf.rb, line 72
def pdfextract(file, options = {})
  options.merge!(file: File.new(file))
  response = request('pdfextract', options).body

  if options[:bucket].nil?
    {pdf: response}
  else
    JSON.parse(response, symbolize_names: true)
  end
end
pdfinfo(file, options = {}) click to toggle source
# File lib/hypdf.rb, line 51
def pdfinfo(file, options = {})
  options.merge!(file: File.new(file))
  JSON.parse(request('pdfinfo', options).body)
end
pdftotext(file, options = {}) click to toggle source
# File lib/hypdf.rb, line 67
def pdftotext(file, options = {})
  options.merge!(file: File.new(file))
  JSON.parse(request('pdftotext', options).body, symbolize_names: true)
end
pdfunite(*params) click to toggle source
# File lib/hypdf.rb, line 83
def pdfunite(*params)
  options = params.last.is_a?(Hash) ? params.delete_at(-1) : {}
  params.each_with_index do |param, index|
    options.merge!("file_#{index}" => file_for(param, index))
  end
  response = request('pdfunite', options).body

  if options[:bucket].nil?
    {pdf: response}
  else
    JSON.parse(response, symbolize_names: true)
  end
end
readform(file, options = {}) click to toggle source
# File lib/hypdf.rb, line 97
def readform(file, options = {})
  options.merge!(file: File.new(file))
  JSON.parse(request('readform', options).body)
end

Private Class Methods

file_for(param, index) click to toggle source
# File lib/hypdf.rb, line 137
def file_for(param, index)
  if pdf_header?(param)
    uploadable_file(param, "file_#{index}.pdf")
  else
    File.new(param)
  end
end
pdf_header?(arg) click to toggle source
# File lib/hypdf.rb, line 145
def pdf_header?(arg)
  arg.is_a?(String) && arg.start_with?('%PDF-')
end
request(method, body) click to toggle source
# File lib/hypdf.rb, line 153
def request(method, body)
  body[:user] ||= ENV["HYPDF_USER"]
  body[:password] ||= ENV["HYPDF_PASSWORD"]

  response = HTTParty.post("#{HyPDF::HOST}/#{method}", body: body)
  case response.code
  when 200 then response
  when 400 then raise HyPDF::ContentRequired
  when 401 then raise HyPDF::AuthorizationRequired
  when 402 then raise HyPDF::PaymentRequired
  when 403 then raise HyPDF::S3AccessDenied
  when 404 then raise HyPDF::NoSuchBucket
  when 408 then raise HyPDF::RequestTimeout
  when 500 then raise HyPDF::InternalServerError
  end
end
uploadable_file(string, filename) click to toggle source
# File lib/hypdf.rb, line 149
def uploadable_file(string, filename)
  UploadIO.new(StringIO.new(string), 'application/octet-stream', filename)
end