Class: RenderPDF

Inherits:
Prawn::Document
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, ApplicationHelper
Defined in:
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_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

Constructor Details

#initialize(options = {}) ⇒ RenderPDF

Returns a new instance of RenderPDF.


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

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


139
140
141
142
143
144
145
146
147
148
149
# File 'lib/render_pdf.rb', line 139

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)


170
171
172
# File 'lib/render_pdf.rb', line 170

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

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


133
134
135
136
# File 'lib/render_pdf.rb', line 133

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

#fontsize(n) ⇒ Object (protected)


153
154
155
# File 'lib/render_pdf.rb', line 153

def fontsize(n)
  n
end

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

TODO:

avoid underscore instead of unicode whitespace in pdf :/


129
130
131
# File 'lib/render_pdf.rb', line 129

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)

158
159
160
161
162
163
164
165
166
167
168
# File 'lib/render_pdf.rb', line 158

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

#save_tmpObject

Helper method to test pdf via rails console: OrderByGroups.new(order).save_tmp


124
125
126
# File 'lib/render_pdf.rb', line 124

def save_tmp
  File.open("#{Rails.root}/tmp/#{self.class.to_s.underscore}.pdf", 'w') {|f| f.write(to_pdf.force_encoding("UTF-8")) }
end

#titleObject


114
115
116
# File 'lib/render_pdf.rb', line 114

def title
  nil
end

#to_pdfObject


118
119
120
121
# File 'lib/render_pdf.rb', line 118

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