Module: ImageToPdf

Defined in:
lib/image_to_pdf.rb,
lib/image_to_pdf/version.rb

Overview

Converts image to pdf

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.convert(path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/image_to_pdf.rb', line 32

def self.convert(path)
  if is_url?(path)
    filename = path.split("/").last
    filename = "sample" if filename.empty?
    downloaded_file = URI.parse("#{path}").open

    File.open(filename, 'wb') { |f| f.write(downloaded_file.read) }   
    f = File.open(filename, "r")
    name = File.basename(f,File.extname(f))
    to_pdf(f.path, name)
    File.delete(f)
  else
    extn = File.extname path
    filep = File.dirname(path)
    filename = File.basename path, extn
    to_pdf(path, filename, filep)
  end

end

.is_url?(str) ⇒ Boolean

working in progress: accepts image urls and converts into pdf

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
# File 'lib/image_to_pdf.rb', line 10

def self.is_url?(str)
  begin
    res = open(str).status
    true if res[0] == "200"
  rescue => e
    false
  end   
end

.to_pdf(path, filename, filep = ".") ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/image_to_pdf.rb', line 19

def self.to_pdf(path, filename, filep=".")
  img = Magick::ImageList.new(path)

  img = Magick::ImageList.new(path)
  if %w(GIF PNG JPEG SVG).include? img.format
    img.write(filep + "/" + filename + ".pdf")
    STDOUT.puts "\033[107;31m Your file saved in #{path}\033[0m"
  else
#      raise StandardError, "Please enter valid PDF file path!"
     STDOUT.puts "Error: Please enter valid PDF file path!"
  end
end