Module: CompilePdf

Defined in:
lib/compile_pdf/version.rb,
lib/compile_pdf/generate_pdf.rb

Constant Summary collapse

VERSION =
"1.0.2"

Instance Method Summary collapse

Instance Method Details

#attachment_type(file, attachment_type) ⇒ Object



26
27
28
# File 'lib/compile_pdf/generate_pdf.rb', line 26

def attachment_type(file, attachment_type)
  file.send(attachment_type)
end

#compile_pdf_module(attachments, attachment_type, file_name = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/compile_pdf/generate_pdf.rb', line 3

def compile_pdf_module(attachments, attachment_type, file_name = nil)
  arr = []
  attachments.each do |a|
    if file_type(a, attachment_type) == "application" && is_pdf?(a, attachment_type)
      arr <<  attachment_type(a, attachment_type).to_s
    end
  end

  file_name = file_name_generate(file_name)

  Prawn::Document.generate(file_name) do  |phr|
    attachments.each do |a|
      if file_type(a, attachment_type) == "application" && is_pdf?(a, attachment_type)
        generate_pdf(phr,a, attachment_type)
      elsif a.file_type == "image"
        generate_image(phr,a, attachment_type)
      elsif file_type(a, attachment_type) != "not_found" && is_pdf?(a, attachment_type) != "not_found"
        generate_text(phr,a, attachment_type)
      end
    end
  end
end

#count_pdf_pages(pdf_file_path) ⇒ Object



51
52
53
54
# File 'lib/compile_pdf/generate_pdf.rb', line 51

def count_pdf_pages(pdf_file_path)
  pdf = Prawn::Document.new(:template => pdf_file_path)
  pdf.page_count
end

#file_name_generate(file_name) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/compile_pdf/generate_pdf.rb', line 30

def file_name_generate(file_name)
  if file_name
    "#{Rails.root}/tmp/#{file_name}.pdf"
  else
    "#{Rails.root}/tmp/#{Time.now}_phr.pdf"
  end
end

#file_type(attachment, attachment_type) ⇒ Object



85
86
87
# File 'lib/compile_pdf/generate_pdf.rb', line 85

def file_type(attachment, attachment_type)
  attachment.respond_to?("#{attachment_type}_content_type") ? attachment.send("#{attachment_type}_content_type").split('/').first : "not_found"
end

#generate_image(phr, a, attachment_type) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/compile_pdf/generate_pdf.rb', line 56

def generate_image(phr,a, attachment_type)
  phr.go_to_page(phr.page_count)
  phr.start_new_page
  file_path = attachment_type(a, attachment_type).to_s
  phr.move_down 5
  phr.image open(file_path, 'User-Agent' => 'ruby'), :position => :center, :fit => [600, 700]
end

#generate_pdf(phr, a, attachment_type) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/compile_pdf/generate_pdf.rb', line 64

def generate_pdf(phr,a, attachment_type)
  file_path = attachment_type(a, attachment_type).to_s
  file_path = open(file_path, 'User-Agent' => 'ruby')
  phr.go_to_page(phr.page_count)
  template_page_count = count_pdf_pages(file_path)
  (1..template_page_count).each do |template_page_number|
    phr.start_new_page(:template => file_path, :template_page => template_page_number,:size => "LEGAL", :layout => :landscape, :margin => 0)
  end
end

#generate_text(phr, a, attachment_type) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/compile_pdf/generate_pdf.rb', line 74

def generate_text(phr, a, attachment_type)
  phr.go_to_page(phr.page_count)
  phr.start_new_page
  path = attachment_type(a, attachment_type).to_s
  open_file = open(path, 'User-Agent' => 'ruby')
  data = open_file.read
  content = Yomu.read :text, data
  phr.move_down 5
  phr.text content
end

#is_pdf?(attachment, attachment_type) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/compile_pdf/generate_pdf.rb', line 89

def is_pdf?(attachment, attachment_type)
  attachment.respond_to?("#{attachment_type}_content_type") ? attachment.send("#{attachment_type}_content_type").split('/').last == "pdf" : "not_found"
end

#merge(pdf_paths, destination) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/compile_pdf/generate_pdf.rb', line 38

def merge(pdf_paths, destination)
  first_pdf_path = pdf_paths.delete_at(0)
  Prawn::Document.generate(destination, :template => first_pdf_path) do |pdf|
    pdf_paths.each do |pdf_path|
      pdf.go_to_page(pdf.page_count)
      template_page_count = count_pdf_pages(pdf_path)
      (1..template_page_count).each do |template_page_number|
        pdf.start_new_page(:template => pdf_path, :template_page => template_page_number)
      end
    end
  end
end