Class: MapnikLegendary::Docwriter
- Inherits:
-
Object
- Object
- MapnikLegendary::Docwriter
- Defined in:
- lib/mapnik_legendary/docwriter.rb
Instance Attribute Summary collapse
-
#image_width ⇒ Object
Returns the value of attribute image_width.
Instance Method Summary collapse
- #add(image, description) ⇒ Object
-
#initialize ⇒ Docwriter
constructor
A new instance of Docwriter.
- #to_html ⇒ Object
- #to_pdf(filename, title) ⇒ Object
Constructor Details
#initialize ⇒ Docwriter
Returns a new instance of Docwriter.
9 10 11 12 |
# File 'lib/mapnik_legendary/docwriter.rb', line 9 def initialize @entries = [] @image_width = 100 end |
Instance Attribute Details
#image_width ⇒ Object
Returns the value of attribute image_width.
7 8 9 |
# File 'lib/mapnik_legendary/docwriter.rb', line 7 def image_width @image_width end |
Instance Method Details
#add(image, description) ⇒ Object
14 15 16 |
# File 'lib/mapnik_legendary/docwriter.rb', line 14 def add(image, description) @entries << [image, description] end |
#to_html ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/mapnik_legendary/docwriter.rb', line 18 def to_html doc = '<html><head></head><body><table>' + "\n" @entries.each do |entry| doc += "<tr><td><img src=\"#{entry[0]}\"></td><td>#{entry[1]}</td></tr>\n" end doc += '</table></body></html>' + "\n" doc end |
#to_pdf(filename, title) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mapnik_legendary/docwriter.rb', line 27 def to_pdf(filename, title) entries = @entries image_width = @image_width Prawn::Document.generate(filename, page_size: 'A4') do font_families.update( 'Ubuntu' => { bold: '/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf', italic: '/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-RI.ttf', normal: '/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-R.ttf' } ) font 'Ubuntu' font_size 12 text title, style: :bold, size: 24, align: :center move_down(40) data = Array.new image_scale = 0.5 entries.each do |entry| data << { image: File.join(File.dirname(filename), entry[0]), scale: image_scale, position: :center, vposition: :center } data << entry[1] end columns = image_width >= 200 ? 2 : 3 table(data.each_slice(columns * 2).to_a, cell_style: { border_width: 0.1 }) do (0..columns - 1).each do |n| column(n * 2).width = image_width * image_scale end end end end |