Class: AlegoPdf::Merging
- Inherits:
-
Object
- Object
- AlegoPdf::Merging
- Defined in:
- lib/alego_pdf/merging.rb
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#orderize ⇒ Object
readonly
Returns the value of attribute orderize.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#urls ⇒ Object
readonly
Returns the value of attribute urls.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(ids, model, attribute, filename = 'tmp/merge_files.pdf', urls = [], orderize = 'url') ⇒ Merging
constructor
A new instance of Merging.
- #join_array ⇒ Object
- #join_urls ⇒ Object
Constructor Details
#initialize(ids, model, attribute, filename = 'tmp/merge_files.pdf', urls = [], orderize = 'url') ⇒ Merging
Returns a new instance of Merging.
8 9 10 11 12 13 14 15 16 |
# File 'lib/alego_pdf/merging.rb', line 8 def initialize(ids, model, attribute, filename = 'tmp/merge_files.pdf', urls = [], orderize = 'url') @ids = ids @model = model @attribute = attribute @filename = filename @urls = urls @orderize = orderize @target = HexaPDF::Document.new end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
6 7 8 |
# File 'lib/alego_pdf/merging.rb', line 6 def attribute @attribute end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
6 7 8 |
# File 'lib/alego_pdf/merging.rb', line 6 def filename @filename end |
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
6 7 8 |
# File 'lib/alego_pdf/merging.rb', line 6 def ids @ids end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
6 7 8 |
# File 'lib/alego_pdf/merging.rb', line 6 def model @model end |
#orderize ⇒ Object (readonly)
Returns the value of attribute orderize.
6 7 8 |
# File 'lib/alego_pdf/merging.rb', line 6 def orderize @orderize end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
6 7 8 |
# File 'lib/alego_pdf/merging.rb', line 6 def target @target end |
#urls ⇒ Object (readonly)
Returns the value of attribute urls.
6 7 8 |
# File 'lib/alego_pdf/merging.rb', line 6 def urls @urls end |
Instance Method Details
#call ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/alego_pdf/merging.rb', line 18 def call if orderize == 'url' join_urls join_array else join_array join_urls end target.write(filename, optimize: true) end |
#join_array ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/alego_pdf/merging.rb', line 44 def join_array array = model.is_a?(String) ? model.camelize.constantize.where(id: ids) : model.where(id: ids) array.each do |object| file = object[attribute] filepath = file.try(:url) || object.try(:arquivo_url) next if filepath.blank? uri = URI.parse(filepath).open(&:read) tempfile = Tempfile.new tempfile.write(uri.force_encoding('UTF-8')) tempfile.close pdf = HexaPDF::Document.open(tempfile) pdf.pages.each { |page| target.pages << target.import(page) } end end |
#join_urls ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/alego_pdf/merging.rb', line 30 def join_urls urls.each do |filepath| next if filepath.blank? uri = URI.parse(filepath).open(&:read) tempfile = Tempfile.new tempfile.write(uri.force_encoding('UTF-8')) tempfile.close pdf = HexaPDF::Document.open(tempfile) pdf.pages.each { |page| target.pages << target.import(page) } end end |