Class: HyperPDF

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperpdf.rb,
lib/hyperpdf/version.rb,
lib/hyperpdf/exceptions.rb

Defined Under Namespace

Classes: AuthorizationRequired, ContentRequired, InternalServerError, NoSuchBucket, PaymentRequired

Constant Summary collapse

VERSION =
"0.2.1"

Instance Method Summary collapse

Constructor Details

#initialize(content, options = {}) ⇒ HyperPDF

Initializes new HyperPDF object

Full list of PDF options see on HyperPDF page

Parameters:

  • content (String)

    HTML document or URL

  • options (Hash) (defaults to: {})

    Authorization and PDF options

Options Hash (options):

  • :user (String)

    If provided, sets user name (by default uses ENV variable)

  • :password (String)

    If provided, sets user password (by default uses ENV variable)

Raises:



16
17
18
19
20
21
22
23
24
25
# File 'lib/hyperpdf.rb', line 16

def initialize(content, options = {})
  raise HyperPDF::ContentRequired if content.nil? || content.empty?
  @content = content

  @options = options
  @user = @options.delete(:user) || ENV["HYPERPDF_USER"]
  @password = @options.delete(:password) || ENV["HYPERPDF_PASSWORD"]

  @request_body = { user: @user, password: @password, content: @content, options: @options }
end

Instance Method Details

#getString

Generates PDF

Returns:

  • (String)

    Binary string containing the generated document or id of asynchronous job



30
31
32
# File 'lib/hyperpdf.rb', line 30

def get
  make_request.body
end

#upload_to_s3(bucket, key, public = false) ⇒ String

Generates PDF and uploads it to AWS S3

Parameters:

  • bucket (String)

    Your S3 bucket name

  • key (String)

    Name for generated file

  • public (Boolean) (defaults to: false)

    Sets public read access

Returns:

  • (String)

    Url to generated document or id of asynchronous job



40
41
42
43
44
# File 'lib/hyperpdf.rb', line 40

def upload_to_s3(bucket, key, public = false)
  @request_body.merge!(bucket: bucket, key: key, public: public)
  resp = JSON.parse make_request.body
  resp["url"] || resp["id"]
end