class POS::Printer

Public Class Methods

new(name, lp_options = []) click to toggle source
# File lib/pos/printer.rb, line 11
def initialize(name, lp_options = [])
  @name = name
  @commands = "\e@".b # This is ESC/POS initialize command.
  @lp_options = ['-d', name, '-o', 'raw', *lp_options]
end
print(printer_name, lp_options: []) { |printer| ... } click to toggle source

Public Instance Methods

align_center() click to toggle source

ESC/POS commands FMI: www.bixolon.com/upload/download/unified%20command%20manual_rev_1_01.pdf

# File lib/pos/printer.rb, line 24
def align_center
  add_command "\ea\1"
end
align_left() click to toggle source
# File lib/pos/printer.rb, line 28
def align_left
  add_command "\ea\0"
end
align_right() click to toggle source
# File lib/pos/printer.rb, line 32
def align_right
  add_command "\ea\2"
end
big_font() click to toggle source
# File lib/pos/printer.rb, line 36
def big_font
  add_command "\eM\0"
end
cut() click to toggle source
# File lib/pos/printer.rb, line 40
def cut
  add_command "\em"
end
double_size() click to toggle source
# File lib/pos/printer.rb, line 44
def double_size
  add_command "\x1d!\x11"
end
line_feed(n = 1) click to toggle source
# File lib/pos/printer.rb, line 48
def line_feed(n = 1)
  add_command "\n" * n
end
normal_size() click to toggle source
# File lib/pos/printer.rb, line 52
def normal_size
  add_command "\x1d!\0"
end
open_drawer() click to toggle source
# File lib/pos/printer.rb, line 56
def open_drawer
  add_command "\x1bp\x00\x19\xff\x1bp\x01\x19\xff"
end
print() click to toggle source
qr_code(str) click to toggle source
# File lib/pos/printer.rb, line 64
def qr_code(str)
  qr_size = 4
  s = str.size + 3
  lsb = s % 256
  msb = s / 256

  add_command "\x1B\x61\x01"
  add_command "\x1D\x28\x6B\x03\x00\x31\x43#{qr_size.chr}"
  add_command "\x1D\x28\x6B\x03\x00\x31\x45\x33"
  add_command "\x1D\x28\x6B#{lsb.chr}"
  add_command "#{msb.chr}\x31\x50\x30"
  add_command str
  add_command "\x1D\x28\x6B\x03\x00\x31\x51\x30"
end
select_character_code_table(chr_table) click to toggle source
# File lib/pos/printer.rb, line 79
def select_character_code_table(chr_table)
  add_command "\et#{get_chr_table_code(chr_table)}"
end
small_font() click to toggle source
# File lib/pos/printer.rb, line 83
def small_font
  add_command "\eM\1"
end
text(str) click to toggle source
# File lib/pos/printer.rb, line 87
def text(str)
  add_command str
end
text_line(str) click to toggle source
# File lib/pos/printer.rb, line 91
def text_line(str)
  add_command str
  line_feed
end

Private Instance Methods

add_command(command) click to toggle source
# File lib/pos/printer.rb, line 98
def add_command(command)
  @commands << replace_spanish_characters(command)
  return
end
get_chr_table_code(chr_table_sym) click to toggle source
# File lib/pos/printer.rb, line 103
def get_chr_table_code(chr_table_sym)
  case chr_table_sym
  when :usa
    0
  when :katakana
    1 
  when :multilingual
    2
  when :portuguese
    3
  when :canadian_french
    4
  when :nordic
    5
  when :latin_1
    16
  when :cyrillic_2
    17
  when :latin_2
    18
  when :euro
    19
  when :hebrew_dos_code
    21
  when :arabic
    22
  when :thai_42
    23
  when :greek
    24
  when :turkish
    25
  when :baltic
    26
  when :farsi
    27
  when :cyrillic
    28
  when :greek_2
    29
  when :baltic_2
    30
  when :thai_14
    31
  when :hebrew_new_code
    33
  when :thai_11
    34
  when :thai_18
    35
  when :cyrillic_3
    36
  when :turkish_2
    37
  when :greek_3
    38
  when :thai_16
    39
  when :arabic_2
    40
  when :vietnam
    41
  when :space
    255
  else
    0
  end.chr
end
replace_spanish_characters(str) click to toggle source
# File lib/pos/printer.rb, line 172
def replace_spanish_characters(str)
  # Codes from: http://www.bixolon.com/upload/download/srp-f310312_code%20pages_english_rev_1_00.pdf
  str
    .b
    .gsub("Á".b, "\xB5".b)
    .gsub("É".b, "\x90".b)
    .gsub("Í".b, "\xD6".b)
    .gsub("Ó".b, "\xE0".b)
    .gsub("Ú".b, "\xE9".b)
    .gsub("Ñ".b, "\xA5".b)
    .gsub("á".b, "\xA0".b)
    .gsub("é".b, "\x82".b)
    .gsub("í".b, "\xA1".b)
    .gsub("ó".b, "\xA2".b)
    .gsub("ú".b, "\xA3".b)
    .gsub("ñ".b, "\xA4".b)
end