2012-10-02 02:50:48 +02:00
|
|
|
require "prawn/measurement_extensions"
|
|
|
|
|
|
|
|
class OrderPdf < Prawn::Document
|
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
|
|
|
def initialize(order, options = {})
|
|
|
|
options[:page_size] ||= "A4"
|
|
|
|
#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
|
2012-10-02 02:50:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_pdf
|
|
|
|
# Define header
|
|
|
|
repeat :all, dynamic: true do
|
|
|
|
draw_text title, size: 10, style: :bold, at: [bounds.left, bounds.top+20] if title # Header
|
2013-05-20 17:14:42 +02:00
|
|
|
draw_text I18n.t('lib.order_pdf.page', :number => page_number), size: 8, at: [bounds.left, bounds.bottom-10] # Footer
|
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={})
|
|
|
|
super(number, options).gsub("\u202f", ' ')
|
|
|
|
end
|
2013-05-20 17:14:42 +02:00
|
|
|
end
|