Method: CombinePDF.new

Defined in:
lib/combine_pdf/api.rb

.new(string = false) ⇒ Object

creats a new PDF object.

Combine PDF will check to see if string is a filename. If it’s a file name, it will attempt to load the PDF file using CombinePDF.load. Otherwise it will attempt parsing string using CombinePDF.parse.

If the string is empty it will return a new PDF object (the same as parse).

For both performance and code readability reasons, CombinePDF.load and CombinePDF.parse should be preffered unless creating a new PDF object.

Raises:

  • (TypeError)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/combine_pdf/api.rb', line 23

def new(string = false)
  return PDF.new unless string
  raise TypeError, "couldn't create PDF object, expecting type String" unless string.is_a?(String) || string.is_a?(Pathname)
  begin
    (begin
      File.file? string
    rescue
      false
    end) ? load(string) : parse(string)
  rescue => _e
    raise 'General PDF error - Use CombinePDF.load or CombinePDF.parse for a non-general error message (the requested file was not found OR the string received is not a valid PDF stream OR the file was found but not valid).'
  end
end