Class: InvoicePrinter::U420

Inherits:
Object
  • Object
show all
Defined in:
lib/invoice_printer/U420.rb

Overview

This class provides the interface to communicate with Epson U420-series invoice printer. The output is defaulted to STDOUT, which is your display. To actually print out the content, please use SerialPort gem to get an IO object of the Serial Port, then assigned it to the output parameter when initiailizing this class.

Example

require 'rubygems'
require 'invoice_printer'
require 'serialport'

serial_out = SerialPort.new("/dev/tty.usbserial",
  :baud_rate => 9600,
  :databits => 8,
  :parity => SerialPort::NONE,
  :stopbits => 1
)

prt = InvoicePrinter::U420.new(
  :header => "Hello, World",
  :lines_total => 35,
  :lines_available => 18,
  :lines_stamp => 5,
  :output => serial_out
)

prt.println "Hello, World"
prt.feed 2
prt.println "Yeah"
prt.cut # This is actually cut the paper.

Constant Summary collapse

DEFAULT_OPTIONS =
{
	:header => "",
	:lines_total => 35,
	:lines_available => 18,
 :lines_stamp => 5,
	:output => STDOUT,
   :encoding => "big5//ignore"
}
ESC =
0x1b.chr

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ U420

Initialize a new instance of U420 printer API interface. The options contains some parameters you can use to customize the behavior of the printer. The default value should be very useful if you’re going to print invoices in Taiwan.

Supported options:

* header - A string contains the header of each invoice, can contains multiple line. (Default: "")
* lines_total - How many lines a page of invoice should contains. (Default: 35)
* lines_available - How many lines should a page of invoice can print on. (Default: 18)
* lines_stamp - The height of the stamp (before the header) of the invoice. (Default: 5)
* output - An IO object which will receive the command of the printer. (Default: STDOUT)
* encoding - The encoding of non-ascii characters the printer supported. (Default: Big5)
* dont_init_printer - Don't send [ESC]@ to printer before the data. (Default: false)


104
105
106
107
108
109
110
# File 'lib/invoice_printer/U420.rb', line 104

def initialize(options)
 @options = DEFAULT_OPTIONS.merge(options)
	@output = @options[:output]
	@lines_used = 0

 self.init_printer unless options[:dont_init_printer]
end

Instance Attribute Details

#optionsObject

Options which controls the behavior of this library. Although we’re using option to give output stream when initializing, please notice that changing options by accessor is not working. To do so, use output accessor instead.



77
78
79
# File 'lib/invoice_printer/U420.rb', line 77

def options
  @options
end

#outputObject

Current output stream, which receives the command generated by this library.



72
73
74
# File 'lib/invoice_printer/U420.rb', line 72

def output
  @output
end

Instance Method Details

#cutObject

Roll the entire page out and do the cut, it then stamp on the next page, too.



129
130
131
132
133
134
135
136
# File 'lib/invoice_printer/U420.rb', line 129

def cut
 return unless @lines_used > 0

	feed(@options[:lines_total] - @lines_used + 1)
	stamp
 print(0x0c.chr)
	@lines_used = 0 
end

#feed(lines = 1) ⇒ Object

Skip some lines, default is 1.



118
119
120
121
# File 'lib/invoice_printer/U420.rb', line 118

def feed(lines = 1)
 @lines_used += lines
	@output.write("#{ESC}d#{lines.chr}")
end

#init_printerObject

Output the [ESC]@ to the printer, initialize the printer.



113
114
115
# File 'lib/invoice_printer/U420.rb', line 113

def init_printer
 @output.write("#{ESC}@")
end

Print out string on the printer, the non-ascii code is automatically converted using Iconv.



139
140
141
# File 'lib/invoice_printer/U420.rb', line 139

def print(str)
	@output.write(Iconv.iconv(@options[:encoding], "utf-8//ignore", str))
end

#println(str) ⇒ Object

Print out string and feed to next line. We also do the paging logic here.



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/invoice_printer/U420.rb', line 145

def println(str)
 unless @lines_used > 0
  feed(@options[:lines_stamp])
		println(@options[:header])
	end

   str.split("\n").each do |line|
 		print("#{line}\n")

  	@lines_used += 1
	  cut if (@lines_used == @options[:lines_available] + @options[:lines_stamp])		
 end
end

#stampObject

Use this command to utilize the stamping mechanism on the printer.



124
125
126
# File 'lib/invoice_printer/U420.rb', line 124

def stamp
	@output.write("#{ESC}o")
end