Class: Labels::PDF::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/labels/pdf/builder.rb

Overview

This class builds a PDF document from the given Labels::Document

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document = nil) ⇒ Builder

Creates a new Labels::PDF::Builder instance



29
30
31
# File 'lib/labels/pdf/builder.rb', line 29

def initialize(document=nil)
  @document = document
end

Instance Attribute Details

#documentObject

The document instance



26
27
28
# File 'lib/labels/pdf/builder.rb', line 26

def document
  @document
end

Instance Method Details

#pdfObject

Returns the rendered PDF document



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/labels/pdf/builder.rb', line 43

def pdf
  unless @document.nil?
    paper = @document.paper
    template = @document.template
    database = @document.database
  
    if paper && template
      document_options = {
        :page_size => [paper.width, paper.height],
        :left_margin => template.left_margin,
        :right_margin => template.right_margin,
        :top_margin => template.top_margin,
        :bottom_margin => template.bottom_margin
      }
      
      page_cell_count = template.columns * template.rows
      page_number_count = (database.data.length / page_cell_count).ceil
      cell = 0
      
      if page_number_count == 0
        page_number_count += 1
      end
      
      Prawn::Document.new document_options do |pdf|
        page_number_count.times do |page|
          if page > 0
            pdf.start_new_page
          end
          
          pdf.define_grid({
            :columns => template.columns,
            :rows => template.rows,
            :column_gutter => template.vertical_gutter,
            :row_gutter => template.horizontal_gutter
          })
          
          # Draw template
          pdf.grid.rows.times do |i|
            pdf.grid.columns.times do |j|
              box = pdf.grid(i,j)
              pdf.bounding_box box.top_left, :width => box.width, :height => box.height do
                _draw_template(pdf, template)
              end
            end
          end
          
          # Draw layers
          pdf.grid.rows.times do |i|
            pdf.grid.columns.times do |j|
              box = pdf.grid(i,j)
              pdf.bounding_box box.top_left, :width => box.width, :height => box.height do
                layers = @document.layers.sort {|x,y| x.z <=> y.z}
                layers.each do |layer|
                  
                  draw = false
                  content = nil
                  if database.data.length > 0
                    if cell < database.data.length
                      if database.data[cell].has_key?(layer.column)
                        content = database.data[cell][layer.column]
                      end
                      draw = true
                    end
                  else
                    draw = true
                  end
                  
                  if draw
                    case layer
                    when Labels::Barcode
                      _draw_barcode(pdf, layer, content)
                    when Labels::Date
                      _draw_date(pdf, layer, content)
                    when Labels::Image
                      _draw_image(pdf, layer, content)
                    when Labels::Shape
                      _draw_shape(pdf, layer, content)
                    when Labels::Text
                      _draw_text(pdf, layer, content)
                    end
                  end
                end
              end
              cell += 1
            end
          end
        end
      end
    end
  end
end

#render(filename = nil) ⇒ Object

Renders the PDF and optionally saves to given filename



34
35
36
37
38
39
40
# File 'lib/labels/pdf/builder.rb', line 34

def render(filename=nil)
  unless filename.nil?
    self.pdf.render_file filename
  else
    self.pdf.render
  end
end