Class: PrintMap::PdfGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/print_map/pdf_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_legend(pdf, legend_images, opts = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/print_map/pdf_generator.rb', line 91

def create_legend(pdf, legend_images, opts = {})
  columns = opts[:columns] || 4
  rows    = opts[:rows]    || 4
  gutter  = opts[:gutter]  || 0

  pdf.define_grid(:columns => columns, :rows => rows, :gutter => gutter)

  pdf.grid.rows.times do |i|
    pdf.grid.columns.times do |j|
      cell = pdf.grid(i,j)
      pdf.bounding_box cell.top_left, :width => cell.width, :height => cell.height do
        return if legend_images.empty?
        image = legend_images.shift

        pdf.image StringIO.new(image[:data]), :at => [pdf.bounds.top_left], :fit => [16, 16]
        pdf.text_box image[:label],
          :width    => cell.width,
          :height   => cell.height,
          :overflow => :ellipses,
          :at       => [pdf.bounds.left + 20, pdf.bounds.top - 2]
      end
    end
  end
end

#generate(opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/print_map/pdf_generator.rb', line 8

def generate(opts = {})
  title  = opts[:title]
  map_image  = opts[:map_image]
  layout = opts[:page_layout]
  legend_images = opts[:legend_images]

  pdf = Prawn::Document.new(:page_layout => layout) do |pdf|
    # html tag definitions
    pdf.tags :h1 => { :font_size => '25', :font_style => :bold }
    # html style definitions
    pdf.styles :footer => { :color => "#999", :font_style => :italic }
    # generate the pdf
    send("generate_#{layout}_pdf", pdf, title, map_image, legend_images)
    write_footer(pdf)
  end

  pdf
end

#generate_landscape_pdf(pdf, title, image, legend_images) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/print_map/pdf_generator.rb', line 50

def generate_landscape_pdf(pdf, title, image, legend_images)
  box = pdf.bounds
  # map
  pdf.bounding_box [box.left, box.top], :width => box.height, :height => box.height do
    pdf.stroke_bounds
    pdf.image StringIO.new(image.to_blob), :at => [box.left, box.top], :width => box.height, :height => box.height
  end


  pdf.bounding_box [box.height + 10, box.top], :width => box.right - box.height - 10, :height => box.height do
    # header
    pdf.text "<h1>#{title}</h1>"
    pdf.move_down 10

    # toc
    pdf.bounding_box [pdf.bounds.left, pdf.cursor], :width => pdf.bounds.right, :height => pdf.cursor do
      create_legend(pdf, legend_images, :columns => 1, :rows => 20)
      pdf.stroke_bounds
    end
  end
end

#generate_portrait_pdf(pdf, title, map_image, legend_images) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/print_map/pdf_generator.rb', line 27

def generate_portrait_pdf(pdf, title, map_image, legend_images)
  box = pdf.bounds
  # header
  pdf.bounding_box [box.left, box.top], :width => box.right, :height => 30 do
    pdf.text "<h1>#{title}</h1>"
  end

  # map
  pdf.bounding_box [box.left, pdf.cursor], :width => box.right, :height => box.width do
    pdf.stroke_bounds
    pdf.image StringIO.new(map_image.to_blob), :at => [box.left, pdf.cursor], :width => box.width, :height => box.width
  end

  pdf.move_down 10
  # toc
  pdf.bounding_box [box.left, pdf.cursor], :width => box.right, :height => 140 do
    pdf.padded_box 5 do
      create_legend(pdf, legend_images, :columns => 4, :rows => 5)
      pdf.stroke_bounds
    end
  end
end


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/print_map/pdf_generator.rb', line 72

def write_footer(pdf)
  box = pdf.bounds
  pdf.canvas do
    footer = {
      :top_left => [box.absolute_left - 20, box.absolute_bottom - 15],
      :width    => box.right + 40,
      :height   => 15
    }

    pdf.bounding_box footer[:top_left], :width => footer[:width], :height => footer[:height] do
      pdf.text '<span class="footer">NB Aquatic Bio-Web</span>'
    end

    pdf.bounding_box footer[:top_left], :width => footer[:width], :height => footer[:height] do
      pdf.text "<span class=\"footer\">Generated on #{Time.now.strftime("%Y/%m/%d")}</span>", :align => :right
    end
  end
end