Class: RenderPdf

Inherits:
Prawn::Document
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, ApplicationHelper
Defined in:
app/lib/render_pdf.rb

Direct Known Subclasses

OrderPdf

Constant Summary collapse

TOP_MARGIN =
36
BOTTOM_MARGIN =
23
HEADER_SPACE =
9
3
HEADER_FONT_SIZE =
16
8
DEFAULT_FONT =
'OpenSans'

Instance Method Summary collapse

Methods included from ApplicationHelper

#base_errors, #bootstrap_flash_patched, #close_button, #expand, #foodcoop_css_path, #foodcoop_css_tag, #format_currency, #format_date, #format_datetime, #format_datetime_timespec, #format_iban, #format_roles, #format_time, #heading_helper, #icon, #items_per_page, #link_to_gmaps, #link_to_top, #pagination_links_remote, #remote_link_to, #show_title?, #show_user, #show_user_link, #sort_link_helper, #tab_is_active?, #truncate, #weekday

Methods included from PathHelper

#finance_group_transactions_path

Constructor Details

#initialize(options = {}) ⇒ RenderPdf

Returns a new instance of RenderPdf.



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
# File 'app/lib/render_pdf.rb', line 67

def initialize(options = {})
  options[:font_size] ||= FoodsoftConfig[:pdf_font_size].try(:to_f) || 12
  options[:page_size] ||= FoodsoftConfig[:pdf_page_size] || 'A4'
  options[:skip_page_creation] = true
  @options = options
  @first_page = true

  super(options)

  # Use ttf for better utf-8 compability
  font_families.update(
    'OpenSans' => {
      bold: font_path('OpenSans-Bold.ttf'),
      italic: font_path('OpenSans-Italic.ttf'),
      bold_italic: font_path('OpenSans-BoldItalic.ttf'),
      normal: font_path('OpenSans-Regular.ttf')
    }
  )

  header = options[:title] || title
  footer = I18n.l(Time.now, format: :long)

  header_size = 0
  header_size = height_of(header, size: HEADER_FONT_SIZE, font: DEFAULT_FONT) + HEADER_SPACE if header
  footer_size = height_of(footer, size: FOOTER_FONT_SIZE, font: DEFAULT_FONT) + FOOTER_SPACE

  start_new_page(top_margin: TOP_MARGIN + header_size, bottom_margin: BOTTOM_MARGIN + footer_size)

  font DEFAULT_FONT

  repeat :all, dynamic: true do
    bounding_box [bounds.left, bounds.top + header_size], width: bounds.width, height: header_size do
      text header, size: HEADER_FONT_SIZE, align: :center, overflow: :shrink_to_fit if header
    end
    font_size FOOTER_FONT_SIZE do
      bounding_box [bounds.left, bounds.bottom - FOOTER_SPACE], width: bounds.width, height: footer_size do
        text footer, align: :left, valign: :bottom
      end
      bounding_box [bounds.left, bounds.bottom - FOOTER_SPACE], width: bounds.width, height: footer_size do
        text I18n.t('lib.render_pdf.page', number: page_number, count: page_count), align: :right, valign: :bottom
      end
    end
  end
end

Instance Method Details

#down_or_page(space = 10) ⇒ Object

add pagebreak or vertical whitespace, depending on configuration



132
133
134
135
136
137
138
139
140
141
142
# File 'app/lib/render_pdf.rb', line 132

def down_or_page(space = 10)
  if @first_page
    @first_page = false
    return
  end
  if pdf_add_page_breaks?
    start_new_page
  else
    move_down space
  end
end

#font_path(name) ⇒ Object (protected)



164
165
166
# File 'app/lib/render_pdf.rb', line 164

def font_path(name)
  Rails.root.join('vendor', 'assets', 'fonts', name)
end

#font_size(points = nil, &block) ⇒ Object



126
127
128
129
# File 'app/lib/render_pdf.rb', line 126

def font_size(points = nil, &block)
  points *= @options[:font_size] / 12 if points
  super(points, &block)
end

#fontsize(size) ⇒ Object (protected)



146
147
148
# File 'app/lib/render_pdf.rb', line 146

def fontsize(size)
  size
end

#number_to_currency(number, options = {}) ⇒ Object

TODO:

avoid underscore instead of unicode whitespace in pdf :/



122
123
124
# File 'app/lib/render_pdf.rb', line 122

def number_to_currency(number, options = {})
  super(number, options).gsub("\u202f", ' ') if number
end

#pdf_add_page_breaks?(docid = nil) ⇒ Boolean (protected)

return whether pagebreak or vertical whitespace is used for breaks

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/lib/render_pdf.rb', line 151

def pdf_add_page_breaks?(docid = nil)
  docid ||= self.class.name.underscore
  cfg = FoodsoftConfig[:pdf_add_page_breaks]
  case cfg
  when Array
    cfg.index(docid.to_s).any?
  when Hash
    cfg[docid.to_s]
  else
    cfg
  end
end

#titleObject



112
113
114
# File 'app/lib/render_pdf.rb', line 112

def title
  nil
end

#to_pdfObject



116
117
118
119
# File 'app/lib/render_pdf.rb', line 116

def to_pdf
  body # Add content, which is defined in subclasses
  render # Render pdf
end