Class: Rescpos::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/rescpos/printer.rb

Constant Summary collapse

SOCK_FLAG =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, port) ⇒ Printer

Returns a new instance of Printer.



6
7
8
9
# File 'lib/rescpos/printer.rb', line 6

def initialize(ip, port)
  @socket = TCPSocket.open(ip, port)
  @socket.send("\x1b\x40", SOCK_FLAG)
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



3
4
5
# File 'lib/rescpos/printer.rb', line 3

def socket
  @socket
end

Class Method Details

.open(ip, port) ⇒ Object



11
12
13
# File 'lib/rescpos/printer.rb', line 11

def self.open(ip, port)
  printer = new(ip, port)
end

Instance Method Details

#closeObject



15
16
17
# File 'lib/rescpos/printer.rb', line 15

def close
  @socket.close
end


19
20
21
22
23
# File 'lib/rescpos/printer.rb', line 19

def print(content, opts = { :encoding => 'GBK', :cut_mode => :partial_cut })
  content = Iconv.iconv("#{ opts[:encoding] || 'GBK' }//IGNORE","UTF-8//IGNORE", content)[0]
  @socket.send(content, SOCK_FLAG)
  send(opts[:cut_mode] || :partial_cut)
end


25
26
27
# File 'lib/rescpos/printer.rb', line 25

def print_report(report, opts={})
  print(report.render, opts)
end