Intro
A ruby implementation of the Line Thermal Printer ESC/POS Command Specifications.
Install
gem install rescpos
Usage
See examples
Specify the template path:
Rescpos.configure do |config|
config.template_path = "path/to/your/escpos/templates"
end
For rails application, the default template path is set to app/escposes. So you should create the templates under app/escposes directory. But if you want to save the templates into another place, you should config the template path in a rails initializer.
Reports
Reports manage the objects that will be used in the templates. You should subclass the Rescpos::Report class to define your own reports. For example:
class SimpleTitledReport < Rescpos::Report
attr_reader :title
def initialize(title = '')
@title = title
end
end
To render the report, you just need to call the render method of the report.
report = SimpleTitledReport.new('A report with a title')
report.render(:template => "<%= title %>")
# A report with a title
You can also create a pre-defined template for the report.
report.render(:file => 'path/to/the/template')
Rescpos will search for the template with the underscore name of the report under the template path if you do not provide any params when rendering the report.
report.render
This will cause rescpos to search for a template named simple_titled.escpos.erb under app/escposes.
Templates
Rescpos use erb to define the templates. So any valid erb templates are valid rescpos templates.
Template’s extension name is set to ‘escpos.erb’.
# dish_item.escpos.erb
<%= text(@dish[:dish_category], :font_size => FONT_BIG) %>
<%= text(key_value("餐桌", @bill[:table]), :font_size => FONT_BIG) %>
<%= text(key_value("人数", @bill[:num_of_people]), :font_size => FONT_BIG) %>
<%= single_splitline %>
<%= text(key_value("菜品", @dish[:name]), :font_size => FONT_BIG) %>
<%= text(key_value("数量", "#{@dish[:quantity]} [根]"), :font_size => FONT_BIG) %>
<%= single_splitline %>
<%= text(key_value("单号", @bill[:id])) %>
<%= text(key_value("操作员", @bill[:waiter])) %>
<%= text(key_value("下单时间", @bill[:created_at])) %>
Printers
In order to print the report, you should create a Rescpos::Printer object and then print the report.
printer = Rescpos::Printer.open('192.168.1.250', 9100)
printer.print(report)
printer.close
You can set the options for printing:
printer.print(report, encoding: 'GBK', cut_mode: :partial_cut)
- Encoding option could be any valid encoding.
- cut_mode option has two values: :full_cut and :partial_cut, by default the mode is set to :partial_cut.