Class: InvoicePrinter::U420
- Inherits:
-
Object
- Object
- InvoicePrinter::U420
- 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
-
#options ⇒ Object
Options which controls the behavior of this library.
-
#output ⇒ Object
Current output stream, which receives the command generated by this library.
Instance Method Summary collapse
-
#cut ⇒ Object
Roll the entire page out and do the cut, it then stamp on the next page, too.
-
#feed(lines = 1) ⇒ Object
Skip some lines, default is 1.
-
#init_printer ⇒ Object
Output the [ESC]@ to the printer, initialize the printer.
-
#initialize(options) ⇒ U420
constructor
Initialize a new instance of U420 printer API interface.
-
#print(str) ⇒ Object
Print out string on the printer, the non-ascii code is automatically converted using Iconv.
-
#println(str) ⇒ Object
Print out string and feed to next line.
-
#stamp ⇒ Object
Use this command to utilize the stamping mechanism on the printer.
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 = DEFAULT_OPTIONS.merge() @output = @options[:output] @lines_used = 0 self.init_printer unless [:dont_init_printer] end |
Instance Attribute Details
#options ⇒ Object
77 78 79 |
# File 'lib/invoice_printer/U420.rb', line 77 def @options end |
#output ⇒ Object
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
#cut ⇒ Object
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_printer ⇒ Object
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(str) ⇒ Object
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 |
#stamp ⇒ Object
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 |