2012-10-02 02:50:48 +02:00
|
|
|
require "prawn/measurement_extensions"
|
|
|
|
|
|
|
|
class OrderPdf < Prawn::Document
|
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
|
|
|
def initialize(order, options = {})
|
2014-03-19 14:01:51 +01:00
|
|
|
options[:page_size] ||= FoodsoftConfig[:pdf_page_size] || "A4"
|
2012-10-02 02:50:48 +02:00
|
|
|
#options[:left_margin] ||= 40
|
|
|
|
#options[:right_margin] ||= 40
|
|
|
|
options[:top_margin] ||= 50
|
|
|
|
#options[:bottom_margin] ||= 40
|
|
|
|
super(options)
|
|
|
|
@order = order
|
2014-03-15 15:09:16 +01:00
|
|
|
@options = options
|
2015-01-03 00:14:38 +01:00
|
|
|
@first_page = true
|
2012-10-02 02:50:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_pdf
|
2015-06-07 18:46:22 +02:00
|
|
|
# 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")
|
|
|
|
}
|
|
|
|
)
|
|
|
|
font "OpenSans"
|
|
|
|
|
2014-03-19 14:01:51 +01:00
|
|
|
font_size fontsize(12)
|
|
|
|
|
2012-10-02 02:50:48 +02:00
|
|
|
# Define header
|
|
|
|
repeat :all, dynamic: true do
|
2015-01-03 00:06:44 +01:00
|
|
|
s = fontsize(8)
|
|
|
|
# header
|
|
|
|
bounding_box [bounds.left, bounds.top+s*2], width: bounds.width, height: s*1.2 do
|
|
|
|
text title, size: s, align: :center if title
|
|
|
|
end
|
|
|
|
# footer
|
|
|
|
bounding_box [bounds.left, bounds.bottom-s], width: bounds.width, height: s*1.2 do
|
|
|
|
text I18n.t('lib.order_pdf.page', number: page_number, count: page_count), size: s, align: :right
|
|
|
|
end
|
|
|
|
bounding_box [bounds.left, bounds.bottom-s], width: bounds.width, height: s*1.2 do
|
|
|
|
text I18n.l(Time.now, format: :long), size: s, align: :left
|
|
|
|
end
|
2012-10-02 02:50:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
body # Add content, which is defined in subclasses
|
|
|
|
|
|
|
|
render # Render pdf
|
|
|
|
end
|
|
|
|
|
|
|
|
# Helper method to test pdf via rails console: OrderByGroups.new(order).save_tmp
|
|
|
|
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
|
2014-03-15 15:09:16 +01:00
|
|
|
|
|
|
|
# XXX avoid underscore instead of unicode whitespace in pdf :/
|
|
|
|
def number_to_currency(number, options={})
|
2014-06-06 12:45:21 +02:00
|
|
|
super(number, options).gsub("\u202f", ' ') if number
|
2014-03-15 15:09:16 +01:00
|
|
|
end
|
2014-03-19 14:01:51 +01:00
|
|
|
|
|
|
|
# return fontsize after scaling it with any configured factor
|
|
|
|
# please use this wherever you're setting a fontsize
|
|
|
|
def fontsize(n)
|
|
|
|
if FoodsoftConfig[:pdf_font_size]
|
|
|
|
n * FoodsoftConfig[:pdf_font_size].to_f/12
|
|
|
|
else
|
|
|
|
n
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# add pagebreak or vertical whitespace, depending on configuration
|
|
|
|
def down_or_page(space=10)
|
2015-01-03 00:14:38 +01:00
|
|
|
if @first_page
|
|
|
|
@first_page = false
|
|
|
|
return
|
|
|
|
end
|
2014-08-28 08:10:57 +02:00
|
|
|
if pdf_add_page_breaks?
|
2014-03-19 14:01:51 +01:00
|
|
|
start_new_page
|
|
|
|
else
|
|
|
|
move_down space
|
|
|
|
end
|
|
|
|
end
|
2014-08-28 08:10:57 +02:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# return whether pagebreak or vertical whitespace is used for breaks
|
|
|
|
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
|
2015-06-07 18:46:22 +02:00
|
|
|
|
|
|
|
def font_path(name)
|
|
|
|
File.join(Rails.root, "vendor", "assets", "fonts", name)
|
|
|
|
end
|
2013-05-20 17:14:42 +02:00
|
|
|
end
|