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
- #attachment_type(file, attachment_type) ⇒ Object
- #compile_pdf_module(attachments, attachment_type, file_name = nil) ⇒ Object
- #count_pdf_pages(pdf_file_path) ⇒ Object
- #file_name_generate(file_name) ⇒ Object
- #file_type(attachment, attachment_type) ⇒ Object
- #generate_image(phr, a, attachment_type) ⇒ Object
- #generate_pdf(phr, a, attachment_type) ⇒ Object
- #generate_text(phr, a, attachment_type) ⇒ Object
- #is_pdf?(attachment, attachment_type) ⇒ Boolean
- #merge(pdf_paths, destination) ⇒ Object
Instance Method Details
#attachment_type(file, attachment_type) ⇒ Object
26 27 28 |
# File 'lib/compile_pdf/generate_pdf.rb', line 26 def (file, ) file.send() 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(, , file_name = nil) arr = [] .each do |a| if file_type(a, ) == "application" && is_pdf?(a, ) arr << (a, ).to_s end end file_name = file_name_generate(file_name) Prawn::Document.generate(file_name) do |phr| .each do |a| if file_type(a, ) == "application" && is_pdf?(a, ) generate_pdf(phr,a, ) elsif a.file_type == "image" generate_image(phr,a, ) elsif file_type(a, ) != "not_found" && is_pdf?(a, ) != "not_found" generate_text(phr,a, ) 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(, ) .respond_to?("#{}_content_type") ? .send("#{}_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, ) phr.go_to_page(phr.page_count) phr.start_new_page file_path = (a, ).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, ) file_path = (a, ).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, ) phr.go_to_page(phr.page_count) phr.start_new_page path = (a, ).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
89 90 91 |
# File 'lib/compile_pdf/generate_pdf.rb', line 89 def is_pdf?(, ) .respond_to?("#{}_content_type") ? .send("#{}_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 |